Одобренный вариант, но он мне не нравится

This commit is contained in:
Katerina881 2023-03-13 18:01:25 +04:00
parent fd8a8e32c6
commit a25b5454a7
4 changed files with 114 additions and 46 deletions

View File

@ -2,30 +2,38 @@ package ip.labwork.student.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.util.Date; import java.util.*;
import java.util.List;
import java.util.Objects;
@Entity @Entity
public class Ord { @Table(name="tab_order")
public class Order {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@Temporal(TemporalType.DATE) @Temporal(TemporalType.DATE)
private Date CreateDate; private Date CreateDate;
private Integer Count; private Integer Count;
@ManyToMany @ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "ords_product", @JoinTable(name = "ords_product",
joinColumns = @JoinColumn(name = "ord_fk"), joinColumns = @JoinColumn(name = "ord_fk"),
inverseJoinColumns = @JoinColumn(name = "product_fk")) inverseJoinColumns = @JoinColumn(name = "product_fk"))
private List<Product> products; private List<Product> products;
public Ord() { public Order() {
} }
public Ord(Date CreateDate, Integer Count) { public Order(Date CreateDate, Integer Count) {
this.CreateDate = CreateDate; this.CreateDate = CreateDate;
this.Count = Count; this.Count = Count;
} }
public void addProduct(Product product) {
if (products == null){
products = new ArrayList<>();
}
this.products.add(product);
if (product.getOrder() == null) {
product.setOrder(this);
}
}
public Long getId() { public Long getId() {
return id; return id;
} }
@ -50,7 +58,7 @@ public class Ord {
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
Ord ord = (Ord) o; Order ord = (Order) o;
return Objects.equals(id, ord.id); return Objects.equals(id, ord.id);
} }
@ -67,4 +75,8 @@ public class Ord {
", Count='" + Count + '\'' + ", Count='" + Count + '\'' +
'}'; '}';
} }
public List<Product> getProducts() {
return products;
}
} }

View File

@ -3,7 +3,6 @@ package ip.labwork.student.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -22,7 +21,7 @@ public class Product {
private List<Component> components; private List<Component> components;
@ManyToMany(mappedBy = "products", fetch = FetchType.EAGER) @ManyToMany(mappedBy = "products", fetch = FetchType.EAGER)
private List<Ord> ords; private List<Order> ords;
public void addComponent(Component component) { public void addComponent(Component component) {
if (components == null){ if (components == null){
components = new ArrayList<>(); components = new ArrayList<>();
@ -85,4 +84,22 @@ public class Product {
public List<Component> getComponents() { public List<Component> getComponents() {
return components; return components;
} }
public List<Order> getOrder() {
/*if(products.contains(product)){
return true;
}else{
return false;
}*/
return ords;
}
public void setOrder(Order order) {
if (ords == null){
ords = new ArrayList<>();
}
this.ords.add(order);
if (!order.getProducts().contains(this)) { // warning this may cause performance issues if you have a large data set since this operation is O(n)
order.getProducts().add(this);
}
}
} }

View File

@ -1,6 +1,7 @@
package ip.labwork.student.service; package ip.labwork.student.service;
import ip.labwork.student.model.Ord; import ip.labwork.student.model.Order;
import ip.labwork.student.model.Product;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
@ -8,6 +9,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@Service @Service
@ -16,18 +18,21 @@ public class OrdService {
private EntityManager em; private EntityManager em;
@Transactional @Transactional
public Ord addOrd(Date CreateDate, Integer Price) { public Order addOrd(Date CreateDate, Integer Price, ArrayList<Product> products) {
if (!StringUtils.hasText(CreateDate.toString())) { if (!StringUtils.hasText(CreateDate.toString())) {
throw new IllegalArgumentException("Ord is null or empty"); throw new IllegalArgumentException("Ord is null or empty");
} }
final Ord order = new Ord(new Date(), Price); final Order order = new Order(new Date(), Price);
for (int i = 0 ; i < products.size(); i++){
order.addProduct(products.get(i));
}
em.persist(order); em.persist(order);
return order; return order;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Ord findOrd(Long id) { public Order findOrd(Long id) {
final Ord product = em.find(Ord.class, id); final Order product = em.find(Order.class, id);
if (product == null) { if (product == null) {
throw new EntityNotFoundException(String.format("Ord with id [%s] is not found", id)); throw new EntityNotFoundException(String.format("Ord with id [%s] is not found", id));
} }
@ -35,31 +40,31 @@ public class OrdService {
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Ord> findAllOrd() { public List<Order> findAllOrd() {
return em.createQuery("select s from Ord s", Ord.class) return em.createQuery("select s from Order s", Order.class)
.getResultList(); .getResultList();
} }
@Transactional @Transactional
public Ord updateOrd(Long id, Date CreateDate, Integer Count) { public Order updateOrd(Long id, Date CreateDate, Integer Count) {
if (!StringUtils.hasText(CreateDate.toString())) { if (!StringUtils.hasText(CreateDate.toString())) {
throw new IllegalArgumentException("Ord is null or empty"); throw new IllegalArgumentException("Ord is null or empty");
} }
final Ord currentOrd = findOrd(id); final Order currentOrd = findOrd(id);
currentOrd.setCreateDate(CreateDate); currentOrd.setCreateDate(CreateDate);
currentOrd.setCount(Count); currentOrd.setCount(Count);
return em.merge(currentOrd); return em.merge(currentOrd);
} }
@Transactional @Transactional
public Ord deleteOrd(Long id) { public Order deleteOrd(Long id) {
final Ord currentOrd = findOrd(id); final Order currentOrd = findOrd(id);
em.remove(currentOrd); em.remove(currentOrd);
return currentOrd; return currentOrd;
} }
@Transactional @Transactional
public void deleteAllOrd() { public void deleteAllOrd() {
em.createQuery("delete from Ord").executeUpdate(); em.createQuery("delete from Order").executeUpdate();
} }
} }

View File

@ -1,7 +1,7 @@
package ip.labwork; package ip.labwork;
import ip.labwork.student.model.Component; import ip.labwork.student.model.Component;
import ip.labwork.student.model.Ord; import ip.labwork.student.model.Order;
import ip.labwork.student.model.Product; import ip.labwork.student.model.Product;
import ip.labwork.student.service.ComponentService; import ip.labwork.student.service.ComponentService;
import ip.labwork.student.service.OrdService; import ip.labwork.student.service.OrdService;
@ -85,14 +85,12 @@ public class JpaStudentTests {
final Product product = productService.addProduct("Бургер", 300, components); final Product product = productService.addProduct("Бургер", 300, components);
log.info(product.toString()); log.info(product.toString());
final Product product1 = productService.findProduct(product.getId()); final Product product1 = productService.findProduct(product.getId());
Assertions.assertEquals(true,product1.getComponents().contains(component1));
Assertions.assertTrue(product1.getComponents().contains(component1));
Assertions.assertEquals(product,product1); Assertions.assertEquals(product,product1);
Assertions.assertEquals(product1.getComponents().size(), 3);
productService.deleteAllProduct(); productService.deleteAllProduct();
Assertions.assertEquals(productService.findAllProduct().size(), 0);
Assertions.assertThrows(EntityNotFoundException.class, () -> productService.findProduct(-1L)); Assertions.assertThrows(EntityNotFoundException.class, () -> productService.findProduct(-1L));
} }
@ -104,8 +102,8 @@ public class JpaStudentTests {
final Product findProduct = productService.findProduct(product.getId()); final Product findProduct = productService.findProduct(product.getId());
log.info(findProduct.toString()); log.info(findProduct.toString());
Assertions.assertEquals(product, findProduct); Assertions.assertEquals(product, findProduct);
}*/ }
*/
@Test @Test
void testProductReadNotFound() { void testProductReadNotFound() {
productService.deleteAllProduct(); productService.deleteAllProduct();
@ -129,16 +127,38 @@ public class JpaStudentTests {
log.info(products.toString()); log.info(products.toString());
Assertions.assertEquals(products.size(), 0); Assertions.assertEquals(products.size(), 0);
} }
/*
@Test @Test
void testOrderCreate() { void testOrderCreate() {
productService.deleteAllProduct();
componentService.deleteAllComponent();
ordService.deleteAllOrd(); ordService.deleteAllOrd();
final Ord ord = ordService.addOrd(new Date(), 3); final Component component1 = componentService.addComponent("Помидор", 3);
log.info(ord.toString()); final Component component2 = componentService.addComponent("Булочка", 2);
Assertions.assertNotNull(ord.getId()); final Component component3 = componentService.addComponent("Огурец", 3);
} ArrayList<Component> components = new ArrayList<>();
@Test components.add(component1);
components.add(component2);
components.add(component3);
final Product product = productService.addProduct("Бургер", 300, components);
ArrayList<Product> products = new ArrayList<>();
products.add(product);
final Order ord = ordService.addOrd(new Date(), 3, products);
ordService.addOrd(new Date(), 4,products);
ordService.addOrd(new Date(), 5,products);
ordService.addOrd(new Date(), 6,products);
log.info(ord.toString());
Assertions.assertEquals(3, ordService.findOrd(ord.getId()).getProducts().get(0).getComponents().size());
Assertions.assertEquals(1, ordService.findOrd(ord.getId()).getProducts().size());
Assertions.assertEquals(4, ordService.findAllOrd().size());
final Order ord1 = ordService.findOrd(ord.getId());
Assertions.assertEquals(ord1, ord);
ordService.deleteAllOrd();
Assertions.assertThrows(EntityNotFoundException.class, () -> ordService.findOrd(-1L));
Assertions.assertEquals(0, ordService.findAllOrd().size());
}
/*@Test
void testOrderRead() { void testOrderRead() {
ordService.deleteAllOrd(); ordService.deleteAllOrd();
final Ord Order = ordService.addOrd(new Date(), 3); final Ord Order = ordService.addOrd(new Date(), 3);
@ -146,9 +166,9 @@ public class JpaStudentTests {
final Ord findOrder = ordService.findOrd(Order.getId()); final Ord findOrder = ordService.findOrd(Order.getId());
log.info(findOrder.toString()); log.info(findOrder.toString());
Assertions.assertEquals(Order, findOrder); Assertions.assertEquals(Order, findOrder);
} }*/
@Test /* @Test
void testOrderReadNotFound() { void testOrderReadNotFound() {
ordService.deleteAllOrd(); ordService.deleteAllOrd();
Assertions.assertThrows(EntityNotFoundException.class, () -> ordService.findOrd(-1L)); Assertions.assertThrows(EntityNotFoundException.class, () -> ordService.findOrd(-1L));
@ -162,13 +182,27 @@ public class JpaStudentTests {
final List<Ord> Orders = ordService.findAllOrd(); final List<Ord> Orders = ordService.findAllOrd();
log.info(Orders.toString()); log.info(Orders.toString());
Assertions.assertEquals(Orders.size(), 2); Assertions.assertEquals(Orders.size(), 2);
} }*/
@Test @Test
void testOrderReadAllEmpty() { void testOrderReadAllEmpty() {
productService.deleteAllProduct();
componentService.deleteAllComponent();
ordService.deleteAllOrd(); ordService.deleteAllOrd();
final List<Ord> Orders = ordService.findAllOrd(); final Component component1 = componentService.addComponent("Помидор", 3);
log.info(Orders.toString()); final Component component2 = componentService.addComponent("Булочка", 2);
Assertions.assertEquals(Orders.size(), 0); final Component component3 = componentService.addComponent("Огурец", 3);
}*/ ArrayList<Component> components = new ArrayList<>();
components.add(component1);
components.add(component2);
components.add(component3);
final Product product = productService.addProduct("Бургер", 300, components);
ArrayList<Product> products = new ArrayList<>();
products.add(product);
final Order ord = ordService.addOrd(new Date(), 3, products);
Assertions.assertEquals(ordService.findAllOrd().size(), 1);
ordService.deleteOrd(ord.getId());
Assertions.assertEquals(ordService.findAllOrd().size(), 0);
}
} }