Одобренный вариант, но он мне не нравится
This commit is contained in:
parent
fd8a8e32c6
commit
a25b5454a7
@ -2,30 +2,38 @@ package ip.labwork.student.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
|
||||
@Entity
|
||||
public class Ord {
|
||||
@Table(name="tab_order")
|
||||
public class Order {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Temporal(TemporalType.DATE)
|
||||
private Date CreateDate;
|
||||
private Integer Count;
|
||||
@ManyToMany
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "ords_product",
|
||||
joinColumns = @JoinColumn(name = "ord_fk"),
|
||||
inverseJoinColumns = @JoinColumn(name = "product_fk"))
|
||||
private List<Product> products;
|
||||
public Ord() {
|
||||
public Order() {
|
||||
}
|
||||
|
||||
public Ord(Date CreateDate, Integer Count) {
|
||||
public Order(Date CreateDate, Integer Count) {
|
||||
this.CreateDate = CreateDate;
|
||||
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() {
|
||||
return id;
|
||||
}
|
||||
@ -50,7 +58,7 @@ public class Ord {
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Ord ord = (Ord) o;
|
||||
Order ord = (Order) o;
|
||||
return Objects.equals(id, ord.id);
|
||||
}
|
||||
|
||||
@ -67,4 +75,8 @@ public class Ord {
|
||||
", Count='" + Count + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
public List<Product> getProducts() {
|
||||
return products;
|
||||
}
|
||||
}
|
@ -3,7 +3,6 @@ package ip.labwork.student.model;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -22,7 +21,7 @@ public class Product {
|
||||
private List<Component> components;
|
||||
|
||||
@ManyToMany(mappedBy = "products", fetch = FetchType.EAGER)
|
||||
private List<Ord> ords;
|
||||
private List<Order> ords;
|
||||
public void addComponent(Component component) {
|
||||
if (components == null){
|
||||
components = new ArrayList<>();
|
||||
@ -85,4 +84,22 @@ public class Product {
|
||||
public List<Component> getComponents() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
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.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
@ -8,6 +9,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@Service
|
||||
@ -16,18 +18,21 @@ public class OrdService {
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Ord addOrd(Date CreateDate, Integer Price) {
|
||||
public Order addOrd(Date CreateDate, Integer Price, ArrayList<Product> products) {
|
||||
if (!StringUtils.hasText(CreateDate.toString())) {
|
||||
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);
|
||||
return order;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Ord findOrd(Long id) {
|
||||
final Ord product = em.find(Ord.class, id);
|
||||
public Order findOrd(Long id) {
|
||||
final Order product = em.find(Order.class, id);
|
||||
if (product == null) {
|
||||
throw new EntityNotFoundException(String.format("Ord with id [%s] is not found", id));
|
||||
}
|
||||
@ -35,31 +40,31 @@ public class OrdService {
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Ord> findAllOrd() {
|
||||
return em.createQuery("select s from Ord s", Ord.class)
|
||||
public List<Order> findAllOrd() {
|
||||
return em.createQuery("select s from Order s", Order.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Ord updateOrd(Long id, Date CreateDate, Integer Count) {
|
||||
public Order updateOrd(Long id, Date CreateDate, Integer Count) {
|
||||
if (!StringUtils.hasText(CreateDate.toString())) {
|
||||
throw new IllegalArgumentException("Ord is null or empty");
|
||||
}
|
||||
final Ord currentOrd = findOrd(id);
|
||||
final Order currentOrd = findOrd(id);
|
||||
currentOrd.setCreateDate(CreateDate);
|
||||
currentOrd.setCount(Count);
|
||||
return em.merge(currentOrd);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Ord deleteOrd(Long id) {
|
||||
final Ord currentOrd = findOrd(id);
|
||||
public Order deleteOrd(Long id) {
|
||||
final Order currentOrd = findOrd(id);
|
||||
em.remove(currentOrd);
|
||||
return currentOrd;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllOrd() {
|
||||
em.createQuery("delete from Ord").executeUpdate();
|
||||
em.createQuery("delete from Order").executeUpdate();
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package ip.labwork;
|
||||
|
||||
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.service.ComponentService;
|
||||
import ip.labwork.student.service.OrdService;
|
||||
@ -85,18 +85,16 @@ public class JpaStudentTests {
|
||||
final Product product = productService.addProduct("Бургер", 300, components);
|
||||
log.info(product.toString());
|
||||
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(product1.getComponents().size(), 3);
|
||||
|
||||
|
||||
productService.deleteAllProduct();
|
||||
Assertions.assertEquals(productService.findAllProduct().size(), 0);
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> productService.findProduct(-1L));
|
||||
}
|
||||
|
||||
/*@Test
|
||||
/* @Test
|
||||
void testProductRead() {
|
||||
productService.deleteAllProduct();
|
||||
final Product product = productService.addProduct("Бургер", 300);
|
||||
@ -104,8 +102,8 @@ public class JpaStudentTests {
|
||||
final Product findProduct = productService.findProduct(product.getId());
|
||||
log.info(findProduct.toString());
|
||||
Assertions.assertEquals(product, findProduct);
|
||||
}*/
|
||||
|
||||
}
|
||||
*/
|
||||
@Test
|
||||
void testProductReadNotFound() {
|
||||
productService.deleteAllProduct();
|
||||
@ -129,16 +127,38 @@ public class JpaStudentTests {
|
||||
log.info(products.toString());
|
||||
Assertions.assertEquals(products.size(), 0);
|
||||
}
|
||||
/*
|
||||
@Test
|
||||
void testOrderCreate() {
|
||||
productService.deleteAllProduct();
|
||||
componentService.deleteAllComponent();
|
||||
ordService.deleteAllOrd();
|
||||
final Ord ord = ordService.addOrd(new Date(), 3);
|
||||
log.info(ord.toString());
|
||||
Assertions.assertNotNull(ord.getId());
|
||||
}
|
||||
final Component component1 = componentService.addComponent("Помидор", 3);
|
||||
final Component component2 = componentService.addComponent("Булочка", 2);
|
||||
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() {
|
||||
ordService.deleteAllOrd();
|
||||
final Ord Order = ordService.addOrd(new Date(), 3);
|
||||
@ -146,9 +166,9 @@ public class JpaStudentTests {
|
||||
final Ord findOrder = ordService.findOrd(Order.getId());
|
||||
log.info(findOrder.toString());
|
||||
Assertions.assertEquals(Order, findOrder);
|
||||
}
|
||||
}*/
|
||||
|
||||
@Test
|
||||
/* @Test
|
||||
void testOrderReadNotFound() {
|
||||
ordService.deleteAllOrd();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> ordService.findOrd(-1L));
|
||||
@ -162,13 +182,27 @@ public class JpaStudentTests {
|
||||
final List<Ord> Orders = ordService.findAllOrd();
|
||||
log.info(Orders.toString());
|
||||
Assertions.assertEquals(Orders.size(), 2);
|
||||
}
|
||||
}*/
|
||||
|
||||
@Test
|
||||
void testOrderReadAllEmpty() {
|
||||
productService.deleteAllProduct();
|
||||
componentService.deleteAllComponent();
|
||||
ordService.deleteAllOrd();
|
||||
final List<Ord> Orders = ordService.findAllOrd();
|
||||
log.info(Orders.toString());
|
||||
Assertions.assertEquals(Orders.size(), 0);
|
||||
}*/
|
||||
final Component component1 = componentService.addComponent("Помидор", 3);
|
||||
final Component component2 = componentService.addComponent("Булочка", 2);
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user