Newer
Older
teacher-diary / src / main / java / ru / mcs / diary / dashboard / DashboardController.java
package ru.mcs.diary.dashboard;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import ru.mcs.diary.auth.CurrentUser;
import ru.mcs.diary.auth.CustomUserDetails;
import ru.mcs.diary.group.GroupRepository;
import ru.mcs.diary.lesson.LessonService;
import ru.mcs.diary.parent.ParentRepository;
import ru.mcs.diary.student.StudentRepository;

import java.time.LocalDate;

@Controller
@RequestMapping("/teacher")
@RequiredArgsConstructor
public class DashboardController {

    private final StudentRepository studentRepository;
    private final ParentRepository parentRepository;
    private final GroupRepository groupRepository;
    private final LessonService lessonService;

    @GetMapping("/dashboard")
    public String dashboard(@CurrentUser CustomUserDetails user, Model model) {
        Long teacherId = user.getTeacherId();

        model.addAttribute("user", user);
        model.addAttribute("activeMenu", "dashboard");
        model.addAttribute("pageTitle", "Главная");

        // Статистика
        model.addAttribute("studentCount", studentRepository.countByTeacherId(teacherId));
        model.addAttribute("parentCount", parentRepository.countByTeacherId(teacherId));
        model.addAttribute("groupCount", groupRepository.countByTeacherId(teacherId));
        model.addAttribute("todayLessonsCount", lessonService.countTodayLessons(teacherId));

        // Занятия на сегодня
        model.addAttribute("todayLessons", lessonService.getLessonsByDate(teacherId, LocalDate.now()));

        return "dashboard/index";
    }
}