Modif class Membre : modification de la jointure

Modif Controller Membre : ajout de methode delete et add
This commit is contained in:
Hedi 2021-12-22 10:40:06 +01:00
parent b3c5c08a9d
commit af3700e99a
5 changed files with 180 additions and 171 deletions

View file

@ -9,6 +9,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.persistence.EntityNotFoundException;
import java.util.List;
import java.util.Optional;
@ -23,7 +24,7 @@ public class MembreController {
@Autowired
private TeamRepository teamRepo;
@GetMapping("/")
@RequestMapping("/")
@ResponseBody
public String home()
{
@ -72,11 +73,36 @@ public class MembreController {
return ResponseEntity.status(HttpStatus.OK).body(membre);
}
@GetMapping(value = "/membres/delete/{id}")
public void deleteMembreId(@PathVariable("id") Integer id) {
// @GetMapping(value = "/membres/delete/{id}")
// public void deleteMembreId(@PathVariable("id") Integer id) {
//
// membreRepo.deleteById(id);
//
// }
membreRepo.deleteById(id);
@DeleteMapping(value = "/delete/{id}")
public ResponseEntity<?> deleteMembre(@PathVariable int id){
try {
membreRepo.delete(membreRepo.getById(id));
//membreRepo.deleteById(id);
return ResponseEntity.status(HttpStatus.OK).body("Membre effacée !");
} catch (EntityNotFoundException e) {
return ResponseEntity.status(HttpStatus.OK).body("Membre introuvable !");
}
}
@PostMapping(value="/add", produces="application/json", consumes="application/json")
public ResponseEntity<?> addMembre(@RequestBody Membre membre){
Membre resultMembre = null;
try {
resultMembre = membreRepo.saveAndFlush(membre);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
return ResponseEntity.status(HttpStatus.CREATED).body(resultMembre);
}
@GetMapping(value = "/team/{id}")

View file

@ -1,5 +1,8 @@
package fr.organizee.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
import java.time.LocalDate;
@ -17,9 +20,14 @@ public class Membre {
private String isAdmin;
private String couleur;
private String smiley;
@ManyToOne
// @ManyToOne
// @JoinColumn(name="TEAM_ID")
// @JsonIgnore
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name="TEAM_ID")
private Team team;
@JsonIgnoreProperties("membre")
private Team team;
public Membre() {
}
@ -33,7 +41,7 @@ public class Membre {
this.isAdmin = isAdmin;
this.couleur = couleur;
this.smiley = smiley;
/* this.team = team;*/
this.team = team;
}
public int getId() {