Работает всё, кроме обновления

This commit is contained in:
Nikita Sergeev 2023-03-16 22:58:22 +04:00
parent 7ae556be81
commit 70a17c4b7c
13 changed files with 635 additions and 39 deletions

View File

@ -0,0 +1,79 @@
package ip.labwork.student.controller;
import ip.labwork.student.model.Order;
import ip.labwork.student.model.Product;
import ip.labwork.student.service.*;
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;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
private final OrderService orderService;
private final ProductService productService;
private final OrderProductsService orderProductsService;
public OrderController(OrderService orderService, ProductService productService, OrderProductsService orderProductsService) {
this.orderService = orderService;
this.productService = productService;
this.orderProductsService = orderProductsService;
}
@GetMapping("/add")
public Order create(@RequestParam("date") String date,
@RequestParam("price") Integer price,
@RequestParam("count") Integer[] count,
@RequestParam("prod") Long[] prod){
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("dd.MM.yyyy");
Date newDate;
try{
newDate = format.parse(date);
}catch (Exception exception){
newDate = new Date();
}
Order order = orderService.addOrder(newDate, price);
for (int i=0; i < prod.length; i++)
orderProductsService.addOrderProducts(order, productService.findProduct(prod[i]), count[i]);
return order;
}
@GetMapping("/update")
public Order update(@RequestParam("id") Long id,
@RequestParam("date") Date date,
@RequestParam("price") Integer price,
@RequestParam("count") Integer[] count,
@RequestParam("prod") Long[] prod){
orderService.updateOrder(id, date, price);
orderProductsService.removeAll(id, prod);
Order order = orderService.findOrder(id);
for(int i = 0; i < prod.length; i++){
orderProductsService.addOrderProducts(order, productService.findProduct(prod[i]),count[i]);
}
return order;
}
@GetMapping("/remove")
public Order remove(@RequestParam("id") Long id){
Order order = orderService.findOrder(id);
orderProductsService.deleteOrder(order);
return orderService.deleteOrder(id);
}
@GetMapping("/removeAll")
public void remove(){
orderProductsService.deleteAllOrder();
orderService.deleteAllOrder();
}
@GetMapping("/find")
public Order find(@RequestParam("id") Long id){
return orderService.findOrder(id);
}
@GetMapping("/findAll")
public List<Order> findAll(){
return orderService.findAllOrder();
}
}

View File

@ -0,0 +1,72 @@
package ip.labwork.student.controller;
import ip.labwork.student.model.Component;
import ip.labwork.student.model.Product;
import ip.labwork.student.model.ProductComponents;
import ip.labwork.student.service.ComponentService;
import ip.labwork.student.service.ProductComponentsService;
import ip.labwork.student.service.ProductService;
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;
import java.util.List;
@RestController
@RequestMapping("/product")
public class ProductController {
private final ProductService productService;
private final ComponentService componentService;
private final ProductComponentsService productComponentsService;
public ProductController(ProductService productService, ComponentService componentService, ProductComponentsService productComponentsService) {
this.productService = productService;
this.componentService = componentService;
this.productComponentsService = productComponentsService;
}
@GetMapping("/add")
public Product create(@RequestParam("name") String name,
@RequestParam("price") Integer price,
@RequestParam("count") Integer[] count,
@RequestParam("comp") Long[] comp){
Product product = productService.addProduct(name,price);
for (int i=0; i < comp.length; i++)
productComponentsService.addProductComponents(product,componentService.findComponent(comp[i]), count[i]);
return product;
}
@GetMapping("/update")
public Product update(@RequestParam("id") Long id,
@RequestParam("name") String name,
@RequestParam("price") Integer price,
@RequestParam("count") Integer[] count,
@RequestParam("comp") Long[] comp){
productService.updateProduct(id, name, price);
productComponentsService.removeAll(id, comp);
Product product = productService.findProduct(id);
for(int i = 0; i < comp.length; i++){
productComponentsService.addProductComponents(product, componentService.findComponent(comp[i]),count[i]);
}
return product;
}
@GetMapping("/remove")
public Product remove(@RequestParam("id") Long id){
Product product = productService.findProduct(id);
productComponentsService.deleteProduct(product);
return productService.deleteProduct(id);
}
@GetMapping("/removeAll")
public void remove(){
productComponentsService.deleteAllProduct();
productService.deleteAllProduct();
}
@GetMapping("/find")
public Product find(@RequestParam("id") Long id){
return productService.findProduct(id);
}
@GetMapping("/findAll")
public List<Product> findAll(){
return productService.findAllProduct();
}
}

View File

@ -3,8 +3,9 @@ package ip.labwork.student.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@Entity
public class Component {
@ -17,7 +18,7 @@ public class Component {
@OneToMany(mappedBy = "component", cascade = CascadeType.ALL)
@JsonIgnore
private Set<ProductComponents> products;
private List<ProductComponents> products;
public Component() {
}
@ -46,14 +47,24 @@ public class Component {
this.price = price;
}
public Set<ProductComponents> getProducts() {
public List<ProductComponents> getProducts() {
return products;
}
public void setProducts(Set<ProductComponents> products) {
public void setProducts(List<ProductComponents> products) {
this.products = products;
}
public void addProduct(ProductComponents productComponents){
if (products == null){
products = new ArrayList<>();
}
if (!products.contains(productComponents))
this.products.add(productComponents);
}
public void removeProduct(ProductComponents productComponents){
if (products.contains(productComponents))
this.products.remove(productComponents);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -0,0 +1,85 @@
package ip.labwork.student.model;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Date date;
private Integer price;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<OrderProducts> products;
public Order(){
}
public Order(Long id, Date date, Integer price, List<OrderProducts> products) {
this.id = id;
this.date = date;
this.price = price;
this.products = products;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public List<OrderProducts> getProducts() {
return products;
}
public void setProducts(List<OrderProducts> products) {
this.products = products;
}
public void addProduct(OrderProducts orderProducts){
if (products == null){
this.products = new ArrayList<>();
}
if (!products.contains(orderProducts))
this.products.add(orderProducts);
}
public void removeProducts(OrderProducts orderProducts){
if (products.contains(orderProducts))
this.products.remove(orderProducts);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Order order)) return false;
return Objects.equals(getId(), order.getId()) && Objects.equals(getDate(), order.getDate()) && Objects.equals(getPrice(), order.getPrice());
}
@Override
public int hashCode() {
return Objects.hash(getId(), getDate(), getPrice());
}
}

View File

@ -0,0 +1,63 @@
package ip.labwork.student.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
@Entity
public class OrderProducts {
@EmbeddedId
private OrderProductsKey id;
@ManyToOne
@MapsId("productId")
@JoinColumn(name = "product_id")
private Product product;
@ManyToOne
@MapsId("orderId")
@JoinColumn(name = "order_id")
@JsonIgnore
private Order order;
private Integer count;
public 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;
this.count = count;
}
public OrderProductsKey getId() {
return id;
}
public void setId(OrderProductsKey id) {
this.id = id;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}

View File

@ -0,0 +1,48 @@
package ip.labwork.student.model;
import jakarta.persistence.Embeddable;
import java.io.Serializable;
import java.util.Objects;
@Embeddable
public class OrderProductsKey implements Serializable {
private Long productId;
private Long orderId;
public OrderProductsKey() {
}
public OrderProductsKey(Long productId, Long orderId) {
this.productId = productId;
this.orderId = orderId;
}
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof OrderProductsKey that)) return false;
return Objects.equals(getProductId(), that.getProductId()) && Objects.equals(getOrderId(), that.getOrderId());
}
@Override
public int hashCode() {
return Objects.hash(getProductId(), getOrderId());
}
}

View File

@ -1,8 +1,11 @@
package ip.labwork.student.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import org.springframework.data.domain.Sort;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@ -14,13 +17,18 @@ public class Product {
private String productName;
private Integer price;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<ProductComponents> components;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
private Set<ProductComponents> components = new HashSet<>();
@JsonIgnore
private List<OrderProducts> orders;
public Product(){
}
public Product(String productName, Integer price, Set<ProductComponents> components) {
public Product(String productName, Integer price, List<ProductComponents> components) {
this.productName = productName;
this.price = price;
this.components = components;
@ -46,11 +54,11 @@ public class Product {
this.price = price;
}
public Set<ProductComponents> getComponents() {
public List<ProductComponents> getComponents() {
return components;
}
public void setComponents(Set<ProductComponents> components) {
public void setComponents(List<ProductComponents> components) {
this.components = components;
}
@ -61,11 +69,37 @@ public class Product {
}
public void addComponent(ProductComponents productComponents){
if (components == null){
this.components = new ArrayList<>();
}
if (!components.contains(productComponents))
this.components.add(productComponents);
}
public void removeComponent(ProductComponents productComponents){
if (components.contains(productComponents))
this.components.remove(productComponents);
}
public List<OrderProducts> getOrders() {
return orders;
}
public void setOrders(List<OrderProducts> orders) {
this.orders = orders;
}
public void addOrder(OrderProducts orderProducts){
if (orders == null){
orders = new ArrayList<>();
}
if (!orders.contains(orderProducts))
this.orders.add(orderProducts);
}
public void removeOrder(OrderProducts orderProducts){
if (orders.contains(orderProducts))
this.orders.remove(orderProducts);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -87,4 +121,5 @@ public class Product {
", price='" + price + '\'' +
'}';
}
}

View File

@ -22,6 +22,7 @@ public class ProductComponents {
public ProductComponents(Component component, Product product, Integer count) {
this.component = component;
this.id = new ProductComponentsKey(product.getId(), component.getId());
this.id.setComponentId(component.getId());
this.id.setProductId(product.getId());
this.product = product;

View File

@ -0,0 +1,77 @@
package ip.labwork.student.service;
import ip.labwork.student.model.*;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class OrderProductsService {
@PersistenceContext
private EntityManager em;
@Transactional
public OrderProducts addOrderProducts(Order order, Product product, Integer count) {
final OrderProducts orderProducts = new OrderProducts(order, product, count);
order.addProduct(orderProducts);
product.addOrder(orderProducts);
em.persist(orderProducts);
return orderProducts;
}
/* @Transactional(readOnly = true)
public Component findProductComponent(Long productId, Long componentId) {
em.find(Product.class, id);
List<ProductComponents> productComponentsList = em.createQuery("select pc from ProductComponents pc", ProductComponents.class)
.getResultList();
final ProductComponentsKey productComponentsKey = new ProductComponentsKey(id,)
final ProductComponents productComponents = em.find(ProductComponents.class, id);
if (component == null) {
throw new EntityNotFoundException(String.format("Component with id [%s] is not found", id));
}
return component;
}*/
@Transactional(readOnly = true)
public List<OrderProducts> findAllOrderProducts(Long id) {
return em.createQuery("select o from OrderProducts o where id.orderId = " + id, OrderProducts.class)
.getResultList();
}
/* @Transactional
public ProductComponents updateProduct(Product product, Component component, Integer Count) {
//final Component currentComponent = fin(id);
currentComponent.setComponentName(ProductName);
currentComponent.setPrice(Count);
return em.merge(currentComponent);
}*/
@Transactional
public void deleteOrder(Order order) {
int size = order.getProducts().size();
for (int i = 0; i < size; i++){
OrderProducts temp = order.getProducts().get(0);
temp.getProduct().removeOrder(temp);
temp.getProduct().removeOrder(temp);
em.remove(temp);
}
}
@Transactional
public void deleteAllOrder() {
em.createQuery("delete from OrderProducts").executeUpdate();
}
@Transactional
public void removeAll(Long id, Long[] prodid) {
/*em.createQuery("delete from ProductComponents p where p.id.productId = " + id + " and p.id.componentId not in "+ compid).executeUpdate();
Product product = em.find(Product.class, id);
product.getComponents().clear();
int s = 5;*/
List<OrderProducts> temp = findAllOrderProducts(id);
for(int i = 0; i < temp.size(); i++){
em.remove(temp.get(i));
}
}
}

View File

@ -0,0 +1,60 @@
package ip.labwork.student.service;
import ip.labwork.student.model.Order;
import ip.labwork.student.model.Product;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
@Service
public class OrderService {
@PersistenceContext
private EntityManager em;
@Transactional
public Order addOrder(Date date, Integer price){
Order order = new Order();
order.setDate(date);
order.setPrice(price);
em.persist(order);
return order;
}
@Transactional(readOnly = true)
public Order findOrder(Long id) {
final Order order = em.find(Order.class, id);
if (order == null) {
throw new EntityNotFoundException(String.format("Order with id [%s] is not found", id));
}
return order;
}
@Transactional(readOnly = true)
public List<Order> findAllOrder() {
return em.createQuery("select o from Order o", Order.class)
.getResultList();
}
@Transactional
public Order updateOrder(Long id, Date date, Integer price) {
final Order currentOrder = findOrder(id);
currentOrder.setDate(date);
currentOrder.setPrice(price);
return em.merge(currentOrder);
}
@Transactional
public Order deleteOrder(Long id) {
final Order currentOrder = findOrder(id);
em.remove(currentOrder);
return currentOrder;
}
@Transactional
public void deleteAllOrder() {
em.createQuery("delete from Order").executeUpdate();
}
}

View File

@ -0,0 +1,83 @@
package ip.labwork.student.service;
import ip.labwork.student.model.Component;
import ip.labwork.student.model.Product;
import ip.labwork.student.model.ProductComponents;
import ip.labwork.student.model.ProductComponentsKey;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Objects;
@Service
public class ProductComponentsService {
@PersistenceContext
private EntityManager em;
@Transactional
public ProductComponents addProductComponents(Product product, Component component, Integer count) {
final ProductComponents productComponents = new ProductComponents(component, product, count);
product.addComponent(productComponents);
component.addProduct(productComponents);
em.persist(productComponents);
return productComponents;
}
/* @Transactional(readOnly = true)
public Component findProductComponent(Long productId, Long componentId) {
em.find(Product.class, id);
List<ProductComponents> productComponentsList = em.createQuery("select pc from ProductComponents pc", ProductComponents.class)
.getResultList();
final ProductComponentsKey productComponentsKey = new ProductComponentsKey(id,)
final ProductComponents productComponents = em.find(ProductComponents.class, id);
if (component == null) {
throw new EntityNotFoundException(String.format("Component with id [%s] is not found", id));
}
return component;
}*/
@Transactional(readOnly = true)
public List<ProductComponents> findAllProductComponents(Long id) {
return em.createQuery("select p from ProductComponents p where id.productId = " + id, ProductComponents.class)
.getResultList();
}
/* @Transactional
public ProductComponents updateProduct(Product product, Component component, Integer Count) {
//final Component currentComponent = fin(id);
currentComponent.setComponentName(ProductName);
currentComponent.setPrice(Count);
return em.merge(currentComponent);
}*/
@Transactional
public void deleteProduct(Product product) {
int size = product.getComponents().size();
for (int i = 0; i < size; i++){
ProductComponents temp = product.getComponents().get(0);
temp.getComponent().removeProduct(temp);
temp.getProduct().removeComponent(temp);
em.remove(temp);
}
}
@Transactional
public void deleteAllProduct() {
em.createQuery("delete from ProductComponents").executeUpdate();
}
@Transactional
public void removeAll(Long id, Long[] compid) {
/*em.createQuery("delete from ProductComponents p where p.id.productId = " + id + " and p.id.componentId not in "+ compid).executeUpdate();
Product product = em.find(Product.class, id);
product.getComponents().clear();
int s = 5;*/
List<ProductComponents> temp = findAllProductComponents(id);
for(int i = 0; i < temp.size(); i++){
em.remove(temp.get(i));
}
}
}

View File

@ -12,8 +12,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class ProductService {
@ -21,25 +19,12 @@ public class ProductService {
private EntityManager em;
@Transactional
public Product addProduct(String productName, Integer price, Set<ProductComponents> components){
Product product = new Product(productName,price,components);
Product newProduct = new Product();
newProduct.setProductName(product.getProductName());
newProduct.setPrice(product.getPrice());
newProduct.getComponents().addAll((product.getComponents()
.stream()
.map(productComponents -> {
Component component = em.find(Component.class, productComponents.getComponent().getId());
ProductComponents newProductComponents = new ProductComponents();
newProductComponents.setComponent(component);
newProductComponents.setProduct(newProduct);
newProductComponents.setCount(productComponents.getCount());
return newProductComponents;
})
.collect(Collectors.toSet())
));
em.persist(newProduct);
return newProduct;
public Product addProduct(String productName, Integer price){
Product product = new Product();
product.setProductName(productName);
product.setPrice(price);
em.persist(product);
return product;
}
@Transactional(readOnly = true)
public Product findProduct(Long id) {
@ -52,15 +37,12 @@ public class ProductService {
@Transactional(readOnly = true)
public List<Product> findAllProduct() {
return em.createQuery("select s from Product s", Product.class)
return em.createQuery("select p from Product p", Product.class)
.getResultList();
}
@Transactional
public Product updateProduct(Long id, String ProductName, Integer Count) {
if (!StringUtils.hasText(ProductName) || Count != 0) {
throw new IllegalArgumentException("Product is null or empty");
}
final Product currentProduct = findProduct(id);
currentProduct.setProductName(ProductName);
currentProduct.setPrice(Count);

View File

@ -22,7 +22,7 @@ public class JpaStudentTests {
ComponentService componentService;
@Autowired
ProductService productService;
@Test
/*@Test
void test(){
Component component = componentService.addComponent("Помидор", 10);
Component component1 = componentService.addComponent("Огурец", 20);
@ -41,5 +41,5 @@ public class JpaStudentTests {
productService.addProduct("Гамбургер", 100, temp);
productService.check();
componentService.check();
}
}*/
}