From b741f13f4d4da6f3c831f95601fee0d9a92da328 Mon Sep 17 00:00:00 2001 From: Nikita Sergeev Date: Sun, 23 Apr 2023 13:00:59 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=81=D1=91=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 9 +- .../java/ip/labwork/WebConfiguration.java | 3 +- .../method/controller/MethodController.java | 3 + .../shop/controller/ComponentController.java | 3 +- .../labwork/shop/controller/ComponentDTO.java | 22 ++- .../controller/ComponentMvcController.java | 55 +++++++ .../shop/controller/OrderController.java | 3 +- .../ip/labwork/shop/controller/OrderDTO.java | 19 ++- .../shop/controller/OrderMvcController.java | 94 ++++++++++++ .../shop/controller/ProductController.java | 3 +- .../labwork/shop/controller/ProductDTO.java | 42 ++++-- .../shop/controller/ProductMvcController.java | 83 ++++++++++ .../labwork/shop/service/ProductService.java | 61 +++++++- .../test/controller/TestController.java | 3 +- .../labwork/util/error/AdviceController.java | 3 +- src/main/resources/public/css/style.css | 86 +++++++++++ .../resources/templates/component-edit.html | 49 ++++++ src/main/resources/templates/component.html | 65 ++++++++ src/main/resources/templates/default.html | 65 ++++++++ src/main/resources/templates/error.html | 14 ++ src/main/resources/templates/index.html | 14 ++ src/main/resources/templates/order.html | 132 ++++++++++++++++ .../resources/templates/product-edit.html | 142 ++++++++++++++++++ src/main/resources/templates/product.html | 108 +++++++++++++ 24 files changed, 1054 insertions(+), 27 deletions(-) create mode 100644 src/main/java/ip/labwork/shop/controller/ComponentMvcController.java create mode 100644 src/main/java/ip/labwork/shop/controller/OrderMvcController.java create mode 100644 src/main/java/ip/labwork/shop/controller/ProductMvcController.java create mode 100644 src/main/resources/public/css/style.css create mode 100644 src/main/resources/templates/component-edit.html create mode 100644 src/main/resources/templates/component.html create mode 100644 src/main/resources/templates/default.html create mode 100644 src/main/resources/templates/error.html create mode 100644 src/main/resources/templates/index.html create mode 100644 src/main/resources/templates/order.html create mode 100644 src/main/resources/templates/product-edit.html create mode 100644 src/main/resources/templates/product.html diff --git a/build.gradle b/build.gradle index 8fdd46b..16e0e67 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,6 @@ jar { } dependencies { - implementation(project(':front')) implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'com.h2database:h2:2.1.210' @@ -27,6 +26,14 @@ dependencies { implementation 'org.springdoc:springdoc-openapi-ui:1.6.5' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.springframework.boot:spring-boot-starter-validation' + + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + implementation 'org.springframework.boot:spring-boot-devtools' + implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect' + + implementation 'org.webjars:bootstrap:5.1.3' + implementation 'org.webjars:jquery:3.6.0' + implementation 'org.webjars:font-awesome:6.1.0' } tasks.named('test') { diff --git a/src/main/java/ip/labwork/WebConfiguration.java b/src/main/java/ip/labwork/WebConfiguration.java index 29b08a4..0913c9c 100644 --- a/src/main/java/ip/labwork/WebConfiguration.java +++ b/src/main/java/ip/labwork/WebConfiguration.java @@ -12,7 +12,8 @@ import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.http.HttpStatus; @Configuration -class WebConfiguration implements WebMvcConfigurer { +public class WebConfiguration implements WebMvcConfigurer { + public static final String REST_API = "/api"; @Override public void addCorsMappings(CorsRegistry registry){ registry.addMapping("/**").allowedMethods("*"); diff --git a/src/main/java/ip/labwork/method/controller/MethodController.java b/src/main/java/ip/labwork/method/controller/MethodController.java index 63add70..3fa801f 100644 --- a/src/main/java/ip/labwork/method/controller/MethodController.java +++ b/src/main/java/ip/labwork/method/controller/MethodController.java @@ -1,11 +1,14 @@ package ip.labwork.method.controller; +import ip.labwork.WebConfiguration; import ip.labwork.method.service.MethodService; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController +@RequestMapping(WebConfiguration.REST_API + "/method") public class MethodController { private final MethodService speakerService; diff --git a/src/main/java/ip/labwork/shop/controller/ComponentController.java b/src/main/java/ip/labwork/shop/controller/ComponentController.java index 4cd5eed..e3fc83a 100644 --- a/src/main/java/ip/labwork/shop/controller/ComponentController.java +++ b/src/main/java/ip/labwork/shop/controller/ComponentController.java @@ -1,5 +1,6 @@ package ip.labwork.shop.controller; +import ip.labwork.WebConfiguration; import ip.labwork.shop.service.ComponentService; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; @@ -7,7 +8,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping("/component") +@RequestMapping(WebConfiguration.REST_API + "/component") public class ComponentController { private final ComponentService componentService; diff --git a/src/main/java/ip/labwork/shop/controller/ComponentDTO.java b/src/main/java/ip/labwork/shop/controller/ComponentDTO.java index a4b4caf..e01ef78 100644 --- a/src/main/java/ip/labwork/shop/controller/ComponentDTO.java +++ b/src/main/java/ip/labwork/shop/controller/ComponentDTO.java @@ -1,6 +1,8 @@ package ip.labwork.shop.controller; +import com.fasterxml.jackson.annotation.JsonProperty; import ip.labwork.shop.model.Component; +import jakarta.validation.constraints.NotBlank; public class ComponentDTO { private long id; @@ -26,15 +28,31 @@ public class ComponentDTO { return id; } + public void setId(long id) { + this.id = id; + } + public String getComponentName() { return componentName; } + public void setComponentName(String componentName) { + this.componentName = componentName; + } + + public int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } + public int getCount() { return count; } - public int getPrice() { - return price; + public void setCount(int count) { + this.count = count; } } \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/controller/ComponentMvcController.java b/src/main/java/ip/labwork/shop/controller/ComponentMvcController.java new file mode 100644 index 0000000..9c99bc2 --- /dev/null +++ b/src/main/java/ip/labwork/shop/controller/ComponentMvcController.java @@ -0,0 +1,55 @@ +package ip.labwork.shop.controller; + +import ip.labwork.shop.service.ComponentService; +import jakarta.validation.Valid; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +@Controller +@RequestMapping("/component") +public class ComponentMvcController { + private final ComponentService componentService; + + public ComponentMvcController(ComponentService componentService) { + this.componentService = componentService; + } + @GetMapping + public String getComponents(Model model) { + model.addAttribute("components", componentService.findAllComponent()); + return "component"; + } + @GetMapping(value = {"/edit", "/edit/{id}"}) + public String editComponent(@PathVariable(required = false) Long id, + Model model) { + if (id == null || id <= 0) { + model.addAttribute("componentDto", new ComponentDTO()); + } else { + model.addAttribute("componentId", id); + model.addAttribute("componentDto", new ComponentDTO(componentService.findComponent(id))); + } + return "component-edit"; + } + @PostMapping(value = {"/", "/{id}"}) + public String saveComponent(@PathVariable(required = false) Long id, + @ModelAttribute @Valid ComponentDTO componentDTO, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "component-edit"; + } + if (id == null || id <= 0) { + componentService.create(componentDTO); + } else { + componentService.updateComponent(id, componentDTO); + } + return "redirect:/component"; + } + @PostMapping("/delete/{id}") + public String deleteComponent(@PathVariable Long id) { + componentService.deleteComponent(id); + return "redirect:/component"; + } +} \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/controller/OrderController.java b/src/main/java/ip/labwork/shop/controller/OrderController.java index a24b2c6..36e83b8 100644 --- a/src/main/java/ip/labwork/shop/controller/OrderController.java +++ b/src/main/java/ip/labwork/shop/controller/OrderController.java @@ -1,5 +1,6 @@ package ip.labwork.shop.controller; +import ip.labwork.WebConfiguration; import ip.labwork.shop.service.OrderService; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; @@ -7,7 +8,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping("/order") +@RequestMapping(WebConfiguration.REST_API + "/order") public class OrderController { private final OrderService orderService; public OrderController(OrderService orderService) { diff --git a/src/main/java/ip/labwork/shop/controller/OrderDTO.java b/src/main/java/ip/labwork/shop/controller/OrderDTO.java index ef9305d..dccbedd 100644 --- a/src/main/java/ip/labwork/shop/controller/OrderDTO.java +++ b/src/main/java/ip/labwork/shop/controller/OrderDTO.java @@ -2,6 +2,7 @@ package ip.labwork.shop.controller; import ip.labwork.shop.model.Order; import ip.labwork.shop.model.OrderStatus; +import jakarta.validation.constraints.NotBlank; import java.util.Date; import java.util.List; @@ -9,7 +10,9 @@ import java.util.Objects; public class OrderDTO { private long id; + @NotBlank(message = "Date can't be null or empty") private Date date = new Date(); + @NotBlank(message = "Price can't be null or empty") private int price; private OrderStatus status = OrderStatus.Неизвестен; private List productDTOList; @@ -31,10 +34,18 @@ public class OrderDTO { return id; } + public void setId(long id) { + this.id = id; + } + public Date getDate() { return date; } + public void setDate(Date date) { + this.date = date; + } + public int getPrice() { return price; } @@ -43,10 +54,6 @@ public class OrderDTO { this.price = price; } - public void setDate(Date date) { - this.date = date; - } - public OrderStatus getStatus() { return status; } @@ -58,4 +65,8 @@ public class OrderDTO { public List getProductDTOList() { return productDTOList; } + + public void setProductDTOList(List productDTOList) { + this.productDTOList = productDTOList; + } } \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/controller/OrderMvcController.java b/src/main/java/ip/labwork/shop/controller/OrderMvcController.java new file mode 100644 index 0000000..e25d933 --- /dev/null +++ b/src/main/java/ip/labwork/shop/controller/OrderMvcController.java @@ -0,0 +1,94 @@ +package ip.labwork.shop.controller; + +import ip.labwork.shop.model.OrderStatus; +import ip.labwork.shop.service.OrderService; +import ip.labwork.shop.service.ProductService; +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +@Controller +@RequestMapping("/order") +public class OrderMvcController { + private final OrderService orderService; + private final ProductService productService; + public OrderMvcController(OrderService orderService, ProductService productService) { + this.orderService = orderService; + this.productService = productService; + } + @GetMapping + public String getOrders(HttpServletRequest request, + Model model) { + Cookie[] cookies = request.getCookies(); + List productDTOS = new ArrayList<>(); + int totalPrice = 0; + for(Cookie cookie : cookies){ + if (StringUtils.isNumeric(cookie.getName())){ + ProductDTO productDTO = new ProductDTO(productService.findProduct(Long.parseLong(cookie.getName())),Integer.parseInt(cookie.getValue())); + productDTOS.add(productDTO); + totalPrice += productDTO.getPrice() * productDTO.getCount(); + } + } + model.addAttribute("productDTOS", productDTOS); + model.addAttribute("totalPrice", totalPrice); + return "order"; + } + @PostMapping + public String createOrder(HttpServletRequest request, + HttpServletResponse response) { + Cookie[] cookies = request.getCookies(); + OrderDTO orderDTO = new OrderDTO(); + List productDTOS = new ArrayList<>(); + int totalPrice = 0; + for(Cookie temp : cookies){ + if (StringUtils.isNumeric(temp.getName())){ + ProductDTO productDTO = new ProductDTO(productService.findProduct(Long.parseLong(temp.getName())),Integer.parseInt(temp.getValue())); + productDTOS.add(productDTO); + totalPrice += productDTO.getPrice() * productDTO.getCount(); + } + } + orderDTO.setPrice(totalPrice); + orderDTO.setProductDTOList(productDTOS); + orderDTO.setStatus(OrderStatus.Готов); + orderService.create(orderDTO); + response.addCookie(new Cookie("delete","")); + return "redirect:/order"; + } +// @PostMapping("/delete/{id}") +// public String deleteProduct(@PathVariable(required = false) Long id, HttpServletRequest request, Model model, HttpServletResponse response){ +// Cookie[] cookies = request.getCookies(); +// List productDTOS = new ArrayList<>(); +// int totalPrice = 0; +// for(Cookie temp : cookies){ +// if (StringUtils.isNumeric(temp.getName())){ +// ProductDTO productDTO; +// if(Long.parseLong(temp.getName()) == id){ +// if (Objects.equals(temp.getValue(), "") || Integer.parseInt(temp.getValue()) == 1){ +// Cookie userNameCookieRemove = new Cookie(temp.getName(), ""); +// userNameCookieRemove.setMaxAge(-1); +// response.addCookie(userNameCookieRemove); +// continue; +// } +// productDTO = new ProductDTO(productService.findProduct(Long.parseLong(temp.getName())),Integer.parseInt(temp.getValue())-1); +// temp.setValue(productDTO.getCount()+""); +// temp.setMaxAge(-1); +// response.addCookie(temp); +// }else{ +// productDTO = new ProductDTO(productService.findProduct(Long.parseLong(temp.getName())),Integer.parseInt(temp.getValue())); +// } +// productDTOS.add(productDTO); +// totalPrice += productDTO.getPrice() * productDTO.getCount(); +// } +// } +// model.addAttribute("productDTOS", productDTOS); +// model.addAttribute("totalPrice", totalPrice); +// return "order"; +// } +} diff --git a/src/main/java/ip/labwork/shop/controller/ProductController.java b/src/main/java/ip/labwork/shop/controller/ProductController.java index 4199caf..46e88c7 100644 --- a/src/main/java/ip/labwork/shop/controller/ProductController.java +++ b/src/main/java/ip/labwork/shop/controller/ProductController.java @@ -1,5 +1,6 @@ package ip.labwork.shop.controller; +import ip.labwork.WebConfiguration; import ip.labwork.shop.service.ProductService; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; @@ -7,7 +8,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping("/product") +@RequestMapping(WebConfiguration.REST_API + "/product") public class ProductController { private final ProductService productService; diff --git a/src/main/java/ip/labwork/shop/controller/ProductDTO.java b/src/main/java/ip/labwork/shop/controller/ProductDTO.java index ee9d337..90b9288 100644 --- a/src/main/java/ip/labwork/shop/controller/ProductDTO.java +++ b/src/main/java/ip/labwork/shop/controller/ProductDTO.java @@ -1,13 +1,17 @@ package ip.labwork.shop.controller; import ip.labwork.shop.model.Product; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import java.util.List; import java.util.Objects; public class ProductDTO { private long id; + @NotBlank(message = "Name can't be null or empty") private String name; + @NotNull(message = "Price can't be null or empty") private int price; private List componentDTOList; private List orderDTOList; @@ -18,7 +22,7 @@ public class ProductDTO { this.name = product.getProductName(); this.price = product.getPrice(); this.image = product.getImage() == null? "" : new String(product.getImage()); - this.componentDTOList = product.getComponents().stream() + this.componentDTOList = product.getComponents() == null ? null : product.getComponents().stream() .filter(x -> Objects.equals(x.getId().getProductId(), product.getId())) .map(y -> new ComponentDTO(y.getComponent(), y.getCount())) .toList(); @@ -42,26 +46,42 @@ public class ProductDTO { return id; } - public int getCount() { - return count; - } - - public void setCount(int count) { - this.count = count; + public void setId(long id) { + this.id = id; } public String getName() { return name; } + public void setName(String name) { + this.name = name; + } + public int getPrice() { return price; } + public void setPrice(int price) { + this.price = price; + } + public List getComponentDTOList() { return componentDTOList; } + public void setComponentDTOList(List componentDTOList) { + this.componentDTOList = componentDTOList; + } + + public List getOrderDTOList() { + return orderDTOList; + } + + public void setOrderDTOList(List orderDTOList) { + this.orderDTOList = orderDTOList; + } + public String getImage() { return image; } @@ -70,7 +90,11 @@ public class ProductDTO { this.image = image; } - public List getOrderDTOList() { - return orderDTOList; + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; } } \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/controller/ProductMvcController.java b/src/main/java/ip/labwork/shop/controller/ProductMvcController.java new file mode 100644 index 0000000..4f14c4f --- /dev/null +++ b/src/main/java/ip/labwork/shop/controller/ProductMvcController.java @@ -0,0 +1,83 @@ +package ip.labwork.shop.controller; + +import ip.labwork.shop.service.ComponentService; +import ip.labwork.shop.service.ProductService; +import jakarta.validation.Valid; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.util.Base64; + +@Controller +@RequestMapping("/product") +public class ProductMvcController { + private final ProductService productService; + private final ComponentService componentService; + public ProductMvcController(ProductService productService, ComponentService componentService) { + this.productService = productService; + this.componentService = componentService; + } + @GetMapping + public String getProducts(Model model) { + model.addAttribute("products", productService.findAllProduct()); + return "product"; + } + @GetMapping(value = {"/edit", "/edit/{id}"}) + public String editProduct(@PathVariable(required = false) Long id, + Model model) { + if (id == null || id <= 0) { + model.addAttribute("productDto", new ProductDTO()); + } else { + model.addAttribute("productId", id); + model.addAttribute("componentDto", new ComponentDTO()); + model.addAttribute("productDto", new ProductDTO(productService.findProduct(id))); + } + model.addAttribute("components", componentService.findAllComponent()); + return "product-edit"; + } + @PostMapping(value = {"/", "/{id}"}) + public String saveProduct(@PathVariable(required = false) Long id, + @RequestParam("file") MultipartFile file, + @ModelAttribute @Valid ProductDTO productDTO, + BindingResult bindingResult, + Model model) throws IOException { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "product-edit"; + } + productDTO.setImage("data:image/jpeg;base64," + Base64.getEncoder().encodeToString(file.getBytes())); + if (id == null || id <= 0) { + return "redirect:/product/edit/" + productService.create(productDTO).getId(); + } else { + productService.updateFields(id, productDTO); + return "redirect:/product/edit/" + id; + } + } + @PostMapping("/delete/{id}") + public String deleteProduct(@PathVariable Long id) { + productService.deleteProduct(id); + return "redirect:/product"; + } + @PostMapping("/delete/{id}/{componentid}") + public String deleteComponent(@PathVariable Long id, + @PathVariable Long componentid) { + productService.deleteComponent(id,componentid); + return "redirect:/product/edit/" + id; + } + @PostMapping("/addproduct/{id}") + public String addComponent(@PathVariable Long id, + @ModelAttribute @Valid ComponentDTO componentDTO, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "product-edit"; + } + productService.addComponent(id, componentDTO); + return "redirect:/product/edit/" + id; + } +} \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/service/ProductService.java b/src/main/java/ip/labwork/shop/service/ProductService.java index 982d330..25779cc 100644 --- a/src/main/java/ip/labwork/shop/service/ProductService.java +++ b/src/main/java/ip/labwork/shop/service/ProductService.java @@ -1,5 +1,6 @@ package ip.labwork.shop.service; +import ip.labwork.shop.controller.ComponentDTO; import ip.labwork.shop.controller.ProductDTO; import ip.labwork.shop.model.Component; import ip.labwork.shop.model.OrderProducts; @@ -30,9 +31,11 @@ public class ProductService { final Product product = new Product(productDTO.getName(), productDTO.getPrice(),productDTO.getImage().getBytes()); validatorUtil.validate(product); productRepository.save(product); - for (int i = 0; i < productDTO.getComponentDTOList().size(); i++) { - final ProductComponents productComponents = new ProductComponents(componentRepository.findById(productDTO.getComponentDTOList().get(i).getId()).orElseThrow(() -> new ComponentNotFoundException(1L)), product, productDTO.getComponentDTOList().get(i).getCount()); - product.addComponent(productComponents); + if(productDTO.getComponentDTOList() != null){ + for (int i = 0; i < productDTO.getComponentDTOList().size(); i++) { + final ProductComponents productComponents = new ProductComponents(componentRepository.findById(productDTO.getComponentDTOList().get(i).getId()).orElseThrow(() -> new ComponentNotFoundException(1L)), product, productDTO.getComponentDTOList().get(i).getCount()); + product.addComponent(productComponents); + } } productRepository.save(product); return new ProductDTO(findProduct(product.getId())); @@ -85,6 +88,17 @@ public class ProductService { return new ProductDTO(currentProduct); } @Transactional + public ProductDTO updateFields(Long id, ProductDTO product) { + final Product currentProduct = findProduct(id); + currentProduct.setProductName(product.getName()); + if (product.getImage().length()>23){ + currentProduct.setImage(product.getImage().getBytes()); + } + validatorUtil.validate(currentProduct); + productRepository.save(currentProduct); + return new ProductDTO(currentProduct); + } + @Transactional public ProductDTO deleteProduct(Long id) { final Product currentProduct = findProduct(id); int size = currentProduct.getComponents().size(); @@ -109,7 +123,44 @@ public class ProductService { } @Transactional - public List findFiltredProducts(Long[] arr) { - return productRepository.findAllById(Arrays.stream(arr).toList()); + public ProductDTO addComponent(Long id, ComponentDTO componentDTO) { + if (componentDTO.getCount() <= 0){ + return null; + } + final Product currentProduct = findProduct(id); + List productComponentsList = productRepository.getProductComponent(id); + if(productComponentsList.stream().filter(x -> x.getId().getComponentId() == componentDTO.getId()).toList().size() != 0) { + final ProductComponents productComponents = productRepository.getProductComponent(id).stream().filter(x -> x.getId().getComponentId().equals(componentDTO.getId())).findFirst().get(); + currentProduct.removeComponent(productComponents); + currentProduct.addComponent(new ProductComponents(componentRepository.findById(componentDTO.getId()).orElseThrow(() -> new ComponentNotFoundException(id)), currentProduct, componentDTO.getCount())); + }else{ + final ProductComponents productComponents = new ProductComponents(componentRepository.findById(componentDTO.getId()).orElseThrow(() -> new ComponentNotFoundException(id)), currentProduct, componentDTO.getCount()); + currentProduct.addComponent(productComponents); + } + productRepository.saveAndFlush(currentProduct); + int price = 0; + for(int i = 0; i < productRepository.getProductComponent(id).size(); i++){ + price += productRepository.getProductComponent(id).get(i).getComponent().getPrice() * productRepository.getProductComponent(id).get(i).getCount(); + } + currentProduct.setPrice(price); + productRepository.saveAndFlush(currentProduct); + return new ProductDTO(currentProduct); + } + @Transactional + public ProductDTO deleteComponent(Long id, Long componentId) { + Product currentProduct = findProduct(id); + final ProductComponents productComponents = productRepository.getProductComponent(id).stream().filter(x -> x.getId().getComponentId().equals(componentId)).findFirst().get(); + currentProduct.removeComponent(productComponents); + Component component = componentRepository.findById(componentId).orElseThrow(() -> new ComponentNotFoundException(componentId)); + component.removeProduct(productComponents); + productRepository.save(currentProduct); + currentProduct = findProduct(id); + int price = 0; + for(int i = 0; i < currentProduct.getComponents().size(); i++){ + price += currentProduct.getComponents().get(i).getComponent().getPrice() * currentProduct.getComponents().get(i).getCount(); + } + currentProduct.setPrice(price); + productRepository.saveAndFlush(currentProduct); + return new ProductDTO(currentProduct); } } diff --git a/src/main/java/ip/labwork/test/controller/TestController.java b/src/main/java/ip/labwork/test/controller/TestController.java index 096108f..b3f4a5d 100644 --- a/src/main/java/ip/labwork/test/controller/TestController.java +++ b/src/main/java/ip/labwork/test/controller/TestController.java @@ -1,5 +1,6 @@ package ip.labwork.test.controller; +import ip.labwork.WebConfiguration; import ip.labwork.test.model.TestDto; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.PostMapping; @@ -8,7 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("/test") +@RequestMapping(WebConfiguration.REST_API + "/test") public class TestController { @PostMapping public TestDto testValidation(@RequestBody @Valid TestDto testDto) { diff --git a/src/main/java/ip/labwork/util/error/AdviceController.java b/src/main/java/ip/labwork/util/error/AdviceController.java index 58a92da..d698a1f 100644 --- a/src/main/java/ip/labwork/util/error/AdviceController.java +++ b/src/main/java/ip/labwork/util/error/AdviceController.java @@ -10,10 +10,11 @@ import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestController; import java.util.stream.Collectors; -@ControllerAdvice +@ControllerAdvice(annotations = RestController.class) public class AdviceController { @ExceptionHandler({ ComponentNotFoundException.class, diff --git a/src/main/resources/public/css/style.css b/src/main/resources/public/css/style.css new file mode 100644 index 0000000..487b833 --- /dev/null +++ b/src/main/resources/public/css/style.css @@ -0,0 +1,86 @@ +nav { + background-color: #c8afaf; +} +main { + background-color: #8c7b7b; + min-height: 90vh; +} +footer { + background-color: #c8afaf; + color: black; +} + +.size { + overflow-x: hidden; +} +.card { + background-color: #8c7b7b; + border-color: #8c7b7b; +} +.card-body { + background-color: #8c7b7b; +} +h1 { + word-wrap: break-word; +} +#banner { + margin: 0px 15px 15px 15px; + padding-top: 15px; + display: flex; + align-items: center; + flex-direction: column; +} + +#banner img { + border-radius: 5px; +} +#banner img.show { + width: 100%; + opacity: 1; + transition: opacity 1s, visibility 0s; +} + +#banner img.hide { + height: 0; + width: 0; + opacity: 0; + visibility: hidden; + transition: opacity 1s, visibility 0s 1s; +} +@media (max-width: 767px) { + .btn { + padding: 1px 5px; + font-size: 12px; + line-height: 1.5; + border-radius: 3px; + } +} +@media (min-width: 768px) and (max-width: 991px) { + .btn { + padding: 5px 10px; + font-size: 12px; + line-height: 1.5; + border-radius: 3px; + } +} +@media (min-width: 992px) and (max-width: 1199px) { + .btn { + padding: 6px 12px; + font-size: 14px; + line-height: 1.42857143; + border-radius: 4px; + } +} +@media (min-width: 1200px) { + .btn { + padding: 10px 16px; + font-size: 18px; + line-height: 1.3333333; + border-radius: 6px; + } +} +@media (max-width: 600px) { + .tem { + padding-right: 50px; + } +} diff --git a/src/main/resources/templates/component-edit.html b/src/main/resources/templates/component-edit.html new file mode 100644 index 0000000..abb0b9b --- /dev/null +++ b/src/main/resources/templates/component-edit.html @@ -0,0 +1,49 @@ + + + + +
+
+
+
+ + +
+
+ + +
+
+ + + Назад + +
+
+
+ + diff --git a/src/main/resources/templates/component.html b/src/main/resources/templates/component.html new file mode 100644 index 0000000..44b4dd8 --- /dev/null +++ b/src/main/resources/templates/component.html @@ -0,0 +1,65 @@ + + + + +
+ +
+ + + + + + + + + + + + + + +
#НазваниеЦена
+ + + +
+ + Изменить + + +
+
+ +
+
+
+
+ + diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html new file mode 100644 index 0000000..70ca687 --- /dev/null +++ b/src/main/resources/templates/default.html @@ -0,0 +1,65 @@ + + + + + Очень вкусно и запятая + + + + + + + +
+ +
+
+
+
+ ООО "Вкусно" © 2022 +
+
+ + + diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html new file mode 100644 index 0000000..3be5037 --- /dev/null +++ b/src/main/resources/templates/error.html @@ -0,0 +1,14 @@ + + + + + + + diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..a6ef848 --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,14 @@ + + + + +
+
It's works!
+ ERROR +
+ + diff --git a/src/main/resources/templates/order.html b/src/main/resources/templates/order.html new file mode 100644 index 0000000..d9dd0f7 --- /dev/null +++ b/src/main/resources/templates/order.html @@ -0,0 +1,132 @@ + + + + +
+

+ Корзина +

+

+
Список товаров
+
+
+ + + + + + + + + + + + + + + + + + +
#ПозицияСтоимость
+ +
+
+

+ +
+ +
+
+

+

+
+ + + + + diff --git a/src/main/resources/templates/product-edit.html b/src/main/resources/templates/product-edit.html new file mode 100644 index 0000000..90b7955 --- /dev/null +++ b/src/main/resources/templates/product-edit.html @@ -0,0 +1,142 @@ + + + + + + +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + + Назад + +
+
+
+
+ + +
+
+ + +
+
+
+ +
+
+
+
+ + + + + + + + + + + + + +
НазваниеКоличество
+ + +
+ +
+
+ +
+
+
+
+ + diff --git a/src/main/resources/templates/product.html b/src/main/resources/templates/product.html new file mode 100644 index 0000000..77a3663 --- /dev/null +++ b/src/main/resources/templates/product.html @@ -0,0 +1,108 @@ + + + + +
+ +
+
+
+
+ Бугер +
+ +
+
+
+
+ + + + +