Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 127 additions & 119 deletions src/main/java/de/tekup/studentsabsence/controllers/GroupController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.tekup.studentsabsence.controllers;


import de.tekup.studentsabsence.entities.Absence;
import de.tekup.studentsabsence.entities.Group;
import de.tekup.studentsabsence.entities.Student;
Expand All @@ -24,123 +23,132 @@
@RequestMapping("/groups")
@AllArgsConstructor
public class GroupController {
private final GroupService groupService;
private final SubjectService subjectService;
private final GroupSubjectService groupSubjectService;
private final AbsenceService absenceService;

@GetMapping({"", "/"})
public String index(Model model) {
List<Group> groups = groupService.getAllGroups();
model.addAttribute("groups", groups);
return "groups/index";
}

@GetMapping("/add")
public String addView(Model model) {
model.addAttribute("levels", LevelEnum.values());
model.addAttribute("specialities", SpecialityEnum.values());
model.addAttribute("group", new Group());
return "groups/add";
}

@PostMapping("/add")
public String add(@Valid Group group, BindingResult bindingResult, Model model) {
if(bindingResult.hasErrors()) {
model.addAttribute("levels", LevelEnum.values());
model.addAttribute("specialities", SpecialityEnum.values());
return "groups/add";
}

groupService.addGroup(group);
return "redirect:/groups";
}

@GetMapping("/{id}/update")
public String updateView(@PathVariable long id, Model model) {
model.addAttribute("levels", LevelEnum.values());
model.addAttribute("specialities", SpecialityEnum.values());
model.addAttribute("group", groupService.getGroupById(id));
return "groups/update";
}

@PostMapping("/{id}/update")
public String update(@PathVariable long id, @Valid Group group, BindingResult bindingResult, Model model) {
if(bindingResult.hasErrors()) {
model.addAttribute("levels", LevelEnum.values());
model.addAttribute("specialities", SpecialityEnum.values());
return "groups/update";
}
groupService.updateGroup(group);
return "redirect:/groups";
}

@GetMapping("/{id}/delete")
public String delete(@PathVariable long id) {
groupService.deleteGroup(id);
return "redirect:/groups";
}

@GetMapping("/{id}/show")
public String show(@PathVariable long id, Model model) {
Group group = groupService.getGroupById(id);

model.addAttribute("group", group);
model.addAttribute("groupSubjects",groupSubjectService.getSubjectsByGroupId(id));
model.addAttribute("students",group.getStudents());
model.addAttribute("absenceService", absenceService);

group.getStudents().forEach(student -> {

});

return "groups/show";
}

@GetMapping("/{id}/add-subject")
public String addSubjectView(Model model , @PathVariable Long id){
model.addAttribute("groupSubjectHolder", new GroupSubjectHolder());
model.addAttribute("group",groupService.getGroupById(id));
model.addAttribute("subjects",subjectService.getAllSubjects());
return "groups/add-subject";

}

@PostMapping("/{id}/add-subject")
public String addSubject(@PathVariable Long id, @Valid GroupSubjectHolder groupSubjectHolder, BindingResult bindingResult, Model model){
if(bindingResult.hasErrors()) {
model.addAttribute("group",groupService.getGroupById(id));
model.addAttribute("subjects",subjectService.getAllSubjects());
return "groups/add-subject";
}

Group group = groupService.getGroupById(id);
groupSubjectService.addSubjectToGroup(group, groupSubjectHolder.getSubject(), groupSubjectHolder.getHours());
return "redirect:/groups/"+id+"/add-subject";
}

@GetMapping("/{gid}/subject/{sid}/delete")
public String deleteSubject(@PathVariable Long gid, @PathVariable Long sid){
groupSubjectService.deleteSubjectFromGroup(gid, sid);
return "redirect:/groups/"+gid+"/show";
}

@GetMapping("/{id}/add-absences")
public String addAbsenceView(@PathVariable long id, Model model) {
Group group = groupService.getGroupById(id);

model.addAttribute("group", group);
model.addAttribute("absence", new Absence());
model.addAttribute("groupSubjects", groupSubjectService.getSubjectsByGroupId(id));
model.addAttribute("students", group.getStudents());

return "groups/add-absences";
}

@PostMapping("/{id}/add-absences")
public String addAbsence(@PathVariable long id, @Valid Absence absence, BindingResult bindingResult, @RequestParam(value = "students", required = false) List<Student> students, Model model) {
//TODO Complete the body of this method
return "redirect:/groups/"+id+"/add-absences";
}
private final GroupService groupService;
private final SubjectService subjectService;
private final GroupSubjectService groupSubjectService;
private final AbsenceService absenceService;

@GetMapping({ "", "/" })
public String index(Model model) {
List<Group> groups = groupService.getAllGroups();
model.addAttribute("groups", groups);
return "groups/index";
}

@GetMapping("/add")
public String addView(Model model) {
model.addAttribute("levels", LevelEnum.values());
model.addAttribute("specialities", SpecialityEnum.values());
model.addAttribute("group", new Group());
return "groups/add";
}

@PostMapping("/add")
public String add(@Valid Group group, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("levels", LevelEnum.values());
model.addAttribute("specialities", SpecialityEnum.values());
return "groups/add";
}

groupService.addGroup(group);
return "redirect:/groups";
}

@GetMapping("/{id}/update")
public String updateView(@PathVariable long id, Model model) {
model.addAttribute("levels", LevelEnum.values());
model.addAttribute("specialities", SpecialityEnum.values());
model.addAttribute("group", groupService.getGroupById(id));
return "groups/update";
}

@PostMapping("/{id}/update")
public String update(@PathVariable long id, @Valid Group group, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("levels", LevelEnum.values());
model.addAttribute("specialities", SpecialityEnum.values());
return "groups/update";
}
groupService.updateGroup(group);
return "redirect:/groups";
}

@GetMapping("/{id}/delete")
public String delete(@PathVariable long id) {
groupService.deleteGroup(id);
return "redirect:/groups";
}

@GetMapping("/{id}/show")
public String show(@PathVariable long id, Model model) {
Group group = groupService.getGroupById(id);

model.addAttribute("group", group);
model.addAttribute("groupSubjects", groupSubjectService.getSubjectsByGroupId(id));
model.addAttribute("students", group.getStudents());
model.addAttribute("absenceService", absenceService);

group.getStudents().forEach(student -> {

});

return "groups/show";
}

@GetMapping("/{id}/add-subject")
public String addSubjectView(Model model, @PathVariable Long id) {
model.addAttribute("groupSubjectHolder", new GroupSubjectHolder());
model.addAttribute("group", groupService.getGroupById(id));
model.addAttribute("subjects", subjectService.getAllSubjects());
return "groups/add-subject";

}

@PostMapping("/{id}/add-subject")
public String addSubject(@PathVariable Long id, @Valid GroupSubjectHolder groupSubjectHolder,
BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("group", groupService.getGroupById(id));
model.addAttribute("subjects", subjectService.getAllSubjects());
return "groups/add-subject";
}

Group group = groupService.getGroupById(id);
groupSubjectService.addSubjectToGroup(group, groupSubjectHolder.getSubject(), groupSubjectHolder.getHours());
return "redirect:/groups/" + id + "/add-subject";
}

@GetMapping("/{gid}/subject/{sid}/delete")
public String deleteSubject(@PathVariable Long gid, @PathVariable Long sid) {
groupSubjectService.deleteSubjectFromGroup(gid, sid);
return "redirect:/groups/" + gid + "/show";
}

@GetMapping("/{id}/add-absences")
public String addAbsenceView(@PathVariable long id, Model model) {
Group group = groupService.getGroupById(id);

model.addAttribute("group", group);
model.addAttribute("absence", new Absence());
model.addAttribute("groupSubjects", groupSubjectService.getSubjectsByGroupId(id));
model.addAttribute("students", group.getStudents());

return "groups/add-absences";
}

@PostMapping("/{id}/add-absences")
public String addAbsence(@PathVariable long id, @Valid Absence absence, BindingResult bindingResult,
@RequestParam(value = "students", required = false) List<Student> students, Model model) {
// TODO Complete the body of this method
if (bindingResult.hasErrors()) {
model.addAttribute("group", groupService.getGroupById(id));
model.addAttribute("subjects", subjectService.getAllSubjects());
model.addAttribute("students", students);
return "groups/add-absences";
}
absenceService.addAbsence(absence);
return "redirect:/groups/" + id + "/add-absences";
}

}
Loading