Skip to content
Snippets Groups Projects
Commit 3f88bbe2 authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

added categories to products before saving

parent 96d1dc92
No related branches found
No related tags found
No related merge requests found
...@@ -2,10 +2,11 @@ package ntnu.idatt2016.v233.SmartMat.controller.product; ...@@ -2,10 +2,11 @@ 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.Category;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product; 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.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.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -26,6 +27,8 @@ public class ProductController { ...@@ -26,6 +27,8 @@ public class ProductController {
private final ProductService productService; private final ProductService productService;
private final CategoryService categoryService;
/** /**
* Creates a product if it does not already exist. * Creates a product if it does not already exist.
* @param productRequest The product to be registered. * @param productRequest The product to be registered.
...@@ -40,10 +43,15 @@ public class ProductController { ...@@ -40,10 +43,15 @@ public class ProductController {
.url(productRequest.image()) .url(productRequest.image())
.build(); .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()) if(productService.getProductById(productRequest.ean()).isPresent())
return ResponseEntity.status(409).build(); return ResponseEntity.status(409).build();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment