Skip to content
Snippets Groups Projects

Lier les services l'un à l'autre

Merged Fatima Ezzahra Majidi requested to merge master into main
17 files
+ 382
189
Compare changes
  • Side-by-side
  • Inline
Files
17
package com.example.gestionstagesbackend.controllers;
import com.example.gestionstagesbackend.model.Candidacy;
import com.example.gestionstagesbackend.model.Student;
import com.example.gestionstagesbackend.model.Stage;
import com.example.gestionstagesbackend.model.UnivSupervisor;
import com.example.gestionstagesbackend.services.CandidacyService;
import com.example.gestionstagesbackend.services.StudentService;
import com.example.gestionstagesbackend.services.StageService;
import com.example.gestionstagesbackend.services.UnivSupervisorService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -11,30 +16,68 @@ import java.util.Optional;
@RestController
@RequestMapping("/api/candidacies")
@CrossOrigin(origins = "*")
public class CandidacyController {
private final CandidacyService candidacyService;
private final StudentService studentService;
private final StageService stageService;
private final UnivSupervisorService univSupervisorService;
public CandidacyController(CandidacyService candidacyService) {
public CandidacyController(CandidacyService candidacyService, StudentService studentService,
StageService stageService, UnivSupervisorService univSupervisorService) {
this.candidacyService = candidacyService;
this.studentService = studentService;
this.stageService = stageService;
this.univSupervisorService = univSupervisorService;
}
@GetMapping
public List<Candidacy> getAllCandidacies() {
return candidacyService.getAllCandidacies();
}
@PostMapping
public ResponseEntity<?> createCandidacy(@RequestBody Candidacy candidacy) {
if (candidacy.getStudent() == null || candidacy.getStudent().getId() == null) {
return ResponseEntity.badRequest().body("Student ID is required");
}
if (candidacy.getStage() == null || candidacy.getStage().getId() == null) {
return ResponseEntity.badRequest().body("Stage ID is required");
}
if (candidacy.getUnivSupervisor() != null && candidacy.getUnivSupervisor().getId() == null) {
return ResponseEntity.badRequest().body("UnivSupervisor ID is invalid");
}
@GetMapping("/{id}")
public Optional<Candidacy> getCandidacyById(@PathVariable Long id) {
return candidacyService.getCandidacyById(id);
Optional<Student> student = studentService.getStudentById(candidacy.getStudent().getId());
Optional<Stage> stage = stageService.getStageById(candidacy.getStage().getId());
Optional<UnivSupervisor> univSupervisor = Optional.empty();
if (candidacy.getUnivSupervisor() != null) {
univSupervisor = univSupervisorService.getUnivSupervisorById(candidacy.getUnivSupervisor().getId());
if (univSupervisor.isEmpty()) {
return ResponseEntity.badRequest().body("UnivSupervisor not found");
}
}
if (student.isEmpty()) {
return ResponseEntity.badRequest().body("Student not found");
}
if (stage.isEmpty()) {
return ResponseEntity.badRequest().body("Stage not found");
}
candidacy.setStudent(student.get());
candidacy.setStage(stage.get());
univSupervisor.ifPresent(candidacy::setUnivSupervisor);
Candidacy savedCandidacy = candidacyService.saveCandidacy(candidacy);
return ResponseEntity.ok(savedCandidacy);
}
@PostMapping
public Candidacy saveCandidacy(@RequestBody Candidacy candidacy) {
return candidacyService.saveCandidacy(candidacy);
@GetMapping
public ResponseEntity<List<Candidacy>> getAllCandidacies() {
return ResponseEntity.ok(candidacyService.getAllCandidacies());
}
@DeleteMapping("/{id}")
public void deleteCandidacy(@PathVariable Long id) {
candidacyService.deleteCandidacy(id);
@GetMapping("/{id}")
public ResponseEntity<Candidacy> getCandidacyById(@PathVariable Long id) {
return candidacyService.getCandidacyById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
}
Loading