|
|
@@ -1,17 +1,42 @@
|
|
|
package pl.dmcs.eldarmuk.backend_springboot.controller;
|
|
|
|
|
|
-import org.springframework.stereotype.Controller;
|
|
|
-import org.springframework.ui.Model;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+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.Student;
|
|
|
+import pl.dmcs.eldarmuk.backend_springboot.repository.StudentRepository;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
|
|
|
-@Controller
|
|
|
+@RestController
|
|
|
+@CrossOrigin(origins = "http://localhost:4200")
|
|
|
+@RequestMapping("/students")
|
|
|
public class StudentController {
|
|
|
+ private StudentRepository studentRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public StudentController(StudentRepository studentRepository) {
|
|
|
+ this.studentRepository = studentRepository;
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping
|
|
|
+ public List<Student> findAllStudents() {
|
|
|
+ return studentRepository.findAll();
|
|
|
+ }
|
|
|
|
|
|
- @GetMapping("/student")
|
|
|
- public String getStudent(Model model) {
|
|
|
- Student student = new Student(1L, "John", "Doe", "john.doe@example.com");
|
|
|
- model.addAttribute("student", student);
|
|
|
- return "student";
|
|
|
+ @DeleteMapping("/{id}")
|
|
|
+ public ResponseEntity<?> deleteStudent(@PathVariable("id") long id) {
|
|
|
+ try {
|
|
|
+ Student student = studentRepository.findById(id);
|
|
|
+ if (student == null) {
|
|
|
+ return ResponseEntity.notFound().build();
|
|
|
+ }
|
|
|
+ studentRepository.deleteById(id);
|
|
|
+ return ResponseEntity.ok(student);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return ResponseEntity.status(500).body("Error deleting student: " + e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
}
|