жесть

This commit is contained in:
VictoriaPresnyakova 2023-04-11 12:25:13 +04:00
parent 0a0ee7145b
commit de7cc57fd4
8 changed files with 147 additions and 52 deletions

BIN
data.mv.db Normal file

Binary file not shown.

View File

@ -22,11 +22,14 @@ public class Component {
private List<Favor> favorsList; private List<Favor> favorsList;
public Component() { public Component() {
favorsList = new ArrayList<>();
} }
public Component(String componentName, Integer amount) { public Component(String componentName, Integer amount) {
this.componentName = componentName; this.componentName = componentName;
this.amount = amount; this.amount = amount;
favorsList = new ArrayList<>();
} }
public Long getId() { public Long getId() {
@ -49,6 +52,20 @@ public class Component {
this.amount = amount; this.amount = amount;
} }
public List<Favor> getFavors() {
return favorsList;
}
public void addFavor(Favor favor) {
if (favorsList == null)
favorsList = new ArrayList<>();
if (!favorsList.contains(favor)){
favorsList.add(favor);
}
if (!favor.getComponents().contains(this)) {
favor.addComponent(this);
}
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {

View File

@ -18,22 +18,22 @@ public class Favor {
@Column(name = "price") @Column(name = "price")
private Integer price; private Integer price;
@ManyToMany(mappedBy = "favorsList") @ManyToMany(fetch = FetchType.EAGER, mappedBy = "favorsList")
private List<Order> ordersList; private List<Order> ordersList;
@ManyToMany @ManyToMany //(fetch = FetchType.EAGER)
@JoinTable(name = "favor_component",
joinColumns = @JoinColumn(name = "favor_fk"),
inverseJoinColumns = @JoinColumn(name = "component_fk"))
private List<Component> componentsList; private List<Component> componentsList;
public Favor() { public Favor() {
componentsList = new ArrayList<>();
ordersList = new ArrayList<>();
} }
public Favor(String favorName, Integer price) { public Favor(String favorName, Integer price) {
this.favorName = favorName; this.favorName = favorName;
this.price = price; this.price = price;
componentsList = new ArrayList<>();
ordersList = new ArrayList<>();
} }
public Long getId() { public Long getId() {
@ -56,6 +56,23 @@ public class Favor {
this.price = price; this.price = price;
} }
public List<Order> getOrders() {
return ordersList;
}
public List<Component> getComponents() {return componentsList;}
public void addOrder(Order order) {
if (ordersList == null)
ordersList = new ArrayList<>();
if (!ordersList.contains(order)){
ordersList.add(order);
}
if (!order.getFavorsList().contains(this)) {
order.addFavor(this);
}
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
@ -65,6 +82,17 @@ public class Favor {
return Objects.equals(id, favor.id); return Objects.equals(id, favor.id);
} }
public void addComponent(Component component){
if (componentsList == null)
componentsList = new ArrayList<>();
if (!componentsList.contains(component)){
componentsList.add(component);
}
if (!component.getFavors().contains(this)) {
component.addFavor(this);
}
}
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id); return Objects.hash(id);

View File

@ -17,14 +17,13 @@ public class Order {
@Column(name = "date") @Column(name = "date")
private Date date; private Date date;
@ManyToMany @ManyToMany (fetch = FetchType.EAGER)
@JoinTable(name = "order_favor", @JoinTable(name = "order_favor",
joinColumns = @JoinColumn(name = "order_fk"), joinColumns = @JoinColumn(name = "order_fk"),
inverseJoinColumns = @JoinColumn(name = "favor_fk")) inverseJoinColumns = @JoinColumn(name = "favor_fk"))
private List<Favor> favorsList; private List<Favor> favorsList;
public Order(){ public Order(){
} }
public Order(Date date) { public Order(Date date) {
this.date = date; this.date = date;
@ -52,6 +51,20 @@ public class Order {
return Objects.equals(getId(), order.getId()) && Objects.equals(getDate(), order.getDate()); return Objects.equals(getId(), order.getId()) && Objects.equals(getDate(), order.getDate());
} }
public List<Favor> getFavorsList() {
return favorsList;
}
public void addFavor(Favor favor){
if (favorsList == null)
favorsList = new ArrayList<>();
if (!favorsList.contains(favor)) {
favorsList.add(favor);
}
favor.addOrder(this);
}
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(getId(), getDate()); return Objects.hash(getId(), getDate());

View File

@ -20,11 +20,11 @@ public class ComponentService {
private EntityManager em; private EntityManager em;
@Transactional @Transactional
public Component addComponent(String componentName, Integer price) { public Component addComponent(String componentName, Integer amount) {
if (!StringUtils.hasText(componentName) || price == 0) { if (!StringUtils.hasText(componentName) || amount == 0) {
throw new IllegalArgumentException("Component is null or empty"); throw new IllegalArgumentException("Component is null or empty");
} }
final Component component = new Component(componentName, price); final Component component = new Component(componentName, amount);
em.persist(component); em.persist(component);
return component; return component;
} }
@ -58,7 +58,6 @@ public class ComponentService {
@Transactional @Transactional
public Component deleteComponent(Long id) { public Component deleteComponent(Long id) {
final Component currentComponent = findComponent(id); final Component currentComponent = findComponent(id);
//int size = currentComponent.getFavors().size();
em.remove(currentComponent); em.remove(currentComponent);
return currentComponent; return currentComponent;
} }

View File

@ -6,6 +6,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import ru.ulstu.is.sbapp.repair.model.Component; import ru.ulstu.is.sbapp.repair.model.Component;
import ru.ulstu.is.sbapp.repair.model.Favor; import ru.ulstu.is.sbapp.repair.model.Favor;
import ru.ulstu.is.sbapp.repair.model.Order;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
@ -66,6 +67,21 @@ public class FavorService {
return currentFavor; return currentFavor;
} }
@Transactional
public Favor addComponentToFavor(Long id, Component component){
final Favor currentFavor = findFavor(id);
if (currentFavor == null){
throw new IllegalArgumentException("Favor with id: " + id + " not found");
}
if (component == null){
throw new IllegalArgumentException("favor not found");
}
currentFavor.addComponent(component);
//component.addFavor(currentFavor);
em.merge(component);
return em.merge(currentFavor);
}
@Transactional @Transactional
public void deleteAllFavor() { public void deleteAllFavor() {
em.createQuery("delete from Favor").executeUpdate(); em.createQuery("delete from Favor").executeUpdate();

View File

@ -20,8 +20,8 @@ public class OrderService {
private EntityManager em; private EntityManager em;
@Transactional @Transactional
public Order addOrder(String date, Integer price) { public Order addOrder(String date) {
if (!StringUtils.hasText(date) || price == 0) { if (!StringUtils.hasText(date)) {
throw new IllegalArgumentException("Order is null or empty"); throw new IllegalArgumentException("Order is null or empty");
} }
Date correctDate = getDate(date); Date correctDate = getDate(date);
@ -52,7 +52,7 @@ public class OrderService {
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Order> findAllOrder() { public List<Order> findAllOrders() {
return em.createQuery("select o from Order o", Order.class) return em.createQuery("select o from Order o", Order.class)
.getResultList(); .getResultList();
} }
@ -74,6 +74,21 @@ public class OrderService {
em.remove(currentOrder); em.remove(currentOrder);
return currentOrder; return currentOrder;
} }
@Transactional
public Order addFavorToOrder(Long id, Favor favor){
final Order currentOrder = findOrder(id);
if (currentOrder == null){
throw new IllegalArgumentException("Order with id: " + id + " not found");
}
if (favor == null){
throw new IllegalArgumentException("favor not found");
}
currentOrder.addFavor(favor);
em.merge(favor);
return em.merge(currentOrder);
}
@Transactional @Transactional
public void deleteAllOrder() { public void deleteAllOrder() {
em.createQuery("delete from Order").executeUpdate(); em.createQuery("delete from Order").executeUpdate();

View File

@ -4,57 +4,64 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.sbapp.repair.model.Component;
import ru.ulstu.is.sbapp.repair.model.Favor;
import ru.ulstu.is.sbapp.repair.model.Order;
import ru.ulstu.is.sbapp.repair.service.ComponentService;
import ru.ulstu.is.sbapp.repair.service.FavorService;
import ru.ulstu.is.sbapp.repair.service.OrderService;
import ru.ulstu.is.sbapp.service.TypeService; import ru.ulstu.is.sbapp.service.TypeService;
import javax.persistence.EntityNotFoundException;
@SpringBootTest @SpringBootTest
class SbappApplicationTests { class SbappApplicationTests {
@Autowired @Autowired
TypeService Service; private ComponentService componentService;
@Autowired
private FavorService favorService;
@Autowired
private OrderService orderService;
@Test @Test
void testIntPlus() { void testOrder(){
final String res = (String) Service.Sum( "100", "10", "int"); componentService.deleteAllComponent();
Assertions.assertEquals("110", res); //orderService.deleteAllOrder();
//favorService.deleteAllFavor(); why?
final Favor favor = favorService.addFavor("Favor1", 100);
final Order order0 = orderService.addOrder("11.02.2023");
final Order order1 = orderService.findOrder(order0.getId());
Assertions.assertEquals(order0, order1);
orderService.addFavorToOrder(order0.getId(), favor);
Assertions.assertEquals(favorService.findFavor(favor.getId()).getOrders().size(), 1);
Assertions.assertEquals(orderService.findOrder(order0.getId()).getFavorsList().size(), 1);
orderService.deleteOrder(order0.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(order0.getId()));
} }
@Test @Test
void testIntMinus() { void testFavor(){
final String res = (String)Service.Min( "100", "10", "int"); componentService.deleteAllComponent();
Assertions.assertEquals("90", res); orderService.deleteAllOrder();
} favorService.deleteAllFavor();
@Test final Component component = componentService.addComponent("Favor1", 100);
void testIntMultiply() {
final String res = (String)Service.Mul( "100", "10", "int");
Assertions.assertEquals("1000", res);
}
@Test final Favor favor0 = favorService.addFavor("fvr", 100);
void testIntDivision() { final Favor favor1 = favorService.findFavor(favor0.getId());
final String res = (String)Service.Del( "100", "10", "int"); Assertions.assertEquals(favor0, favor1);
Assertions.assertEquals("10", res);
}
@Test
void testStringPlus() {
final String res = (String)Service.Sum( "abc", "dfe", "str");
Assertions.assertEquals("abcdfe", res);
}
@Test favorService.addComponentToFavor(favor0.getId(), component);
void testStringMin() { Assertions.assertEquals(favorService.findFavor(favor0.getId()).getComponents().size(), 1);
final String res = (String)Service.Min( "abcd", "ef", "str"); //Assertions.assertEquals(componentService.findComponent(component.getId()).getFavor(), favor0);
Assertions.assertEquals("cd", res);
}
@Test favorService.deleteFavor(favor0.getId());
void testStringMul() { Assertions.assertThrows(EntityNotFoundException.class, () -> favorService.findFavor(favor0.getId()));
final String res = (String)Service.Mul( "adc", "de", "str");
Assertions.assertEquals("adcdeadcde", res);
}
@Test
void testStringDiv() {
final String res = (String)Service.Del( "adcdef", "de", "str");
Assertions.assertEquals("adcf", res);
} }
} }