|
@@ -7,21 +7,19 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import pl.dmcs.springbootjsp_iwa.model.Student;
|
|
import pl.dmcs.springbootjsp_iwa.model.Student;
|
|
|
import pl.dmcs.springbootjsp_iwa.repository.AddressRepository;
|
|
import pl.dmcs.springbootjsp_iwa.repository.AddressRepository;
|
|
|
import pl.dmcs.springbootjsp_iwa.repository.StudentRepository;
|
|
import pl.dmcs.springbootjsp_iwa.repository.StudentRepository;
|
|
|
-
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
|
|
|
+@CrossOrigin(origins = "http://localhost:4200")
|
|
|
@RequestMapping("/students")
|
|
@RequestMapping("/students")
|
|
|
public class StudentRESTController {
|
|
public class StudentRESTController {
|
|
|
|
|
|
|
|
private final StudentRepository studentRepository;
|
|
private final StudentRepository studentRepository;
|
|
|
- private final AddressRepository addressRepository;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- public StudentRESTController(StudentRepository studentRepository, AddressRepository addressRepository) {
|
|
|
|
|
|
|
+ public StudentRESTController(StudentRepository studentRepository) {
|
|
|
this.studentRepository = studentRepository;
|
|
this.studentRepository = studentRepository;
|
|
|
- this.addressRepository = addressRepository;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@GetMapping
|
|
@GetMapping
|
|
@@ -34,24 +32,27 @@ public class StudentRESTController {
|
|
|
Student student = studentRepository.findById(id);
|
|
Student student = studentRepository.findById(id);
|
|
|
if (student == null) {
|
|
if (student == null) {
|
|
|
System.out.println("Student not found!");
|
|
System.out.println("Student not found!");
|
|
|
- return new ResponseEntity<Student>(HttpStatus.NOT_FOUND);
|
|
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
}
|
|
}
|
|
|
- return new ResponseEntity<Student>(student, HttpStatus.FOUND);
|
|
|
|
|
|
|
+ return new ResponseEntity<>(student, HttpStatus.OK);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PostMapping
|
|
@PostMapping
|
|
|
public ResponseEntity<Student> addStudent(@RequestBody Student student) {
|
|
public ResponseEntity<Student> addStudent(@RequestBody Student student) {
|
|
|
- if (student.getAddress().getId() <= 0) {
|
|
|
|
|
|
|
+ /*
|
|
|
|
|
+ // Commented out due to simplify http requests sent from angular app
|
|
|
|
|
+ if (student.getAddress().getId() <= 0 )
|
|
|
|
|
+ {
|
|
|
addressRepository.save(student.getAddress());
|
|
addressRepository.save(student.getAddress());
|
|
|
- }
|
|
|
|
|
|
|
+ }*/
|
|
|
studentRepository.save(student);
|
|
studentRepository.save(student);
|
|
|
- return new ResponseEntity<Student>(student, HttpStatus.CREATED);
|
|
|
|
|
|
|
+ return new ResponseEntity<>(student, HttpStatus.CREATED);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@DeleteMapping
|
|
@DeleteMapping
|
|
|
public ResponseEntity<Student> deleteAllStudents() {
|
|
public ResponseEntity<Student> deleteAllStudents() {
|
|
|
studentRepository.deleteAll();
|
|
studentRepository.deleteAll();
|
|
|
- return new ResponseEntity<Student>(HttpStatus.NO_CONTENT);
|
|
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@DeleteMapping("/{id}")
|
|
@DeleteMapping("/{id}")
|
|
@@ -59,24 +60,24 @@ public class StudentRESTController {
|
|
|
Student student = studentRepository.findById(id);
|
|
Student student = studentRepository.findById(id);
|
|
|
if (student == null) {
|
|
if (student == null) {
|
|
|
System.out.println("Student not found!");
|
|
System.out.println("Student not found!");
|
|
|
- return new ResponseEntity<Student>(HttpStatus.NOT_FOUND);
|
|
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
}
|
|
}
|
|
|
studentRepository.deleteById(id);
|
|
studentRepository.deleteById(id);
|
|
|
- return new ResponseEntity<Student>(HttpStatus.NO_CONTENT);
|
|
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PutMapping
|
|
@PutMapping
|
|
|
public ResponseEntity<Student> updateAllStudents(@RequestBody List<Student> students) {
|
|
public ResponseEntity<Student> updateAllStudents(@RequestBody List<Student> students) {
|
|
|
studentRepository.deleteAll();
|
|
studentRepository.deleteAll();
|
|
|
studentRepository.saveAll(students);
|
|
studentRepository.saveAll(students);
|
|
|
- return new ResponseEntity<Student>(HttpStatus.NO_CONTENT);
|
|
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PutMapping("/{id}")
|
|
@PutMapping("/{id}")
|
|
|
public ResponseEntity<Student> updateStudent(@RequestBody Student student, @PathVariable("id") long id) {
|
|
public ResponseEntity<Student> updateStudent(@RequestBody Student student, @PathVariable("id") long id) {
|
|
|
student.setId(id);
|
|
student.setId(id);
|
|
|
studentRepository.save(student);
|
|
studentRepository.save(student);
|
|
|
- return new ResponseEntity<Student>(HttpStatus.NO_CONTENT);
|
|
|
|
|
|
|
+ return new ResponseEntity<>(student,HttpStatus.NO_CONTENT);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PatchMapping("/{id}")
|
|
@PatchMapping("/{id}")
|
|
@@ -84,10 +85,10 @@ public class StudentRESTController {
|
|
|
Student student = studentRepository.findById(id);
|
|
Student student = studentRepository.findById(id);
|
|
|
if (student == null) {
|
|
if (student == null) {
|
|
|
System.out.println("Student not found!");
|
|
System.out.println("Student not found!");
|
|
|
- return new ResponseEntity<Student>(HttpStatus.NOT_FOUND);
|
|
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
}
|
|
}
|
|
|
partialUpdate(student, updates);
|
|
partialUpdate(student, updates);
|
|
|
- return new ResponseEntity<Student>(HttpStatus.NO_CONTENT);
|
|
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void partialUpdate(Student student, Map<String, Object> updates) {
|
|
private void partialUpdate(Student student, Map<String, Object> updates) {
|
|
@@ -105,4 +106,5 @@ public class StudentRESTController {
|
|
|
}
|
|
}
|
|
|
studentRepository.save(student);
|
|
studentRepository.save(student);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|