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 60cb1e88f0c17d219efb56c5c00c9151b4832ff8..abda8869cf2dbbd2246bebf9c0d61ed969455237 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 @@ -2,12 +2,14 @@ package ntnu.idatt2016.v233.SmartMat.controller.product; import lombok.AllArgsConstructor; import ntnu.idatt2016.v233.SmartMat.dto.request.ProductRequest; +import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy; import ntnu.idatt2016.v233.SmartMat.entity.product.Category; import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.service.AllergyService; import ntnu.idatt2016.v233.SmartMat.service.product.CategoryService; import ntnu.idatt2016.v233.SmartMat.service.product.ProductService; import ntnu.idatt2016.v233.SmartMat.util.CategoryUtil; +import ntnu.idatt2016.v233.SmartMat.util.ProductUtil; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -89,6 +91,26 @@ public class ProductController { }); }); } + boolean vegan = CategoryUtil.isVegan(product.getName(),product.getDescription()); + if(!vegan){ + Allergy allergy = allergyService.getAllergyByName("Ikke vegansk").get(); + product.addAllergy(allergy); + allergy.addProduct(product); + allergy = allergyService.getAllergyByName("Ikke vegetariansk").get(); + product.addAllergy(allergy); + allergy.addProduct(product); + } + boolean vegetarian = CategoryUtil.isVegetarian(product.getName(),product.getDescription(),vegan); + if(!vegetarian){ + Allergy allergy = allergyService.getAllergyByName("Ikke vegetariansk").get(); + product.addAllergy(allergy); + allergy.addProduct(product); + } + if(!CategoryUtil.isHalal(product.getName(),product.getDescription(),vegetarian)){ + Allergy allergy = allergyService.getAllergyByName("Haram").get() ; + product.addAllergy(allergy); + allergy.addProduct(product); + } productService.saveProduct(product); return ResponseEntity.ok(product); diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductControllerTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductControllerTest.java index bbd03eb12bdddbc1d8ab558d5812e2b8965c5f9c..1e6985a7b6f05d55f872142fddee575a3ff9c412 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductControllerTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/product/ProductControllerTest.java @@ -1,8 +1,10 @@ package ntnu.idatt2016.v233.SmartMat.controller.product; import ntnu.idatt2016.v233.SmartMat.dto.request.ProductRequest; +import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy; import ntnu.idatt2016.v233.SmartMat.entity.product.Category; import ntnu.idatt2016.v233.SmartMat.entity.product.Product; +import ntnu.idatt2016.v233.SmartMat.service.AllergyService; import ntnu.idatt2016.v233.SmartMat.service.product.CategoryService; import ntnu.idatt2016.v233.SmartMat.service.product.ProductService; import org.junit.jupiter.api.Test; @@ -30,6 +32,9 @@ public class ProductControllerTest { @Mock private CategoryService categoryService; + @Mock + private AllergyService allergyService; + @InjectMocks private ProductController productController; @@ -41,14 +46,17 @@ public class ProductControllerTest { .name("Test Product") .description("A test product kylling") .image("http://test.com/image.jpg") + .allergies(List.of("Gluten", "Melk")) .build(); when(productService.getProductById(123L)).thenReturn(Optional.empty()); when(productService.getProductVolume(123L)).thenReturn(Optional.of(List.of("1", "kg"))); when(categoryService.getCategoryByName(anyString())).thenReturn(Optional.of(Category.builder() - .categoryName("Kjøtt") + .categoryName("Kjøtt") .build())); + when(allergyService.getAllergyByName(anyString())).thenReturn(Optional.of(Allergy.builder().name("test-allergy").build())); + // Act ResponseEntity<Product> response = productController.createProduct(productRequest); Product product = response.getBody(); diff --git a/target/classes/application.properties b/target/classes/application.properties index b13fdb2403ba71eb04d48c3180a862f33be4f843..0b2be5379b7e68fd12834a5ebbf3e40f67a27eab 100644 --- a/target/classes/application.properties +++ b/target/classes/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