|
@@ -0,0 +1,80 @@
|
|
|
|
|
+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.Subject;
|
|
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.model.Teacher;
|
|
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.repository.AccountRepository;
|
|
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.repository.SubjectRepository;
|
|
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.repository.TeacherRepository;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+@RestController
|
|
|
|
|
+@CrossOrigin(origins = "http://localhost:4200")
|
|
|
|
|
+@RequestMapping("/subjects")
|
|
|
|
|
+public class SubjectController {
|
|
|
|
|
+ private SubjectRepository subjectRepository;
|
|
|
|
|
+ private AccountRepository accountRepository;
|
|
|
|
|
+ private TeacherRepository teacherRepository;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ public SubjectController(SubjectRepository subjectRepository, AccountRepository accountRepository, TeacherRepository teacherRepository) {
|
|
|
|
|
+ this.subjectRepository = subjectRepository;
|
|
|
|
|
+ this.accountRepository = accountRepository;
|
|
|
|
|
+ this.teacherRepository = teacherRepository;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @GetMapping
|
|
|
|
|
+ public List<Subject> findAllSubjects() {
|
|
|
|
|
+ return subjectRepository.findAll();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @GetMapping(value = "/{id}")
|
|
|
|
|
+ public ResponseEntity<Subject> findSubject(@PathVariable("id") long id){
|
|
|
|
|
+ try {
|
|
|
|
|
+ Subject subject = subjectRepository.findById(id);
|
|
|
|
|
+ if (subject == null){
|
|
|
|
|
+ System.out.println("Subject with id " + id + " not found");
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
|
|
+ }
|
|
|
|
|
+ return new ResponseEntity<>(subject, HttpStatus.OK);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ System.out.println("Error finding subject: " + e.getMessage());
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @PostMapping
|
|
|
|
|
+ public ResponseEntity<?> addSubject(@RequestBody Subject subject){
|
|
|
|
|
+ try {
|
|
|
|
|
+ if(subjectRepository.existsByName(subject.getName())){
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
|
|
|
|
+ }
|
|
|
|
|
+ subjectRepository.save(subject);
|
|
|
|
|
+ return new ResponseEntity<>(subject, HttpStatus.CREATED);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ System.out.println("Error adding subject: " + e.getMessage());
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @DeleteMapping(value = "/{id}")
|
|
|
|
|
+ public ResponseEntity<Subject> deleteSubject(@PathVariable("id") long id){
|
|
|
|
|
+ try {
|
|
|
|
|
+ Subject subject = subjectRepository.findById(id);
|
|
|
|
|
+ if (subject == null){
|
|
|
|
|
+ System.out.println("Subject with id " + id + " not found");
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
|
|
+ }
|
|
|
|
|
+ subjectRepository.delete(subject);
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ System.out.println("Error deleting subject: " + e.getMessage());
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|