Added web config, added student service remove method.

This commit is contained in:
Andrew Lalis 2019-05-13 11:40:24 +02:00 committed by andrewlalis
parent 731f0ba596
commit 06cbf34b54
4 changed files with 105 additions and 35 deletions

View File

@ -0,0 +1,41 @@
package nl.andrewlalis.teaching_assistant_assistant.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.authorizeRequests()
.antMatchers("/resources/**").permitAll().anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
public UserDetailsService userDetailsService () {
UserDetails user = User.withDefaultPasswordEncoder()
.username("andrewlalis")
.password("test")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}

View File

@ -1,11 +1,7 @@
package nl.andrewlalis.teaching_assistant_assistant.controllers.students; 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.people.Student;
import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.Team;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository; import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeamRepository;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -20,13 +16,9 @@ import java.util.Optional;
public class StudentEntityController { public class StudentEntityController {
private StudentRepository studentRepository; private StudentRepository studentRepository;
private TeamRepository teamRepository;
private CourseRepository courseRepository;
protected StudentEntityController(StudentRepository studentRepository, TeamRepository teamRepository, CourseRepository courseRepository) { protected StudentEntityController(StudentRepository studentRepository) {
this.studentRepository = studentRepository; this.studentRepository = studentRepository;
this.teamRepository = teamRepository;
this.courseRepository = courseRepository;
} }
@GetMapping("/students/{id}") @GetMapping("/students/{id}")
@ -35,29 +27,4 @@ public class StudentEntityController {
optionalStudent.ifPresent(student -> model.addAttribute("student", student)); optionalStudent.ifPresent(student -> model.addAttribute("student", student));
return "students/entity"; return "students/entity";
} }
@GetMapping("/students/{id}/remove")
public String getRemove(@PathVariable long id) {
Optional<Student> optionalStudent = this.studentRepository.findById(id);
optionalStudent.ifPresent(student -> {
for (Team team : student.getTeams()) {
team.removeMember(student);
student.removeFromAssignedTeam(team);
this.teamRepository.save(team);
}
for (Course course : student.getCourses()) {
course.removeParticipant(student);
student.removeFromAssignedCourse(course);
this.courseRepository.save(course);
}
this.studentRepository.delete(student);
});
return "redirect:/students";
}
} }

View File

@ -0,0 +1,34 @@
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.Optional;
/**
* Controller for removing a student from the application.
*/
@Controller
public class StudentEntityRemoveController {
private StudentService studentService;
private StudentRepository studentRepository;
protected StudentEntityRemoveController(StudentService studentService, StudentRepository studentRepository) {
this.studentService = studentService;
this.studentRepository = studentRepository;
}
@GetMapping("/students/{id}/remove")
public String getRemove(@PathVariable long id) {
Optional<Student> optionalStudent = this.studentRepository.findById(id);
optionalStudent.ifPresent(student -> this.studentService.removeStudent(student));
return "redirect:/students";
}
}

View File

@ -2,10 +2,14 @@ package nl.andrewlalis.teaching_assistant_assistant.services;
import nl.andrewlalis.teaching_assistant_assistant.model.Course; import nl.andrewlalis.teaching_assistant_assistant.model.Course;
import nl.andrewlalis.teaching_assistant_assistant.model.people.Student; import nl.andrewlalis.teaching_assistant_assistant.model.people.Student;
import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.Team;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository; import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository; import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRepository;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeamRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
/** /**
* Helps with manipulation and various operations on individual students or groups of students (not teams). * Helps with manipulation and various operations on individual students or groups of students (not teams).
*/ */
@ -14,10 +18,12 @@ public class StudentService {
private StudentRepository studentRepository; private StudentRepository studentRepository;
private CourseRepository courseRepository; private CourseRepository courseRepository;
private TeamRepository teamRepository;
protected StudentService (StudentRepository studentRepository, CourseRepository courseRepository) { protected StudentService (StudentRepository studentRepository, CourseRepository courseRepository, TeamRepository teamRepository) {
this.studentRepository = studentRepository; this.studentRepository = studentRepository;
this.courseRepository = courseRepository; this.courseRepository = courseRepository;
this.teamRepository = teamRepository;
} }
/** /**
@ -49,4 +55,26 @@ public class StudentService {
this.studentRepository.save(student); this.studentRepository.save(student);
} }
/**
* Removes a student from the application completely.
* @param student The student to remove.
*/
public void removeStudent(Student student) {
List<Team> teams = student.getTeams();
for (Team team : teams) {
team.removeMember(student);
student.removeFromAssignedTeam(team);
this.teamRepository.save(team);
}
List<Course> courses = student.getCourses();
for (Course course : courses) {
course.removeParticipant(student);
student.removeFromAssignedCourse(course);
this.courseRepository.save(course);
}
this.studentRepository.delete(student);
}
} }