From 13429e31595d872e1d9996d093eea4f260464ab7 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sat, 11 May 2019 10:06:59 +0200 Subject: [PATCH] Cleaned up some of the StudentEntity controller --- .../{Courses.java => CoursesController.java} | 4 +- .../controllers/StudentsController.java | 30 ++++++++++ ...java => TeachingAssistantsController.java} | 7 ++- .../StudentCreateController.java} | 42 ++++++-------- ...tity.java => StudentEntityController.java} | 32 ++-------- .../entity/StudentEntityEditController.java | 58 +++++++++++++++++++ ...=> TeachingAssistantEntityController.java} | 7 ++- .../services/StudentService.java | 52 +++++++++++++++++ 8 files changed, 175 insertions(+), 57 deletions(-) rename src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/{Courses.java => CoursesController.java} (93%) create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/StudentsController.java rename src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/{TeachingAssistants.java => TeachingAssistantsController.java} (90%) rename src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/{Students.java => students/StudentCreateController.java} (55%) rename src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/{StudentEntity.java => StudentEntityController.java} (60%) create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/entity/StudentEntityEditController.java rename src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/teaching_assistants/{TeachingAssistantEntity.java => TeachingAssistantEntityController.java} (88%) create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/services/StudentService.java diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/Courses.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/CoursesController.java similarity index 93% rename from src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/Courses.java rename to src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/CoursesController.java index ea5df0e..8883abf 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/Courses.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/CoursesController.java @@ -12,11 +12,11 @@ import org.springframework.web.bind.annotation.PostMapping; * Controller for the list of courses in the system. */ @Controller -public class Courses { +public class CoursesController { private CourseRepository courseRepository; - protected Courses(CourseRepository courseRepository) { + protected CoursesController(CourseRepository courseRepository) { this.courseRepository = courseRepository; } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/StudentsController.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/StudentsController.java new file mode 100644 index 0000000..66b8aca --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/StudentsController.java @@ -0,0 +1,30 @@ +package nl.andrewlalis.teaching_assistant_assistant.controllers; + +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +/** + * Controller for operations dealing with the global collection of students, not particular to one course. + */ +@Controller +public class StudentsController { + + private StudentRepository studentRepository; + + protected StudentsController(StudentRepository studentRepository) { + this.studentRepository = studentRepository; + } + + /** + * Gets a list of all students. + * @param model The view model. + * @return The template for displaying a list of students. + */ + @GetMapping("/students") + public String get(Model model) { + model.addAttribute("students", this.studentRepository.findAll()); + return "students"; + } +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/TeachingAssistants.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/TeachingAssistantsController.java similarity index 90% rename from src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/TeachingAssistants.java rename to src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/TeachingAssistantsController.java index a863b10..91cc428 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/TeachingAssistants.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/TeachingAssistantsController.java @@ -13,13 +13,16 @@ import org.springframework.web.bind.annotation.PostMapping; import java.util.Optional; +/** + * Controller for the list of teaching assistants in the system. + */ @Controller -public class TeachingAssistants { +public class TeachingAssistantsController { private TeachingAssistantRepository teachingAssistantRepository; private CourseRepository courseRepository; - protected TeachingAssistants(TeachingAssistantRepository teachingAssistantRepository, CourseRepository courseRepository) { + protected TeachingAssistantsController(TeachingAssistantRepository teachingAssistantRepository, CourseRepository courseRepository) { this.teachingAssistantRepository = teachingAssistantRepository; this.courseRepository = courseRepository; } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/Students.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentCreateController.java similarity index 55% rename from src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/Students.java rename to src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentCreateController.java index 3b4a14b..ffce89b 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/Students.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentCreateController.java @@ -1,9 +1,9 @@ -package nl.andrewlalis.teaching_assistant_assistant.controllers; +package nl.andrewlalis.teaching_assistant_assistant.controllers.students; import nl.andrewlalis.teaching_assistant_assistant.model.Course; import nl.andrewlalis.teaching_assistant_assistant.model.people.Student; import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository; -import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository; +import nl.andrewlalis.teaching_assistant_assistant.services.StudentService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -14,25 +14,23 @@ import org.springframework.web.bind.annotation.RequestParam; import java.util.Optional; /** - * Controller for operations dealing with the global collection of students, not particular to one course. + * Controller for creating a new student. */ @Controller -public class Students { +public class StudentCreateController { + /** + * A constant which defines what value is returned if the user says that the newly created student should not be + * part of a course. + */ private static final String NO_COURSE = "NO_COURSE_SELECTED"; - private StudentRepository studentRepository; private CourseRepository courseRepository; + private StudentService studentService; - protected Students(StudentRepository studentRepository, CourseRepository courseRepository) { - this.studentRepository = studentRepository; + protected StudentCreateController(CourseRepository courseRepository, StudentService studentService) { this.courseRepository = courseRepository; - } - - @GetMapping("/students") - public String get(Model model) { - model.addAttribute("students", this.studentRepository.findAll()); - return "students"; + this.studentService = studentService; } @GetMapping("/students/create") @@ -49,20 +47,16 @@ public class Students { ) public String postCreate( @ModelAttribute Student newStudent, - @RequestParam(value = "course_code", required = false) String courseCode + @RequestParam(value = "course_code", required = false, defaultValue = NO_COURSE) String courseCode ) { - this.studentRepository.save(newStudent); - - if (courseCode != null && !courseCode.equals(NO_COURSE)) { - Optional optionalCourse = this.courseRepository.findByCode(courseCode); - optionalCourse.ifPresent(course -> { - course.addParticipant(newStudent); - newStudent.assignToCourse(course); - this.courseRepository.save(course); - this.studentRepository.save(newStudent); - }); + Optional optionalCourse = this.courseRepository.findByCode(courseCode); + Course course = null; + if (optionalCourse.isPresent()) { + course = optionalCourse.get(); } + this.studentService.createStudent(newStudent, course); + return "redirect:/students"; } } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntity.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntityController.java similarity index 60% rename from src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntity.java rename to src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntityController.java index 9fd8547..0f679d7 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntity.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntityController.java @@ -9,20 +9,21 @@ import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeamReposi 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.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; import java.util.Optional; +/** + * Controller for a single student entity. + */ @Controller -public class StudentEntity { +public class StudentEntityController { private StudentRepository studentRepository; private TeamRepository teamRepository; private CourseRepository courseRepository; - protected StudentEntity(StudentRepository studentRepository, TeamRepository teamRepository, CourseRepository courseRepository) { + protected StudentEntityController(StudentRepository studentRepository, TeamRepository teamRepository, CourseRepository courseRepository) { this.studentRepository = studentRepository; this.teamRepository = teamRepository; this.courseRepository = courseRepository; @@ -35,30 +36,7 @@ public class StudentEntity { return "students/entity"; } - @GetMapping("/students/{id}/edit") - public String getEdit(@PathVariable long id, Model model) { - Optional optionalStudent = this.studentRepository.findById(id); - optionalStudent.ifPresent(student -> model.addAttribute("student", student)); - return "students/entity/edit"; - } - @PostMapping( - value = "/students/{id}/edit", - consumes = "application/x-www-form-urlencoded" - ) - public String post(@ModelAttribute Student editedStudent, @PathVariable long id) { - Optional optionalStudent = this.studentRepository.findById(id); - optionalStudent.ifPresent(student -> { - student.setFirstName(editedStudent.getFirstName()); - student.setLastName(editedStudent.getLastName()); - student.setEmailAddress(editedStudent.getEmailAddress()); - student.setGithubUsername(editedStudent.getGithubUsername()); - student.setStudentNumber(editedStudent.getStudentNumber()); - this.studentRepository.save(student); - }); - - return "redirect:/students/{id}"; - } @GetMapping("/students/{id}/remove") public String getRemove(@PathVariable long id) { diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/entity/StudentEntityEditController.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/entity/StudentEntityEditController.java new file mode 100644 index 0000000..a8d587c --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/entity/StudentEntityEditController.java @@ -0,0 +1,58 @@ +package nl.andrewlalis.teaching_assistant_assistant.controllers.students.entity; + +import nl.andrewlalis.teaching_assistant_assistant.model.people.Student; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository; +import nl.andrewlalis.teaching_assistant_assistant.services.StudentService; +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.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.Optional; + +/** + * Controller for editing a student entity. + */ +@Controller("/students/{id}/edit") +public class StudentEntityEditController { + + private StudentRepository studentRepository; + private StudentService studentService; + + protected StudentEntityEditController(StudentRepository studentRepository, StudentService studentService) { + this.studentRepository = studentRepository; + this.studentService = studentService; + } + + /** + * Get the data of the student whose information is going to be edited, and add that to the model to be rendered. + * @param id The id of the student to edit. + * @param model The view model. + * @return The edit template which will be populated with the student's data. + */ + @GetMapping("/students/{id}/edit") + public String getEdit(@PathVariable long id, Model model) { + Optional optionalStudent = this.studentRepository.findById(id); + optionalStudent.ifPresent(student -> model.addAttribute("student", student)); + return "students/entity/edit"; + } + + /** + * Receives edited data about a student and saves it. + * @param editedStudent A temporary Student object containing the edited information. + * @param id The id of the student to edit the information of. + * @return A redirect to the entity page for the student whose information was just edited. + */ + @PostMapping( + value = "/students/{id}/edit", + consumes = "application/x-www-form-urlencoded" + ) + public String post(@ModelAttribute Student editedStudent, @PathVariable long id) { + Optional optionalStudent = this.studentRepository.findById(id); + optionalStudent.ifPresent(student -> this.studentService.editStudent(student, editedStudent)); + + return "redirect:/students/{id}"; + } +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/teaching_assistants/TeachingAssistantEntity.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/teaching_assistants/TeachingAssistantEntityController.java similarity index 88% rename from src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/teaching_assistants/TeachingAssistantEntity.java rename to src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/teaching_assistants/TeachingAssistantEntityController.java index 99991b3..acf4c94 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/teaching_assistants/TeachingAssistantEntity.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/teaching_assistants/TeachingAssistantEntityController.java @@ -9,12 +9,15 @@ import org.springframework.web.bind.annotation.PathVariable; import java.util.Optional; +/** + * Controller for a single teaching assistant entity. + */ @Controller -public class TeachingAssistantEntity { +public class TeachingAssistantEntityController { private TeachingAssistantRepository teachingAssistantRepository; - protected TeachingAssistantEntity(TeachingAssistantRepository teachingAssistantRepository) { + protected TeachingAssistantEntityController(TeachingAssistantRepository teachingAssistantRepository) { this.teachingAssistantRepository = teachingAssistantRepository; } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/services/StudentService.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/services/StudentService.java new file mode 100644 index 0000000..258c771 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/services/StudentService.java @@ -0,0 +1,52 @@ +package nl.andrewlalis.teaching_assistant_assistant.services; + +import nl.andrewlalis.teaching_assistant_assistant.model.Course; +import nl.andrewlalis.teaching_assistant_assistant.model.people.Student; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository; +import org.springframework.stereotype.Service; + +/** + * Helps with manipulation and various operations on individual students or groups of students (not teams). + */ +@Service +public class StudentService { + + private StudentRepository studentRepository; + private CourseRepository courseRepository; + + protected StudentService (StudentRepository studentRepository, CourseRepository courseRepository) { + this.studentRepository = studentRepository; + this.courseRepository = courseRepository; + } + + /** + * Creates a new student and assigns them to a course, if provided. + * @param student An unsaved student model. + * @param course The course to assign the student to. This may be null. + */ + public void createStudent(Student student, Course course) { + if (course != null) { + course.addParticipant(student); + student.assignToCourse(course); + this.courseRepository.save(course); + this.studentRepository.save(student); + } + } + + /** + * Edits a students' data. More specifically, updates the provided student with the attributes found + * in the provided editedStudent object. + * @param student The student to update. + * @param editedStudent A model containing updated attributes to assign to the given student. + */ + public void editStudent(Student student, Student editedStudent) { + student.setFirstName(editedStudent.getFirstName()); + student.setLastName(editedStudent.getLastName()); + student.setStudentNumber(editedStudent.getStudentNumber()); + student.setEmailAddress(editedStudent.getEmailAddress()); + student.setGithubUsername(editedStudent.getGithubUsername()); + this.studentRepository.save(student); + } + +}