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

@ -17,6 +17,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
}
test {

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){
//
// preferenceRepository.deleteById(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());
// }
//
// }
}

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;
}
@ -151,4 +163,17 @@ public class Restaurant {
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;

View File

@ -1,6 +1,557 @@
Nouvelle Requête POSTMAN:
20/01/2022
GET localhost:8080/restaurantbytype/1
{
"info": {
"_postman_id": "5edbac98-7561-4ccc-b517-87c01aef2a41",
"name": "Simpleat",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Personne",
"item": [
{
"name": "Accueil",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "formdata",
"formdata": []
},
"url": {
"raw": "localhost:8080/",
"host": [
"localhost"
],
"port": "8080",
"path": [
""
]
}
},
"response": []
},
{
"name": "affichPersonnes",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "localhost:8080/users",
"host": [
"localhost"
],
"port": "8080",
"path": [
"users"
]
}
},
"response": []
},
{
"name": "AffichUnePersonne",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "localhost:8080/user/2",
"host": [
"localhost"
],
"port": "8080",
"path": [
"user",
"2"
]
}
},
"response": []
},
{
"name": "AjoutPersonne",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"nom\": \"nouvel\",\r\n \"prenom\": \"ajout\",\r\n \"email\": \"role@ajout.fr\",\r\n \"password\": \"root\",\r\n \"roles\": [\r\n {\r\n \"id\": 2\r\n }\r\n ]\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/add-user",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"add-user"
]
}
},
"response": []
},
{
"name": "UpdatePersonne",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"id\": 2,\r\n \"nom\": \"salut\",\r\n \"prenom\": \"enleve\",\r\n \"email\": \"nouvel@ajout.fr\",\r\n \"password\": \"root\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/update-user/2",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"update-user",
"2"
]
}
},
"response": []
},
{
"name": "DeletePersonne",
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "localhost:8080/delete-user/1",
"host": [
"localhost"
],
"port": "8080",
"path": [
"delete-user",
"1"
]
}
},
"response": []
}
]
},
{
"name": "Preference",
"item": [
{
"name": "AffichPreference",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "localhost:8080/preference/5/5",
"host": [
"localhost"
],
"port": "8080",
"path": [
"preference",
"5",
"5"
]
}
},
"response": []
},
{
"name": "AffichPreferences",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "localhost:8080/preferences",
"host": [
"localhost"
],
"port": "8080",
"path": [
"preferences"
]
}
},
"response": []
},
{
"name": "Ajout preferences",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"preferencePK\": {\r\n \"personne\": {\r\n \"id\": 10\r\n },\r\n \"restau\": {\r\n \"id\": 2\r\n }\r\n },\r\n \"note\": 5,\r\n \"favori\": true\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "localhost:8080/add-preference",
"host": [
"localhost"
],
"port": "8080",
"path": [
"add-preference"
]
}
},
"response": []
},
{
"name": "Delete preferences",
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "localhost:8080/delete-preference/:idrestau/:iduser",
"host": [
"localhost"
],
"port": "8080",
"path": [
"delete-preference",
":idrestau",
":iduser"
],
"variable": [
{
"key": "idrestau",
"value": "3"
},
{
"key": "iduser",
"value": "1"
}
]
}
},
"response": []
},
{
"name": "Del prefByRestau[OUT]",
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "localhost:8080/delete-pref-byrestau/3",
"host": [
"localhost"
],
"port": "8080",
"path": [
"delete-pref-byrestau",
"3"
]
}
},
"response": []
}
]
},
{
"name": "Restaurant",
"item": [
{
"name": "AffichRestaurants",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "localhost:8080/restaurants",
"host": [
"localhost"
],
"port": "8080",
"path": [
"restaurants"
]
}
},
"response": []
},
{
"name": "AfficheUnRestaurant",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "localhost:8080/restaurant/2",
"host": [
"localhost"
],
"port": "8080",
"path": [
"restaurant",
"2"
]
}
},
"response": []
},
{
"name": "AfficheRestoParType",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "localhost:8080/restaurantbytype/1",
"host": [
"localhost"
],
"port": "8080",
"path": [
"restaurantbytype",
"1"
]
}
},
"response": []
},
{
"name": "AjoutRestaurant",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": " {\r\n \"nom\": \"gevaudan\",\r\n \"adresse\": \"Montreil\",\r\n \"latitude\": \"31.45571\",\r\n \"longitude\": \"34.43719\",\r\n \"typerestaus\": [\r\n {\r\n \"id\": 3\r\n },\r\n {\r\n \"id\": 4\r\n }\r\n ]\r\n }",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/add-restaurant",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"add-restaurant"
]
}
},
"response": []
},
{
"name": "UpdateRestaurant",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": " {\r\n \"id\": 2,\r\n \"nom\": \"suepr\",\r\n \"adresse\": \"93100 Montreuil\",\r\n \"telephone\": \"6809964349\",\r\n \"aEmporter\": false,\r\n \"surPlace\": true,\r\n \"prixMin\": 1.0,\r\n \"prixMax\": 17.0,\r\n \"accesPMR\": false,\r\n \"latitude\": \"31.45571\",\r\n \"longitude\": \"34.43719\",\r\n \"typerestaus\": []\r\n }",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/update-restaurant/2",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"update-restaurant",
"2"
]
}
},
"response": []
},
{
"name": "DeleteRestaurant",
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "localhost:8080/delete-restaurant/4",
"host": [
"localhost"
],
"port": "8080",
"path": [
"delete-restaurant",
"4"
]
}
},
"response": []
}
]
},
{
"name": "Type",
"item": [
{
"name": "AffichTypes",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "localhost:8080/types",
"host": [
"localhost"
],
"port": "8080",
"path": [
"types"
]
}
},
"response": []
},
{
"name": "Affich1Type",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "localhost:8080/type/2",
"host": [
"localhost"
],
"port": "8080",
"path": [
"type",
"2"
]
}
},
"response": []
},
{
"name": "UpdateType",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"id\": 2,\r\n \"libelle\": \"creperie\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "localhost:8080/update-type/2",
"host": [
"localhost"
],
"port": "8080",
"path": [
"update-type",
"2"
]
}
},
"response": []
},
{
"name": "AddType",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"libelle\": \"coucou\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "localhost:8080/add-type",
"host": [
"localhost"
],
"port": "8080",
"path": [
"add-type"
]
}
},
"response": []
},
{
"name": "DeleteType",
"request": {
"method": "DELETE",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "localhost:8080/delete-type/:id",
"host": [
"localhost"
],
"port": "8080",
"path": [
"delete-type",
":id"
],
"variable": [
{
"key": "id",
"value": "2"
}
]
}
},
"response": []
}
]
}
]
}

View File

@ -19,20 +19,20 @@ insert into restaurant (nom, adresse, telephone, a_emporter, sur_place, prix, ac
insert into restaurant (nom, adresse, telephone, a_emporter, sur_place, prix, accespmr, latitude, longitude) values ('Physicians Total Care, Inc.', '462 Mosinee Lane', '8521719889', false, false, 4, true, '-13.5', '48.86667');
insert into restaurant (nom, adresse, telephone, a_emporter, sur_place, prix, accespmr, latitude, longitude) values ('Triweld Industries, Inc.', '2482 Corscot Way', '7513208452', false, true, 2, true, '34.326903', '108.117456');
insert into preference (id_restau, id_personne, note, favori) values (2, 9, 1, false);
insert into preference (id_restau, id_personne, note, favori) values (3, 1, 5, false);
insert into preference (id_restau, id_personne, note, favori) values (1, 9, 3, true);
insert into preference (id_restau, id_personne, note, favori) values (4, 6, 2, false);
insert into preference (id_restau, id_personne, note, favori) values (5, 5, 5, false);
insert into preference (id_restau, id_personne, note, favori) values (1, 10, 1, false);
insert into preference (id_restau, id_personne, note, favori) values (5, 3, 5, false);
insert into preference (id_restau, id_personne, note, favori) values (1, 7, 1, true);
insert into preference (id_restau, id_personne, note, favori) values (4, 2, 1, false);
insert into preference (id_restau, id_personne, note, favori) values (1, 3, 5, false);
insert into preference (id_restau, id_personne, note, favori) values (3, 10, 4, false);
insert into preference (id_restau, id_personne, note, favori) values (3, 3, 4, true);
insert into preference (id_restau, id_personne, note, favori) values (2, 3, 3, false);
insert into preference (id_restau, id_personne, note, favori) values (2, 8, 3, false);
insert into preference (id_restau, id_personne, note, favoris) values (2, 9, 1, false);
insert into preference (id_restau, id_personne, note, favoris) values (3, 1, 5, false);
insert into preference (id_restau, id_personne, note, favoris) values (1, 9, 3, true);
insert into preference (id_restau, id_personne, note, favoris) values (4, 6, 2, false);
insert into preference (id_restau, id_personne, note, favoris) values (5, 5, 5, false);
insert into preference (id_restau, id_personne, note, favoris) values (1, 10, 1, false);
insert into preference (id_restau, id_personne, note, favoris) values (5, 3, 5, false);
insert into preference (id_restau, id_personne, note, favoris) values (1, 7, 1, true);
insert into preference (id_restau, id_personne, note, favoris) values (4, 2, 1, false);
insert into preference (id_restau, id_personne, note, favoris) values (1, 3, 5, false);
insert into preference (id_restau, id_personne, note, favoris) values (3, 10, 4, false);
insert into preference (id_restau, id_personne, note, favoris) values (3, 3, 4, true);
insert into preference (id_restau, id_personne, note, favoris) values (2, 3, 3, false);
insert into preference (id_restau, id_personne, note, favoris) values (2, 8, 3, false);
insert into type (id_type, libelle) values (1, 'Kebab');
insert into type (id_type, libelle) values (2, 'Supermarché');