This repository has been archived by the owner on Feb 18, 2018. It is now read-only.
/
StudentController.java
121 lines (95 loc) · 4.24 KB
/
StudentController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package nl.infosupport.javaminor.case1.resources;
import java.util.ArrayList;
import java.util.List;
import nl.infosupport.javaminor.case1.entities.CourseInstance;
import nl.infosupport.javaminor.case1.entities.Student;
import nl.infosupport.javaminor.case1.services.CourseInstanceService;
import nl.infosupport.javaminor.case1.services.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
public class StudentController {
private static final Logger LOG = LoggerFactory.getLogger(StudentController.class);
private StudentService studentService;
private CourseInstanceService courseInstanceService;
@Autowired
public StudentController(StudentService studentService,
CourseInstanceService courseInstanceService) {
this.studentService = studentService;
this.courseInstanceService = courseInstanceService;
}
@GetMapping("/cursist/toevoegen")
public String getRegisterStudentPage(Model model) {
model.addAttribute("student", new Student());
return "addStudent";
}
@PostMapping("/cursist/toevoegen")
public String registerStudent(@ModelAttribute Student student,
RedirectAttributes redirectAttributes) {
studentService.saveStudent(student);
redirectAttributes.addFlashAttribute("message",
"Cursist met naam: " + student.getFirstName() + " " + student.getLastName()
+ " is toegevoegd.");
return "redirect:/cursusoverzicht";
}
@GetMapping("/cursist/inschrijven")
public String getAddStudentToCourseInstancePage(Model model,
@RequestParam("cursusInstantie") Long courseInstanceId) {
LOG.info("cursist/inschrijven with courseInstanceId: {}", courseInstanceId);
CourseInstance currentCourseInstance = courseInstanceService
.getCourseInstanceById(courseInstanceId);
if (currentCourseInstance == null) {
return "redirect:/cursusoverzicht";
}
model.addAttribute("courseInstance", currentCourseInstance);
List<Student> students = studentService.getStudents();
List<Student> notSignedUpStudents = new ArrayList<>();
// TODO
// check for each student if they exist... maybe ask how to do this in db?
// It is in the lookup table
for (Student student : students) {
if (student.getCourseInstances().stream()
.filter(courseInstance -> courseInstance.getId().equals(courseInstanceId))
.count() == 0) {
notSignedUpStudents.add(student);
}
}
model.addAttribute("students", notSignedUpStudents);
return "addStudentToCourseInstance";
}
@PostMapping("/cursist/inschrijven")
public String addStudentToCourseInstance(@RequestParam("student_id") Long studentId,
@RequestParam("course_instance_id") Long courseInstanceId) {
LOG.info("addStudentToCourseInstance with studentId {} and courseInstanceId {}", studentId,
courseInstanceId);
CourseInstance courseInstance = courseInstanceService.getCourseInstanceById(courseInstanceId);
Student student = studentService.getStudentById(studentId);
student.getCourseInstances().add(courseInstance);
studentService.addCourseInstance(student);
return "redirect:/cursusoverzicht";
}
// zodat ik de studentDetail pagina kan aanmaken
@GetMapping("/cursisten")
public String getStudentsPage(Model model) {
LOG.info("getStudentsPage");
List<Student> students = studentService.getStudents();
model.addAttribute("students", students);
return "students";
}
@GetMapping("/cursist/detail")
public String getStudentDetailPage(@RequestParam("cursist_id") Long studentId, Model model) {
LOG.info("getStudentDetailPage");
Student student = studentService.getStudentById(studentId);
model.addAttribute("student", student);
model.addAttribute("courseInstances", student.getCourseInstances());
return "studentDetail";
}
}