|
@@ -1,15 +1,11 @@
|
|
|
package com.example.projectback.controllers;
|
|
package com.example.projectback.controllers;
|
|
|
|
|
|
|
|
-import com.example.projectback.model.Changeset;
|
|
|
|
|
-import com.example.projectback.model.Student;
|
|
|
|
|
-import com.example.projectback.model.Subject;
|
|
|
|
|
-import com.example.projectback.model.SubjectStudent;
|
|
|
|
|
-import com.example.projectback.repository.StudentRepository;
|
|
|
|
|
-import com.example.projectback.repository.SubjectRepository;
|
|
|
|
|
-import com.example.projectback.repository.SubjectStudentRepository;
|
|
|
|
|
|
|
+import com.example.projectback.model.*;
|
|
|
|
|
+import com.example.projectback.repository.*;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
@@ -24,18 +20,45 @@ public class SubjectController {
|
|
|
private final SubjectRepository subjectRepository;
|
|
private final SubjectRepository subjectRepository;
|
|
|
private final SubjectStudentRepository subjectStudentRepository;
|
|
private final SubjectStudentRepository subjectStudentRepository;
|
|
|
private final StudentRepository studentRepository;
|
|
private final StudentRepository studentRepository;
|
|
|
|
|
+ private final GradeRepository gradeRepository;
|
|
|
|
|
+ private final UserRepository userRepository;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- public SubjectController(SubjectRepository subjectRepository, SubjectStudentRepository subjectStudentRepository, StudentRepository studentRepository) {
|
|
|
|
|
|
|
+ public SubjectController(SubjectRepository subjectRepository, SubjectStudentRepository subjectStudentRepository,
|
|
|
|
|
+ StudentRepository studentRepository, GradeRepository gradeRepository,
|
|
|
|
|
+ UserRepository userRepository) {
|
|
|
this.subjectRepository = subjectRepository;
|
|
this.subjectRepository = subjectRepository;
|
|
|
this.subjectStudentRepository = subjectStudentRepository;
|
|
this.subjectStudentRepository = subjectStudentRepository;
|
|
|
this.studentRepository = studentRepository;
|
|
this.studentRepository = studentRepository;
|
|
|
|
|
+ this.gradeRepository = gradeRepository;
|
|
|
|
|
+ this.userRepository = userRepository;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@GetMapping
|
|
@GetMapping
|
|
|
- public List<Subject> getAllSubjects() {return subjectRepository.findAll();}
|
|
|
|
|
|
|
+ @PreAuthorize("hasRole('ADMIN')")
|
|
|
|
|
+ public List<Subject> getAllSubjects() {
|
|
|
|
|
+ return subjectRepository.findAll();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @GetMapping("/student/{id}")
|
|
|
|
|
+ @PreAuthorize("hasRole('ADMIN') or hasRole('USER')")
|
|
|
|
|
+ public List<Subject> getAllSubjectsForStudent(@PathVariable("id") Long studentID) {
|
|
|
|
|
+ User user = userRepository.getById(studentID);
|
|
|
|
|
+ Student student = studentRepository.getByEmail(user.getEmail());
|
|
|
|
|
+ List<SubjectStudent> assoc = subjectStudentRepository.getAllByStudent(student);
|
|
|
|
|
+ List<Subject> studentSubjects = new ArrayList<>();
|
|
|
|
|
+ for (SubjectStudent subjectStudent: assoc) {
|
|
|
|
|
+ Subject subject = subjectStudent.getSubject();
|
|
|
|
|
+ List<SubjectStudent> newAssoc = new ArrayList<>();
|
|
|
|
|
+ newAssoc.add(new SubjectStudent(subject, student));
|
|
|
|
|
+ subject.setStudentsAssoc(newAssoc);
|
|
|
|
|
+ studentSubjects.add(subject);
|
|
|
|
|
+ }
|
|
|
|
|
+ return studentSubjects;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
@GetMapping("/{id}")
|
|
@GetMapping("/{id}")
|
|
|
|
|
+ @PreAuthorize("hasRole('ADMIN')")
|
|
|
public ResponseEntity<Subject> getSubject(@PathVariable("id") Long id) {
|
|
public ResponseEntity<Subject> getSubject(@PathVariable("id") Long id) {
|
|
|
Optional<Subject> subject = subjectRepository.findById(id);
|
|
Optional<Subject> subject = subjectRepository.findById(id);
|
|
|
if (subject.isEmpty()) {
|
|
if (subject.isEmpty()) {
|
|
@@ -45,27 +68,41 @@ public class SubjectController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PostMapping
|
|
@PostMapping
|
|
|
|
|
+ @PreAuthorize("hasRole('ADMIN')")
|
|
|
public ResponseEntity<Subject> addSubject(@RequestBody Subject subject) {
|
|
public ResponseEntity<Subject> addSubject(@RequestBody Subject subject) {
|
|
|
subjectRepository.save(subject);
|
|
subjectRepository.save(subject);
|
|
|
return new ResponseEntity<>(subject, HttpStatus.CREATED);
|
|
return new ResponseEntity<>(subject, HttpStatus.CREATED);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/delete")
|
|
@PostMapping("/delete")
|
|
|
|
|
+ @PreAuthorize("hasRole('ADMIN')")
|
|
|
public ResponseEntity<Changeset> deleteSubjects(@RequestBody Changeset changeset) {
|
|
public ResponseEntity<Changeset> deleteSubjects(@RequestBody Changeset changeset) {
|
|
|
for (Long recId : changeset.getRecords()) {
|
|
for (Long recId : changeset.getRecords()) {
|
|
|
- subjectRepository.deleteById(recId);
|
|
|
|
|
|
|
+ Optional<Subject> subject = subjectRepository.findById(recId);
|
|
|
|
|
+ if (subject.isEmpty()) continue;
|
|
|
|
|
+ List<SubjectStudent> subjectStudents = subjectStudentRepository.getAllBySubject(subject.get());
|
|
|
|
|
+ List<Grade> grades = gradeRepository.getAllBySubjectID(subject.get().getId());
|
|
|
|
|
+ List<Student> allStudents = studentRepository.findAll();
|
|
|
|
|
+ for(Student student: allStudents) {
|
|
|
|
|
+ student.getGrades().removeAll(grades);
|
|
|
|
|
+ studentRepository.save(student);
|
|
|
|
|
+ }
|
|
|
|
|
+ subjectStudentRepository.deleteAll(subjectStudents);
|
|
|
|
|
+ gradeRepository.deleteAll(grades);
|
|
|
|
|
+ subjectRepository.delete(subject.get());
|
|
|
}
|
|
}
|
|
|
return new ResponseEntity<>(changeset, HttpStatus.OK);
|
|
return new ResponseEntity<>(changeset, HttpStatus.OK);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PatchMapping("/{id}")
|
|
@PatchMapping("/{id}")
|
|
|
|
|
+ @PreAuthorize("hasRole('ADMIN')")
|
|
|
public ResponseEntity<Subject> updatePartOfSubject(@RequestBody Map<String, Object> updates, @PathVariable("id") Long id) {
|
|
public ResponseEntity<Subject> updatePartOfSubject(@RequestBody Map<String, Object> updates, @PathVariable("id") Long id) {
|
|
|
- Subject subject = subjectRepository.getById(id);
|
|
|
|
|
- if (subject == null) {
|
|
|
|
|
|
|
+ Optional<Subject> subject = subjectRepository.findById(id);
|
|
|
|
|
+ if (subject.isEmpty()) {
|
|
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
}
|
|
}
|
|
|
- partialUpdates(subject, updates);
|
|
|
|
|
- return new ResponseEntity<>(HttpStatus.OK);
|
|
|
|
|
|
|
+ partialUpdates(subject.get(), updates);
|
|
|
|
|
+ return new ResponseEntity<>(subject.get(), HttpStatus.OK);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void partialUpdates(Subject subject, Map<String, Object> updates) {
|
|
private void partialUpdates(Subject subject, Map<String, Object> updates) {
|
|
@@ -73,8 +110,8 @@ public class SubjectController {
|
|
|
subject.setName((String) updates.get("nane"));
|
|
subject.setName((String) updates.get("nane"));
|
|
|
}
|
|
}
|
|
|
if (updates.containsKey("studentAssoc")) {
|
|
if (updates.containsKey("studentAssoc")) {
|
|
|
- ArrayList<SubjectStudent> newStudents = new ArrayList<>();
|
|
|
|
|
- for (Integer studentId : (ArrayList<Integer>)updates.get("studentAssoc")) {
|
|
|
|
|
|
|
+ List<SubjectStudent> newStudents = subject.getStudentsAssoc();
|
|
|
|
|
+ for (Integer studentId : (List<Integer>) updates.get("studentAssoc")) {
|
|
|
Student student = this.studentRepository.getById(studentId.longValue());
|
|
Student student = this.studentRepository.getById(studentId.longValue());
|
|
|
SubjectStudent newAssoc = new SubjectStudent(subject, student);
|
|
SubjectStudent newAssoc = new SubjectStudent(subject, student);
|
|
|
this.subjectStudentRepository.save(newAssoc);
|
|
this.subjectStudentRepository.save(newAssoc);
|
|
@@ -84,4 +121,19 @@ public class SubjectController {
|
|
|
}
|
|
}
|
|
|
subjectRepository.save(subject);
|
|
subjectRepository.save(subject);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @DeleteMapping("/{subjectID}/{studentID}")
|
|
|
|
|
+ @PreAuthorize("hasRole('ADMIN')")
|
|
|
|
|
+ public ResponseEntity<Subject> removeStudentFromSubject(@PathVariable("subjectID") Long subjectID, @PathVariable Long studentID) {
|
|
|
|
|
+ Optional<Subject> subject = subjectRepository.findById(subjectID);
|
|
|
|
|
+ Optional<Student> student = studentRepository.findById(studentID);
|
|
|
|
|
+ if (subject.isEmpty() || student.isEmpty()) {
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
|
|
+ }
|
|
|
|
|
+ SubjectStudent assoc = this.subjectStudentRepository.getByStudentAndSubject(student.get(), subject.get());
|
|
|
|
|
+ subject.get().getStudentsAssoc().remove(assoc);
|
|
|
|
|
+ this.subjectStudentRepository.delete(assoc);
|
|
|
|
|
+ this.subjectRepository.save(subject.get());
|
|
|
|
|
+ return new ResponseEntity<>(subject.get(), HttpStatus.OK);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|