From c57dc591f1e3c519d264a479a9b4f8b1fdac6c7c Mon Sep 17 00:00:00 2001 From: Katerina881 Date: Mon, 27 Mar 2023 17:33:09 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=B4=D0=B5=D1=8E=D1=81=D1=8C=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/index.html | 43 +++-- front/src/scriptComponent.js | 4 +- front/src/scriptOrder.js | 156 +++++++++++------- .../shop/controller/OrderController.java | 7 +- .../ip/labwork/shop/controller/OrderDTO.java | 5 +- .../shop/controller/ProductController.java | 7 +- .../labwork/shop/controller/ProductDTO.java | 20 ++- .../java/ip/labwork/shop/model/Order.java | 2 +- .../ip/labwork/shop/model/OrderProducts.java | 7 +- .../ip/labwork/shop/service/OrderService.java | 10 +- .../labwork/shop/service/ProductService.java | 18 +- 11 files changed, 169 insertions(+), 110 deletions(-) diff --git a/front/src/index.html b/front/src/index.html index 447171f..59944c7 100644 --- a/front/src/index.html +++ b/front/src/index.html @@ -27,26 +27,38 @@ -
+
-
- - +
+ +
-
- - +
+ + +
+
+ + +
+
+ + +
+
+ +
- +
-
- +
+
-
- +
+
@@ -54,9 +66,10 @@ - - - + + + + diff --git a/front/src/scriptComponent.js b/front/src/scriptComponent.js index 4a784dc..e0e883f 100644 --- a/front/src/scriptComponent.js +++ b/front/src/scriptComponent.js @@ -36,7 +36,7 @@ window.addEventListener('DOMContentLoaded', function () { const remove = async function (){ console.info('Try to remove item'); - if (itemId.value !== 0) { + if (componentIdInput.value !== 0) { if (!confirm('Do you really want to remove this item?')) { console.info('Canceled'); return; @@ -48,7 +48,7 @@ window.addEventListener('DOMContentLoaded', function () { "Content-Type": "application/json", } }; - const response = await fetch(host + `/component/` + itemId.value, requestParams); + const response = await fetch(host + `/component/` + componentIdInput.value, requestParams); return await response.json(); } diff --git a/front/src/scriptOrder.js b/front/src/scriptOrder.js index 0d8d677..1078ead 100644 --- a/front/src/scriptOrder.js +++ b/front/src/scriptOrder.js @@ -4,72 +4,110 @@ window.addEventListener('DOMContentLoaded', function () { const host = "http://localhost:8080"; const table = document.getElementById("tbody"); const form = document.getElementById("form"); - const lastNameInput = document.getElementById("componentName"); - const firstNameInput = document.getElementById("firstName"); - const testErrorBtn = document.getElementById("testError"); - const testNormalBtn = document.getElementById("testNormal"); + const orderIdInput = document.getElementById("orderId"); + const orderDate = document.getElementById("orderDate"); + const priceInput = document.getElementById("orderPrice"); + const productIdInput = document.getElementById("productId"); + const productCountInput = document.getElementById("productCount"); + const buttonRemove = document.getElementById("btnRemove"); + const buttonUpdate = document.getElementById("btnUpdate"); const getData = async function () { - table.innerHTML = ""; - const response = await fetch(host + "/student"); - const data = await response.json(); - data.forEach(student => { - table.innerHTML += - ` - - - - `; - }) - } - const create = async function (firstName, lastName) { - const requestParams = { - method: "POST", - headers: { - "Content-Type": "application/json", - } - }; - const response = await fetch(host + `/student?firstName=${firstName}&lastName=${lastName}`, requestParams); - return await response.json(); - } - - const test = async function (testObject) { - const requestParams = { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(testObject), - }; - const response = await fetch(host + "/test", requestParams); - if (response.status === 200) { + table.innerHTML = ""; + const response = await fetch(host + "/order"); const data = await response.json(); - alert(`TestDto=[id=${data.id}, name=${data.name}, data=${data.data}]`); + data.forEach(Order => { + let temp = "" + table.innerHTML += + ` + + + + + `; + }) } - if (response.status === 400) { - const data = await response.text(); - alert(data); - } - } - form.addEventListener("submit", function (event) { - event.preventDefault(); - create(firstNameInput.value, lastNameInput.value).then((result) => { - getData(); - firstNameInput.value = ""; - lastNameInput.value = ""; - alert(`Student[id=${result.id}, firstName=${result.firstName}, lastName=${result.lastName}]`); + const create = async function () { + const requestParams = { + method: "POST", + headers: { + "Content-Type": "application/json", + } + }; + let temp = Date.now(); + let time = new Date(temp); + let tru = time.toString(); + const response = await fetch(host + `/order?price=${priceInput.value}&date=${tru}&count=${productCountInput.value}&prod=${productIdInput.value}`, requestParams); + return await response.json(); + } + + const remove = async function (){ + console.info('Try to remove item'); + if (orderIdInput.value !== 0) { + if (!confirm('Do you really want to remove this item?')) { + console.info('Canceled'); + return; + } + } + const requestParams = { + method: "DELETE", + headers: { + "Content-Type": "application/json", + } + }; + const response = await fetch(host + `/order/` + orderIdInput.value, requestParams); + return await response.json(); + } + + const update = async function (){ + console.info('Try to update item'); + if (orderIdInput.value === 0 || orderDate.value == null || orderPrice.value === 0 || productIdInput.value === 0 || productCountInput.value === 0) { + return; + } + const requestParams = { + method: "PUT", + headers: { + "Content-Type": "application/json", + } + }; + let temp = Date.now(); + let time = new Date(temp); + let tru = time.toString(); + const response = await fetch(host + `/order/${orderIdInput.value}?price=${priceInput.value}&date=${tru}&count=${productCountInput.value}&prod=${productIdInput.value}`, requestParams); + return await response.json(); + } + + buttonRemove.addEventListener('click', function (event){ + event.preventDefault(); + remove().then((result) => { + getData() + orderIdInput.value = ""; + }); }); - }); - testErrorBtn.addEventListener("click", function () { - test({}); - }); + buttonUpdate.addEventListener('click', function (event){ + event.preventDefault(); + update().then((result) => { + getData() + orderIdInput.value = ""; + priceInput.value = ""; + }); + }); - testNormalBtn.addEventListener("click", function () { - test({id: 10, name: "test"}); - }); - - getData(); + form.addEventListener("submit", function (event) { + event.preventDefault(); + create().then((result) => { + getData(); + priceInput.value = ""; + orderIdInput.value = ""; + alert(`Component[id=${result.id}, price=${result.price}, componentName=${result.date}]`); + }); + }); + getData(); }); \ 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 3a69a92..a4890ed 100644 --- a/src/main/java/ip/labwork/shop/controller/OrderController.java +++ b/src/main/java/ip/labwork/shop/controller/OrderController.java @@ -18,13 +18,13 @@ public class OrderController { } @PostMapping - public OrderDTO createOrder(@RequestParam("date") String date, + public Order createOrder(@RequestParam("date") String date, @RequestParam("price") Integer price, @RequestParam("count") Integer[] count, @RequestParam("prod") Long[] prod){ final Order order = orderService.addOrder(date, price); orderService.addOrderProducts(orderService.findOrder(order.getId()), count, productService.findFiltredProducts(prod)); - return new OrderDTO(order); + return order; } @PutMapping("/{id}") public OrderDTO updateOrder(@PathVariable Long id, @@ -32,8 +32,7 @@ public class OrderController { @RequestParam("price") Integer price, @RequestParam("count") Integer[] count, @RequestParam("prod") Long[] prod){ - orderService.updateOrder(id, date, price, count, productService.findFiltredProducts(prod)); - return new OrderDTO(orderService.update(orderService.findOrder(id),orderService.getOrderProducts(orderService.findOrder(id)), orderService.getOrderProducts(orderService.findOrder(id)).stream().map(p -> p.getId().getProductId()).toList(), count, productService.findFiltredProducts(prod))); + return new OrderDTO(orderService.updateOrder(id, date, price, count, productService.findFiltredProducts(prod))); } @DeleteMapping("/{id}") public OrderDTO removeOrder(@PathVariable Long id){ diff --git a/src/main/java/ip/labwork/shop/controller/OrderDTO.java b/src/main/java/ip/labwork/shop/controller/OrderDTO.java index 3ca5b5a..1943e3e 100644 --- a/src/main/java/ip/labwork/shop/controller/OrderDTO.java +++ b/src/main/java/ip/labwork/shop/controller/OrderDTO.java @@ -15,7 +15,10 @@ public class OrderDTO { this.id = order.getId(); this.date = order.getDate(); this.price = order.getPrice(); - this.productDTOList = order.getProducts().stream().filter(x -> Objects.equals(x.getId().getOrderId(), order.getId())).map(x -> new ProductDTO(x.getProduct())).toList(); + this.productDTOList = order.getProducts().stream() + .filter(x -> Objects.equals(x.getId().getOrderId(), order.getId())) + .map(y -> new ProductDTO(y.getProduct(), y.getCount())) + .toList(); } public long getId() { diff --git a/src/main/java/ip/labwork/shop/controller/ProductController.java b/src/main/java/ip/labwork/shop/controller/ProductController.java index 6de4fcb..88ad7f5 100644 --- a/src/main/java/ip/labwork/shop/controller/ProductController.java +++ b/src/main/java/ip/labwork/shop/controller/ProductController.java @@ -23,9 +23,7 @@ public class ProductController { @RequestParam("price") Integer price, @RequestParam("count") Integer[] count, @RequestParam("comp") Long[] comp){ - final Product product = productService.addProduct(name, price); - productService.addProductComponents(productService.findProduct(product.getId()), count, componentService.findFiltredComponents(comp)); - return new ProductDTO(productService.findProduct(product.getId())); + return new ProductDTO(productService.addProduct(name, price, count, componentService.findFiltredComponents(comp))); } @PutMapping("/{id}") public ProductDTO updateProduct(@PathVariable Long id, @@ -33,8 +31,7 @@ public class ProductController { @RequestParam("price") Integer price, @RequestParam("count") Integer[] count, @RequestParam("comp") Long[] comp){ - productService.updateProduct(id, name, price, count, componentService.findFiltredComponents(comp)); - return new ProductDTO(productService.update(productService.findProduct(id),productService.getProductComponents(productService.findProduct(id)), productService.getProductComponents(productService.findProduct(id)).stream().map(p -> p.getId().getComponentId()).toList(), count, componentService.findFiltredComponents(comp))); + return new ProductDTO(productService.updateProduct(id, name, price, count, componentService.findFiltredComponents(comp))); } @DeleteMapping("/{id}") public ProductDTO removeProduct(@PathVariable Long id){ diff --git a/src/main/java/ip/labwork/shop/controller/ProductDTO.java b/src/main/java/ip/labwork/shop/controller/ProductDTO.java index 2815b2c..1c0cf20 100644 --- a/src/main/java/ip/labwork/shop/controller/ProductDTO.java +++ b/src/main/java/ip/labwork/shop/controller/ProductDTO.java @@ -1,5 +1,6 @@ package ip.labwork.shop.controller; +import ip.labwork.shop.model.Component; import ip.labwork.shop.model.Product; import ip.labwork.shop.model.ProductComponents; @@ -12,8 +13,8 @@ public class ProductDTO { private final long id; private final String productName; private final int price; + private int count = 0; private final List componentDTOList; - private final List orderDTOList; public ProductDTO(Product product) { this.id = product.getId(); this.productName = product.getProductName(); @@ -22,9 +23,17 @@ public class ProductDTO { .filter(x -> Objects.equals(x.getId().getProductId(), product.getId())) .map(y -> new ComponentDTO(y.getComponent(), y.getCount())) .toList(); - this.orderDTOList = product.getOrders() == null ? null : product.getOrders().stream().filter(x -> Objects.equals(x.getId().getProductId(), product.getId())).map(x -> new OrderDTO(x.getOrder())).toList(); } - + public ProductDTO(Product product, int count) { + this.id = product.getId(); + this.productName = product.getProductName(); + this.price = product.getPrice(); + this.componentDTOList = product.getComponents().stream() + .filter(x -> Objects.equals(x.getId().getProductId(), product.getId())) + .map(y -> new ComponentDTO(y.getComponent(), y.getCount())) + .toList(); + this.count = count; + } public long getId() { return id; } @@ -41,7 +50,8 @@ public class ProductDTO { return componentDTOList; } - public List getOrderDTOList() { - return orderDTOList; + + public int getCount() { + return count; } } \ No newline at end of file diff --git a/src/main/java/ip/labwork/shop/model/Order.java b/src/main/java/ip/labwork/shop/model/Order.java index ce86872..d06351c 100644 --- a/src/main/java/ip/labwork/shop/model/Order.java +++ b/src/main/java/ip/labwork/shop/model/Order.java @@ -15,7 +15,7 @@ public class Order { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; - @NotNull(message = "Date can't be null or empty") + //@NotNull(message = "Date can't be null or empty") @Column(name = "date") private Date date; @NotNull(message = "Price can't be null or empty") diff --git a/src/main/java/ip/labwork/shop/model/OrderProducts.java b/src/main/java/ip/labwork/shop/model/OrderProducts.java index 021681a..57ab8bf 100644 --- a/src/main/java/ip/labwork/shop/model/OrderProducts.java +++ b/src/main/java/ip/labwork/shop/model/OrderProducts.java @@ -9,12 +9,12 @@ import jakarta.validation.constraints.NotNull; @Table(name = "order_product") public class OrderProducts { @EmbeddedId - private OrderProductsKey id; - @ManyToOne + private OrderProductsKey id = new OrderProductsKey(); + @ManyToOne(cascade = CascadeType.MERGE) @MapsId("productId") @JoinColumn(name = "product_id") private Product product; - @ManyToOne + @ManyToOne(cascade = CascadeType.MERGE) @MapsId("orderId") @JoinColumn(name = "order_id") @JsonIgnore @@ -28,7 +28,6 @@ public class OrderProducts { public OrderProducts(Order order, Product product, Integer count) { this.order = order; - this.id = new OrderProductsKey(product.getId(), order.getId()); this.id.setOrderId(order.getId()); this.id.setProductId(product.getId()); this.product = product; diff --git a/src/main/java/ip/labwork/shop/service/OrderService.java b/src/main/java/ip/labwork/shop/service/OrderService.java index 113c852..e349513 100644 --- a/src/main/java/ip/labwork/shop/service/OrderService.java +++ b/src/main/java/ip/labwork/shop/service/OrderService.java @@ -34,6 +34,7 @@ public class OrderService { public void addOrderProducts(Order order, Integer[] count, List products){ for (int i = 0; i < products.size(); i++) { final OrderProducts orderProducts = new OrderProducts(order, products.get(i), count[i]); + orderProductRepository.saveAndFlush(orderProducts); order.addProduct(orderProducts); products.get(i).addOrder(orderProducts); orderProductRepository.save(orderProducts); @@ -62,7 +63,7 @@ public class OrderService { return orderRepository.findAll(); } - /*@Transactional + @Transactional public Order updateOrder(Long id, String date, Integer price, Integer[] count, List products) { final Order currentOrder = findOrder(id); currentOrder.setDate(getDate(date)); @@ -81,6 +82,7 @@ public class OrderService { orderProductRepository.save(orderProducts); } else { final OrderProducts orderProducts = new OrderProducts(currentOrder, products.get(i), count[i]); + orderProductRepository.saveAndFlush(orderProducts); currentOrder.addProduct(orderProducts); products.get(i).addOrder(orderProducts); orderProductRepository.save(orderProducts); @@ -92,9 +94,9 @@ public class OrderService { orderProductRepository.delete(orderProductsList.get(i)); } return currentOrder; - }*/ + } - @Transactional + /*@Transactional public Order updateOrder(Long id, String date, Integer price, Integer[] count, List products) { final Order currentOrder = findOrder(id); currentOrder.setDate(getDate(date)); @@ -136,7 +138,7 @@ public class OrderService { } } return currentOrder; - } + }*/ public List getOrderProducts(Order currentOrder){ return orderProductRepository.getOrderProductsByOrderId(currentOrder.getId()); } diff --git a/src/main/java/ip/labwork/shop/service/ProductService.java b/src/main/java/ip/labwork/shop/service/ProductService.java index f148ccf..e34fac7 100644 --- a/src/main/java/ip/labwork/shop/service/ProductService.java +++ b/src/main/java/ip/labwork/shop/service/ProductService.java @@ -33,14 +33,10 @@ public class ProductService { this.componentRepository = componentRepository; } @Transactional - public Product addProduct(String productName, Integer price) { + public Product addProduct(String productName, Integer price, Integer[] count, List components) { final Product product = new Product(productName, price); validatorUtil.validate(product); productRepository.save(product); - return product; - } - @Transactional - public void addProductComponents(Product product, Integer[] count, List components){ for (int i = 0; i < components.size(); i++) { final ProductComponents productComponents = new ProductComponents(components.get(i), product, count[i]); productComponentRepository.saveAndFlush(productComponents); @@ -48,6 +44,7 @@ public class ProductService { components.get(i).addProduct(productComponents); productComponentRepository.saveAndFlush(productComponents); } + return product; } @Transactional(readOnly = true) public Product findProduct(Long id) { @@ -60,7 +57,7 @@ public class ProductService { return productRepository.findAll(); } - /*@Transactional + @Transactional public Product updateProduct(Long id, String productName, Integer price, Integer[] count, List components) { final Product currentProduct = findProduct(id); currentProduct.setProductName(productName); @@ -80,6 +77,7 @@ public class ProductService { } else { final ProductComponents productComponents = new ProductComponents(components.get(i), currentProduct, count[i]); + productComponentRepository.saveAndFlush(productComponents); currentProduct.addComponent(productComponents); components.get(i).addProduct(productComponents); productComponentRepository.save(productComponents); @@ -91,8 +89,8 @@ public class ProductService { productComponentRepository.delete(productComponentsList.get(i)); } return currentProduct; - }*/ - @Transactional + } + /* @Transactional public Product updateProduct(Long id, String productName, Integer price, Integer[] count, List components) { final Product currentProduct = findProduct(id); currentProduct.setProductName(productName); @@ -124,7 +122,7 @@ public class ProductService { final Long currentId = components.get(i).getId(); if (component_id.contains(currentId)) { productComponentsList.remove(productComponentsList.stream().filter(x -> Objects.equals(x.getId().getComponentId(), currentId)).toList().get(0)); - component_id.remove(components.get(i).getId()); + component_id.remove(currentId); } else { final ProductComponents productComponents = new ProductComponents(components.get(i), currentProduct, count[i]); @@ -134,7 +132,7 @@ public class ProductService { } } return currentProduct; - } + }*/ public List getProductComponents(Product currentProduct){ return productComponentRepository.getProductComponentsByProductId(currentProduct.getId()); }
#First nameLast nameИДДата оформленияЦенаПродукты
${student.id}${student.firstName}${student.lastName}
${Order.id}${Order.date}${Order.price}${temp}