Skip to content
Snippets Groups Projects
Commit dedc1852 authored by Fatima Ezzahra Majidi's avatar Fatima Ezzahra Majidi
Browse files

Mise à jour des PUT,POST,DELETE en fonction des relations entre les composants

parent c375bc94
Branches
No related tags found
1 merge request!5Mise à jour des PUT,POST,DELETE en fonction des relations entre les composants
Showing
with 140 additions and 12 deletions
......@@ -89,6 +89,8 @@
</dependency>
</dependencies>
<build>
......
......@@ -76,10 +76,17 @@ public class CandidacyController {
@GetMapping("/{id}")
public ResponseEntity<Candidacy> getCandidacyById(@PathVariable Long id) {
return candidacyService.getCandidacyById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
Optional<Candidacy> optionalCandidacy = candidacyService.getCandidacyById(id);
if (optionalCandidacy.isPresent()) {
Candidacy candidacy = optionalCandidacy.get();
candidacy.getStage().getId(); // Ensure Stage ID is included
return ResponseEntity.ok(candidacy);
} else {
return ResponseEntity.notFound().build();
}
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteCandidacy(@PathVariable Long id) {
if (!candidacyService.existsById(id)) {
......
......@@ -6,6 +6,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/enterprises")
......@@ -49,4 +50,21 @@ public class EnterpriseController {
enterpriseService.deleteEnterprise(id);
return ResponseEntity.noContent().build();
}
@PutMapping("/{id}")
public ResponseEntity<?> updateEnterprise(@PathVariable Long id, @RequestBody Enterprise updatedEnterprise) {
Optional<Enterprise> optionalEnterprise = enterpriseService.getEnterpriseById(id);
if (optionalEnterprise.isPresent()) {
Enterprise existingEnterprise = optionalEnterprise.get();
existingEnterprise.setName(updatedEnterprise.getName());
existingEnterprise.setAddress(updatedEnterprise.getAddress());
Enterprise savedEnterprise = enterpriseService.saveEnterprise(existingEnterprise);
return ResponseEntity.ok(savedEnterprise);
} else {
return ResponseEntity.notFound().build();
}
}
}
......@@ -49,6 +49,20 @@ public class StageController {
public ResponseEntity<List<Stage>> getAllStages() {
return ResponseEntity.ok(stageService.getAllStages());
}
@GetMapping("/{id}")
public ResponseEntity<Stage> getStageById(@PathVariable Long id) {
Optional<Stage> optionalStage = stageService.getStageById(id);
if (optionalStage.isPresent()) {
Stage stage = optionalStage.get();
stage.getEnterprise().getId(); // Ensure Enterprise ID is included
return ResponseEntity.ok(stage);
} else {
return ResponseEntity.notFound().build();
}
}
@PutMapping("/{id}")
public ResponseEntity<?> updateStage(@PathVariable Long id, @RequestBody Stage updatedStage) {
Optional<Stage> optionalStage = stageService.getStageById(id);
......@@ -72,5 +86,17 @@ public class StageController {
return ResponseEntity.notFound().build();
}
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteStage(@PathVariable Long id) {
Optional<Stage> optionalStage = stageService.getStageById(id);
if (optionalStage.isPresent()) {
stageService.deleteStage(id);
return ResponseEntity.noContent().build(); // HTTP 204 No Content (successful deletion)
} else {
return ResponseEntity.notFound().build(); // HTTP 404 Not Found if the stage doesn't exist
}
}
}
......@@ -40,10 +40,34 @@ public class StudentController {
.orElse(ResponseEntity.notFound().build());
}
// endpoint pour recherche de candidacy par student
// Endpoint to get all candidacies of a student
@GetMapping("/{id}/candidacies")
public ResponseEntity<List<Candidacy>> getStudentCandidacies(@PathVariable Long id) {
List<Candidacy> candidacies = candidacyService.getCandidaciesByStudentId(id);
return ResponseEntity.ok(candidacies);
}
// ✅ PUT - Update student by ID
@PutMapping("/{id}")
public ResponseEntity<Student> updateStudent(@PathVariable Long id, @RequestBody Student updatedStudent) {
Optional<Student> optionalStudent = studentService.getStudentById(id);
if (optionalStudent.isPresent()) {
Student existingStudent = optionalStudent.get();
existingStudent.setFirstname(updatedStudent.getFirstname());
existingStudent.setLastname(updatedStudent.getLastname());
Student savedStudent = studentService.saveStudent(existingStudent);
return ResponseEntity.ok(savedStudent);
} else {
return ResponseEntity.notFound().build();
}
}
// ✅ DELETE - Remove student by ID
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteStudent(@PathVariable Long id) {
studentService.deleteStudent(id);
return ResponseEntity.noContent().build();
}
}
......@@ -35,4 +35,27 @@ public class UnivSupervisorController {
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
// ✅ PUT - Update UnivSupervisor by ID
@PutMapping("/{id}")
public ResponseEntity<UnivSupervisor> updateUnivSupervisor(@PathVariable Long id, @RequestBody UnivSupervisor updatedSupervisor) {
Optional<UnivSupervisor> optionalSupervisor = univSupervisorService.getUnivSupervisorById(id);
if (optionalSupervisor.isPresent()) {
UnivSupervisor existingSupervisor = optionalSupervisor.get();
existingSupervisor.setName(updatedSupervisor.getName());
UnivSupervisor savedSupervisor = univSupervisorService.saveUnivSupervisor(existingSupervisor);
return ResponseEntity.ok(savedSupervisor);
} else {
return ResponseEntity.notFound().build();
}
}
// ✅ DELETE - Remove UnivSupervisor by ID
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUnivSupervisor(@PathVariable Long id) {
univSupervisorService.deleteUnivSupervisor(id);
return ResponseEntity.noContent().build();
}
}
......@@ -16,9 +16,11 @@ public class Candidacy {
private Student student;
@ManyToOne
@JoinColumn(name = "stage_id")
@JsonBackReference
private Stage stage; // suppression de @JsonIgnoreProperties pour inclure Stage in response
@JoinColumn(name = "stage_id", nullable = false)
@JsonIgnoreProperties({"candidacies", "enterprise"}) // Ensures that Candidacy includes Stage but avoids loops
private Stage stage;
// suppression de @JsonIgnoreProperties pour inclure Stage in response
@ManyToOne
@JoinColumn(name = "univ_supervisor_id")
......
package com.example.gestionstagesbackend.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonManagedReference;
......@@ -13,9 +14,10 @@ public class Enterprise {
private String address;
@OneToMany(mappedBy = "enterprise", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonManagedReference // as the parent side
//@JsonManagedReference // Ensures that stages are included in Enterprise JSON responses
private List<Stage> stages;
public Enterprise() {}
public Enterprise(String name, String address) {
......
package com.example.gestionstagesbackend.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import java.util.List;
......@@ -15,13 +16,15 @@ public class Stage {
@ManyToOne
@JoinColumn(name = "enterprise_id", nullable = false)
@JsonBackReference // Prevents infinite recursion in JSON serialization
@JsonIgnoreProperties({"stages"}) // Prevents infinite loops but keeps enterprise data in JSON
private Enterprise enterprise;
@OneToMany(mappedBy = "stage", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonManagedReference // ✅ Allows Candidacies to appear in Stage responses
//@JsonManagedReference
private List<Candidacy> candidacies;
public Stage() {}
public Stage(String name, String description, Enterprise enterprise) {
......
......@@ -3,6 +3,8 @@ package com.example.gestionstagesbackend.repositories;
import com.example.gestionstagesbackend.model.Stage;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Optional;
......@@ -12,4 +14,7 @@ public interface StageRepository extends JpaRepository<Stage, Long> {
@EntityGraph(attributePaths = {"enterprise"}) // Ensures Enterprise is fetched
Optional<Stage> findById(Long id);
@Query("SELECT s FROM Stage s LEFT JOIN FETCH s.enterprise WHERE s.id = :id")
Optional<Stage> findByIdWithEnterprise(@Param("id") Long id);
}
......@@ -16,7 +16,7 @@ public class EnterpriseService {
}
public Enterprise saveEnterprise(Enterprise enterprise) {
System.out.println("Saving enterprise: " + enterprise.getName() + ", " + enterprise.getAddress());
//System.out.println("Saving enterprise: " + enterprise.getName() + ", " + enterprise.getAddress());
// If stages exist, set their enterprise reference correctly
if (enterprise.getStages() != null && !enterprise.getStages().isEmpty()) {
......
......@@ -2,7 +2,9 @@ package com.example.gestionstagesbackend.services;
import com.example.gestionstagesbackend.model.Stage;
import com.example.gestionstagesbackend.repositories.StageRepository;
import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
......@@ -24,6 +26,12 @@ public class StageService {
}
public Optional<Stage> getStageById(Long id) {
return stageRepository.findById(id);
return stageRepository.findByIdWithEnterprise(id);
}
public void deleteStage(Long id) {
stageRepository.deleteById(id);
}
}
......@@ -26,4 +26,12 @@ public class StudentService {
public Optional<Student> getStudentById(Long id) {
return studentRepository.findById(id);
}
public void deleteStudent(Long id) {
if (studentRepository.existsById(id)) {
studentRepository.deleteById(id);
} else {
throw new IllegalArgumentException("Student with ID " + id + " does not exist.");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment