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

Relations entre composants

parent 9fbb9d85
No related branches found
No related tags found
1 merge request!4Relations entre composants
Showing
with 101 additions and 106 deletions
......@@ -7,16 +7,13 @@ import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable()) // ✅ Disable CSRF protection
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/**").permitAll() // Allow all API calls
.anyRequest().authenticated() // Require authentication for other requests
)
.csrf(csrf -> csrf.disable()); // Disable CSRF for testing
.anyRequest().permitAll() // ✅ Allow all requests (for testing)
);
return http.build();
}
}
package com.example.gestionstagesbackend.controllers;
import com.example.gestionstagesbackend.model.Enterprise;
import com.example.gestionstagesbackend.services.EnterpriseService;
import org.springframework.http.ResponseEntity;
......@@ -10,7 +9,7 @@ import java.util.List;
@RestController
@RequestMapping("/api/enterprises")
@CrossOrigin(origins = "*") // Allow requests from all origins (for testing)
@CrossOrigin(origins = "*")
public class EnterpriseController {
private final EnterpriseService enterpriseService;
......@@ -21,20 +20,33 @@ public class EnterpriseController {
@PostMapping
public ResponseEntity<Enterprise> createEnterprise(@RequestBody Enterprise enterprise) {
System.out.println("Received POST request: " + enterprise.getName() + ", " + enterprise.getAddress());
Enterprise savedEnterprise = enterpriseService.saveEnterprise(enterprise);
System.out.println("Response sent with Enterprise ID: " + savedEnterprise.getId());
return ResponseEntity.ok(savedEnterprise);
}
@GetMapping
public ResponseEntity<List<Enterprise>> getAllEnterprises() {
System.out.println("🔵 Fetching all enterprises...");
return ResponseEntity.ok(enterpriseService.getAllEnterprises());
}
@GetMapping("/{id}")
public ResponseEntity<Enterprise> getEnterpriseById(@PathVariable Long id) {
System.out.println("Fetching enterprise with ID: " + id);
return enterpriseService.getEnterpriseById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteEnterprise(@PathVariable Long id) {
System.out.println("Deleting enterprise with ID: " + id);
enterpriseService.deleteEnterprise(id);
return ResponseEntity.noContent().build();
}
}
......@@ -36,9 +36,15 @@ public class StageController {
stage.setEnterprise(enterprise.get());
Stage savedStage = stageService.saveStage(stage);
return ResponseEntity.ok(savedStage);
// Fetch the saved stage with enterprise details included
Optional<Stage> fullStage = stageService.getStageById(savedStage.getId());
return fullStage.map(ResponseEntity::ok).orElse(ResponseEntity.ok(savedStage));
}
@GetMapping
public ResponseEntity<List<Stage>> getAllStages() {
return ResponseEntity.ok(stageService.getAllStages());
......
package com.example.gestionstagesbackend.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
......@@ -11,17 +12,17 @@ public class Candidacy {
@ManyToOne
@JoinColumn(name = "student_id")
@JsonIgnoreProperties({"candidacies"}) // Prevents infinite recursion when serializing
@JsonIgnoreProperties({"candidacies"}) // Prevents recursion, but keeps other data
private Student student;
@ManyToOne
@JoinColumn(name = "stage_id")
@JsonIgnoreProperties({"candidacies"})
private Stage stage;
@JsonBackReference
private Stage stage; // suppression de @JsonIgnoreProperties pour inclure Stage in response
@ManyToOne
@JoinColumn(name = "univ_supervisor_id")
@JsonIgnoreProperties({"supervisedStages"})
@JsonIgnoreProperties({"supervisedStages"}) // Prevents recursion
private UnivSupervisor univSupervisor;
public Candidacy() {}
......@@ -32,35 +33,15 @@ public class Candidacy {
this.univSupervisor = univSupervisor;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Student getStudent() {
return student;
}
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public void setStudent(Student student) {
this.student = student;
}
public Student getStudent() { return student; }
public void setStudent(Student student) { this.student = student; }
public Stage getStage() {
return stage;
}
public Stage getStage() { return stage; }
public void setStage(Stage stage) { this.stage = stage; }
public void setStage(Stage stage) {
this.stage = stage;
}
public UnivSupervisor getUnivSupervisor() {
return univSupervisor;
}
public void setUnivSupervisor(UnivSupervisor univSupervisor) {
this.univSupervisor = univSupervisor;
}
public UnivSupervisor getUnivSupervisor() { return univSupervisor; }
public void setUnivSupervisor(UnivSupervisor univSupervisor) { this.univSupervisor = univSupervisor; }
}
package com.example.gestionstagesbackend.model;
import jakarta.persistence.*;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@Entity
@Table(name = "enterprise")
public class Enterprise {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String address;
@OneToMany(mappedBy = "enterprise", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "enterprise", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonManagedReference // as the parent side
private List<Stage> stages;
public Enterprise() {}
......@@ -26,27 +23,13 @@ public class Enterprise {
this.address = address;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() { return id; }
public String getName() { return name; }
public String getAddress() { return address; }
public List<Stage> getStages() { return stages; }
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void setId(Long id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAddress(String address) { this.address = address; }
public void setStages(List<Stage> stages) { this.stages = stages; }
}
package com.example.gestionstagesbackend.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import java.util.List;
@Entity
......@@ -14,49 +15,41 @@ public class Stage {
@ManyToOne
@JoinColumn(name = "enterprise_id", nullable = false)
@JsonBackReference // Prevents infinite recursion in JSON serialization
private Enterprise enterprise;
@OneToMany(mappedBy = "stage", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "stage", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonManagedReference // ✅ Allows Candidacies to appear in Stage responses
private List<Candidacy> candidacies;
public Stage() {}
public Stage(Long id, String name, String description, Enterprise enterprise) {
this.id = id;
public Stage(String name, String description, Enterprise enterprise) {
this.name = name;
this.description = description;
this.enterprise = enterprise;
}
public Long getId() {
return id;
}
public void setId(Long id) {
public Stage(Long id, String name, String description, Enterprise enterprise, List<Candidacy> candidacies) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
this.description = description;
this.enterprise = enterprise;
this.candidacies = candidacies;
}
public String getDescription() {
return description;
}
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public void setDescription(String description) {
this.description = description;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Enterprise getEnterprise() {
return enterprise;
}
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public void setEnterprise(Enterprise enterprise) {
this.enterprise = enterprise;
}
public Enterprise getEnterprise() { return enterprise; }
public void setEnterprise(Enterprise enterprise) { this.enterprise = enterprise; }
public List<Candidacy> getCandidacies() { return candidacies; }
public void setCandidacies(List<Candidacy> candidacies) { this.candidacies = candidacies; }
}
package com.example.gestionstagesbackend.repositories;
import com.example.gestionstagesbackend.model.Enterprise;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.Optional;
public interface EnterpriseRepository extends JpaRepository<Enterprise, Long> {
@Query("SELECT e FROM Enterprise e LEFT JOIN FETCH e.stages WHERE e.id = :id")
Optional<Enterprise> findByIdWithStages(@Param("id") Long id);
}
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.stereotype.Repository;
import java.util.Optional;
@Repository
public interface StageRepository extends JpaRepository<Stage, Long> {
@EntityGraph(attributePaths = {"enterprise"}) // Ensures Enterprise is fetched
Optional<Stage> findById(Long id);
}
package com.example.gestionstagesbackend.services;
import com.example.gestionstagesbackend.model.Enterprise;
import com.example.gestionstagesbackend.repositories.EnterpriseRepository;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class EnterpriseService {
......@@ -19,15 +16,31 @@ public class EnterpriseService {
}
public Enterprise saveEnterprise(Enterprise enterprise) {
return enterpriseRepository.save(enterprise);
}
System.out.println("Saving enterprise: " + enterprise.getName() + ", " + enterprise.getAddress());
public Optional<Enterprise> getEnterpriseById(Long id) {
return enterpriseRepository.findById(id);
// If stages exist, set their enterprise reference correctly
if (enterprise.getStages() != null && !enterprise.getStages().isEmpty()) {
enterprise.getStages().forEach(stage -> stage.setEnterprise(enterprise));
} else {
enterprise.setStages(null); // Avoid redundant or circular references
}
Enterprise savedEnterprise = enterpriseRepository.save(enterprise);
System.out.println("Saved Enterprise ID: " + savedEnterprise.getId());
return savedEnterprise;
}
public List<Enterprise> getAllEnterprises() {
return enterpriseRepository.findAll();
}
public Optional<Enterprise> getEnterpriseById(Long id) {
return enterpriseRepository.findByIdWithStages(id);
}
public void deleteEnterprise(Long id) {
System.out.println("Deleting Enterprise ID: " + id);
enterpriseRepository.deleteById(id);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment