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 6646026e7704c060b85549eb1530d7a00da8b8ae..5d4751e478133855963de8271e11275452a99412 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,10 +2,11 @@ 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.Category; import ntnu.idatt2016.v233.SmartMat.entity.product.Product; +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.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -26,6 +27,8 @@ public class ProductController { private final ProductService productService; + private final CategoryService categoryService; + /** * Creates a product if it does not already exist. * @param productRequest The product to be registered. @@ -40,10 +43,15 @@ public class ProductController { .url(productRequest.image()) .build(); + Category category = categoryService + .getCategoryByName(CategoryUtil.defineCategory(product.getName(),product.getDescription())) + .orElse(null); + if (category == null) + return ResponseEntity.badRequest().build(); + product.setCategory(category); - CategoryUtil.defineCategory(product.getName(),product.getDescription()); if(productService.getProductById(productRequest.ean()).isPresent()) return ResponseEntity.status(409).build();