|
|
@@ -0,0 +1,77 @@
|
|
|
+package pl.dmcs.eldarmuk.backend_springboot.controller;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.model.Account;
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.model.Grade;
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.model.Student;
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.repository.AccountRepository;
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.repository.GradeRepository;
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.repository.StudentRepository;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@CrossOrigin(origins = "http://localhost:4200")
|
|
|
+@RequestMapping("/grades")
|
|
|
+public class GradeController {
|
|
|
+ private GradeRepository gradeRepository;
|
|
|
+ private AccountRepository accountRepository;
|
|
|
+ private StudentRepository studentRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public GradeController(GradeRepository gradeRepository, AccountRepository accountRepository, StudentRepository studentRepository) {
|
|
|
+ this.gradeRepository = gradeRepository;
|
|
|
+ this.accountRepository = accountRepository;
|
|
|
+ this.studentRepository = studentRepository;
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping(value = "/subject/{id}")
|
|
|
+ public List<Grade> findGradesBySubject(@PathVariable("id") long id){
|
|
|
+ return gradeRepository.findGradesBySubjectId(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping(value = "/student/{username}")
|
|
|
+ public ResponseEntity<List<Grade>> findGradesByStudent(@PathVariable("username") String username){
|
|
|
+ try {
|
|
|
+ Account account = accountRepository.findByUsername(username)
|
|
|
+ .orElseThrow(() -> new RuntimeException("Error: User not found!"));
|
|
|
+ Student student = studentRepository.findByAccountUsername(account.getUsername());
|
|
|
+ List<Grade> grades = gradeRepository.findGradesByStudentId(student.getId());
|
|
|
+ return new ResponseEntity<>(grades, HttpStatus.OK);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping
|
|
|
+ public ResponseEntity<Grade> addGrade(@RequestBody Grade grade){
|
|
|
+ try {
|
|
|
+ gradeRepository.save(grade);
|
|
|
+ return new ResponseEntity<>(grade, HttpStatus.CREATED);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PatchMapping(value = "/edit/{id}")
|
|
|
+ public ResponseEntity<Grade> editGrade(@PathVariable("id") long id, @RequestBody java.util.Map<String, Integer> body){
|
|
|
+ try {
|
|
|
+ Grade currentGrade = gradeRepository.findById(id);
|
|
|
+ if (currentGrade == null){
|
|
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
+ }
|
|
|
+ Integer grade = body.get("grade");
|
|
|
+ if (grade == null) {
|
|
|
+ return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
|
|
+ }
|
|
|
+ currentGrade.setGrade(grade);
|
|
|
+ gradeRepository.save(currentGrade);
|
|
|
+ return new ResponseEntity<>(currentGrade, HttpStatus.OK);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|