Cleaned up some of the StudentEntity controller

This commit is contained in:
Andrew Lalis 2019-05-11 10:06:59 +02:00 committed by andrewlalis
parent 1f115f8c3f
commit 13429e3159
8 changed files with 175 additions and 57 deletions

View File

@ -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;
}

View File

@ -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";
}
}

View File

@ -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;
}

View File

@ -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<Course> optionalCourse = this.courseRepository.findByCode(courseCode);
optionalCourse.ifPresent(course -> {
course.addParticipant(newStudent);
newStudent.assignToCourse(course);
this.courseRepository.save(course);
this.studentRepository.save(newStudent);
});
Course course = null;
if (optionalCourse.isPresent()) {
course = optionalCourse.get();
}
this.studentService.createStudent(newStudent, course);
return "redirect:/students";
}
}

View File

@ -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<Student> 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<Student> 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) {

View File

@ -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<Student> 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 <code>Student</code> 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<Student> optionalStudent = this.studentRepository.findById(id);
optionalStudent.ifPresent(student -> this.studentService.editStudent(student, editedStudent));
return "redirect:/students/{id}";
}
}

View File

@ -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;
}

View File

@ -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 <code>student</code> with the attributes found
* in the provided <code>editedStudent</code> 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);
}
}