Skip to content
Snippets Groups Projects
Commit 1193559d authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Merge branch '253-clasifie-if-a-product-is-not-vegan-not-vegetarian-and-haram' into 'main'

Resolve "Clasifie if a product is not-vegan, not-vegetarian and haram"

Closes #253

See merge request idatt2106-v23-03/backend!202
parents 84e2d1c7 584a43fd
No related branches found
No related tags found
No related merge requests found
...@@ -2,12 +2,14 @@ package ntnu.idatt2016.v233.SmartMat.controller.product; ...@@ -2,12 +2,14 @@ package ntnu.idatt2016.v233.SmartMat.controller.product;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.dto.request.ProductRequest; 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.Category;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import ntnu.idatt2016.v233.SmartMat.service.AllergyService; import ntnu.idatt2016.v233.SmartMat.service.AllergyService;
import ntnu.idatt2016.v233.SmartMat.service.product.CategoryService; import ntnu.idatt2016.v233.SmartMat.service.product.CategoryService;
import ntnu.idatt2016.v233.SmartMat.service.product.ProductService; import ntnu.idatt2016.v233.SmartMat.service.product.ProductService;
import ntnu.idatt2016.v233.SmartMat.util.CategoryUtil; import ntnu.idatt2016.v233.SmartMat.util.CategoryUtil;
import ntnu.idatt2016.v233.SmartMat.util.ProductUtil;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -89,6 +91,26 @@ public class ProductController { ...@@ -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); productService.saveProduct(product);
return ResponseEntity.ok(product); return ResponseEntity.ok(product);
......
package ntnu.idatt2016.v233.SmartMat.controller.product; package ntnu.idatt2016.v233.SmartMat.controller.product;
import ntnu.idatt2016.v233.SmartMat.dto.request.ProductRequest; 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.Category;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product; 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.CategoryService;
import ntnu.idatt2016.v233.SmartMat.service.product.ProductService; import ntnu.idatt2016.v233.SmartMat.service.product.ProductService;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -30,6 +32,9 @@ public class ProductControllerTest { ...@@ -30,6 +32,9 @@ public class ProductControllerTest {
@Mock @Mock
private CategoryService categoryService; private CategoryService categoryService;
@Mock
private AllergyService allergyService;
@InjectMocks @InjectMocks
private ProductController productController; private ProductController productController;
...@@ -41,6 +46,7 @@ public class ProductControllerTest { ...@@ -41,6 +46,7 @@ public class ProductControllerTest {
.name("Test Product") .name("Test Product")
.description("A test product kylling") .description("A test product kylling")
.image("http://test.com/image.jpg") .image("http://test.com/image.jpg")
.allergies(List.of("Gluten", "Melk"))
.build(); .build();
when(productService.getProductById(123L)).thenReturn(Optional.empty()); when(productService.getProductById(123L)).thenReturn(Optional.empty());
...@@ -49,6 +55,8 @@ public class ProductControllerTest { ...@@ -49,6 +55,8 @@ public class ProductControllerTest {
.categoryName("Kjøtt") .categoryName("Kjøtt")
.build())); .build()));
when(allergyService.getAllergyByName(anyString())).thenReturn(Optional.of(Allergy.builder().name("test-allergy").build()));
// Act // Act
ResponseEntity<Product> response = productController.createProduct(productRequest); ResponseEntity<Product> response = productController.createProduct(productRequest);
Product product = response.getBody(); Product product = response.getBody();
......
...@@ -5,7 +5,7 @@ spring.datasource.username=smartmat_user ...@@ -5,7 +5,7 @@ spring.datasource.username=smartmat_user
spring.datasource.password=Eyhs1OJxyZC56NQCrV7yAolEk9AkLAsC spring.datasource.password=Eyhs1OJxyZC56NQCrV7yAolEk9AkLAsC
# jpa settings # jpa settings
spring.jpa.show-sql=true spring.jpa.show-sql=false
## Hibernate Properties ## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database # The SQL dialect makes Hibernate generate better SQL for the chosen database
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment