diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductController.java index 5d81db83a00577a97135789baf8d1f3b8e6880b9..be2ceaa5da6d94b5aad6f104beaa9cbe415bcb52 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductController.java @@ -55,6 +55,7 @@ public class ProductController { return ResponseEntity.badRequest().build(); product.setCategory(category); + category.addProduct(product); if(productService.getProductById(productRequest.ean()).isPresent()) @@ -77,8 +78,13 @@ public class ProductController { } if(productRequest.allergies() != null){ - productRequest.allergies().forEach(allergyName-> product.addAllergy(allergyService.getAllergyByName(allergyName).get())); - } + productRequest.allergies().forEach(allergyName-> { + allergyService.getAllergyByName(allergyName).ifPresent(allergy -> { + product.addAllergy(allergy); + allergy.addProduct(product); + }); + }); + } productService.saveProduct(product); return ResponseEntity.ok(product); @@ -141,6 +147,19 @@ public class ProductController { product.get().setAmount(Double.parseDouble(volumeUnit.get().get(0))); } + product.get().getAllergies().stream().filter(allergy -> allergy.getProducts().contains(product.get())) + .forEach(allergy -> allergy.getProducts().remove(product.get())); + product.get().getAllergies().clear(); + + + if(productRequest.allergies() != null){ + productRequest.allergies().forEach(allergyName-> { + allergyService.getAllergyByName(allergyName).ifPresent(allergy -> { + product.get().addAllergy(allergy); + allergy.addProduct(product.get()); + }); + }); + } productService.updateProduct(product.get()); return ResponseEntity.ok(product.get()); diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Allergy.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Allergy.java index dc776c200390eef2a571df2e83054c97b47d08e2..b3b87d9a84a1006c6c974436cc3d86e944c7335f 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Allergy.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Allergy.java @@ -61,4 +61,26 @@ public class Allergy{ users.add(tempuser); } + + public void addProduct(Product product) { + if (products == null) + products = new ArrayList<>(); + + if(!products.contains(product)) + products.add(product); + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Allergy allergy)) return false; + return getName().equals(allergy.getName()); + } + + @Override + public int hashCode() { + return getName().hashCode(); + } + } \ No newline at end of file diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java index 2e2b8f622c7864274b557f82ca28cab2f0e65948..d460d91555e64c54c0cc100f747e19838da96fc2 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java @@ -25,4 +25,11 @@ public class Category { @OneToMany(mappedBy = "category") @JsonIgnore private List<Product> products; + + public void addProduct(Product product) { + if (products == null) + products = List.of(product); + else if(!products.contains(product)) + products.add(product); + } } \ No newline at end of file diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java index 71f295cd4627c3a5f7882d78920f050e395fe266..45efc15f82d56754a013e126571ed087eb37658e 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java @@ -120,6 +120,21 @@ public class Product{ if (allergies == null){ allergies = new ArrayList<>(); } - allergies.add(allergy); + if(!allergies.contains(allergy)) + allergies.add(allergy); + } + + + @Override + public boolean equals(Object o){ + if(o instanceof Product p){ + return p.getEan() == this.getEan(); + } + return false; + } + + @Override + public int hashCode(){ + return (int) this.getEan(); } } \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b13fdb2403ba71eb04d48c3180a862f33be4f843..0b2be5379b7e68fd12834a5ebbf3e40f67a27eab 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -5,7 +5,7 @@ spring.datasource.username=smartmat_user spring.datasource.password=Eyhs1OJxyZC56NQCrV7yAolEk9AkLAsC # jpa settings -spring.jpa.show-sql=true +spring.jpa.show-sql=false ## Hibernate Properties # The SQL dialect makes Hibernate generate better SQL for the chosen database