suppr restau+pref=OK

This commit is contained in:
Thomas Cardon 2022-01-21 18:28:54 +01:00
parent d06c06b595
commit 5a64f29586
9 changed files with 700 additions and 54 deletions

View file

@ -11,7 +11,6 @@ import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@ -61,11 +60,21 @@ public class PreferenceController {
// return ResponseEntity.status(HttpStatus.OK).body(preferenceRepository.save(personne));
// }
//
// @DeleteMapping(value = "/delete-restaurant/{id}")
// public void suppressionPerso(@PathVariable int id){
@DeleteMapping("/delete-preference/{idrestau}/{iduser}")
public void deletePreferenceById(@PathVariable int iduser, @PathVariable int idrestau ){
PreferencePK id = new PreferencePK(personneRepository.getById(iduser) ,restaurantRepository.getById(idrestau));
preferenceRepository.deleteById(id);
}
// @DeleteMapping("/delete-pref-byrestau/{idrestau}")
// public void deletePreferenceByRestau(@PathVariable int idrestau ){
// List<Personne> list = personneRepository.findAll();
// for (int i = 0; i < list.size(); i++) {
// if(findPreferenceById(list.get(i).getId(),idrestau).isEmpty() == false) {
// preferenceRepository.deleteById(new PreferencePK(list.get(i),new Restaurant(idrestau)));
// }
// //System.out.println(findPreferenceById(list.get(i).getId(),idrestau).isEmpty());
// }
//
// preferenceRepository.deleteById(id);
// }
}

View file

@ -4,6 +4,7 @@ package fr.cardon.simpleat.model;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@ -12,7 +13,9 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@ -24,6 +27,7 @@ public class Personne {
private String email;
private String password;
private Collection<Role> roles = new ArrayList<Role>();
private Collection<Preference> preference = new ArrayList<Preference>();
public Personne() {
@ -32,6 +36,12 @@ public class Personne {
}
public Personne(int id) {
super();
this.id = id;
}
public Personne(String nom, String prenom, String email, String password) {
super();
this.nom = nom;
@ -107,4 +117,16 @@ public class Personne {
this.roles = roles;
}
@OneToMany(mappedBy = "personne", cascade = CascadeType.REMOVE)
@JsonIgnore
public Collection<Preference> getPreference() {
return preference;
}
public void setPreference(Collection<Preference> preference) {
this.preference = preference;
}
}

View file

@ -1,13 +1,20 @@
package fr.cardon.simpleat.model;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@IdClass(PreferencePK.class)
public class Preference {
private PreferencePK preferencePK;
private Personne personne;
private Restaurant restau;
private int note;
private boolean favoris;
@ -19,36 +26,79 @@ public class Preference {
}
public Preference(PreferencePK preferencePK, int note, boolean favoris) {
public Preference(Personne personne, Restaurant restau) {
super();
this.preferencePK = preferencePK;
this.personne = personne;
this.restau = restau;
}
public Preference(Personne personne, Restaurant restau, int note, boolean favoris) {
super();
this.personne = personne;
this.restau = restau;
this.note = note;
this.favoris = favoris;
}
@EmbeddedId
public PreferencePK getPreferencePK() {
return preferencePK;
@Id
@ManyToOne
@JoinColumn(name="id_personne" )
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public Personne getPersonne() {
return personne;
}
public void setPreferencePK(PreferencePK preferencePK) {
this.preferencePK = preferencePK;
public void setPersonne(Personne personne) {
this.personne = personne;
}
@Id
@ManyToOne
@JoinColumn(name="id_restau" )
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public Restaurant getRestau() {
return restau;
}
public void setRestau(Restaurant restau) {
this.restau = restau;
}
@Column(nullable = true)
public int getNote() {
return note;
}
public void setNote(int note) {
this.note = note;
}
@Column(nullable = true)
public boolean isFavori() {
public boolean isFavoris() {
return favoris;
}
public void setFavori(boolean favoris) {
public void setFavoris(boolean favoris) {
this.favoris = favoris;
}
}

View file

@ -3,16 +3,10 @@ package fr.cardon.simpleat.model;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Embeddable
public class PreferencePK implements Serializable {
/**
@ -36,9 +30,6 @@ public class PreferencePK implements Serializable {
}
@ManyToOne
@JoinColumn(name="id_personne", referencedColumnName ="id_personne" )
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public Personne getPersonne() {
return personne;
}
@ -48,10 +39,6 @@ public class PreferencePK implements Serializable {
this.personne = personne;
}
@ManyToOne // TODO mappedBy preferences dans restaurant
@JoinColumn(name="id_restau",referencedColumnName ="id_restau" )
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public Restaurant getRestau() {
return restau;
}

View file

@ -3,6 +3,7 @@ package fr.cardon.simpleat.model;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@ -11,10 +12,9 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@ -32,6 +32,7 @@ public class Restaurant {
private String longitude;
private String website;
private Collection<TypeRestau> typerestaus = new ArrayList<TypeRestau>();
private Collection<Preference> preference = new ArrayList<Preference>();
//TODO @OneToMany relier avec une collec de preferences
@ -40,6 +41,17 @@ public class Restaurant {
// TODO Auto-generated constructor stub
}
public Restaurant(int id) {
super();
this.id = id;
}
public Restaurant(String nom, String adresse, String telephone, boolean aEmporter, boolean surPlace, int prix,
boolean accesPMR, String latitude, String longitude, String website, Collection<TypeRestau> typerestaus) {
super();
@ -142,7 +154,7 @@ public class Restaurant {
@JoinTable(name="type_restau",
joinColumns = @JoinColumn(name = "id_restau"/*classe en cours*/,referencedColumnName = "id_restau" /*classe reliée*/) ,
inverseJoinColumns = @JoinColumn(name = "id_type",referencedColumnName = "id_type"))
@JsonBackReference("typerestaus")
@JsonIgnore
public Collection<TypeRestau> getTyperestaus() {
return typerestaus;
}
@ -150,5 +162,18 @@ public class Restaurant {
public void setTyperestaus(Collection<TypeRestau> typerestaus) {
this.typerestaus = typerestaus;
}
@OneToMany(mappedBy = "restau", cascade = CascadeType.REMOVE)
@JsonIgnore
public Collection<Preference> getPreference() {
return preference;
}
public void setPreference(Collection<Preference> preference) {
this.preference = preference;
}
}

View file

@ -2,6 +2,7 @@ package fr.cardon.simpleat.model;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;