жесть

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;
public Component() {
favorsList = new ArrayList<>();
}
public Component(String componentName, Integer amount) {
this.componentName = componentName;
this.amount = amount;
favorsList = new ArrayList<>();
}
public Long getId() {
@ -49,6 +52,20 @@ public class Component {
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
public boolean equals(Object o) {

View File

@ -18,22 +18,22 @@ public class Favor {
@Column(name = "price")
private Integer price;
@ManyToMany(mappedBy = "favorsList")
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "favorsList")
private List<Order> ordersList;
@ManyToMany
@JoinTable(name = "favor_component",
joinColumns = @JoinColumn(name = "favor_fk"),
inverseJoinColumns = @JoinColumn(name = "component_fk"))
@ManyToMany //(fetch = FetchType.EAGER)
private List<Component> componentsList;
public Favor() {
componentsList = new ArrayList<>();
ordersList = new ArrayList<>();
}
public Favor(String favorName, Integer price) {
this.favorName = favorName;
this.price = price;
componentsList = new ArrayList<>();
ordersList = new ArrayList<>();
}
public Long getId() {
@ -56,6 +56,23 @@ public class Favor {
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
public boolean equals(Object o) {
@ -65,6 +82,17 @@ public class Favor {
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
public int hashCode() {
return Objects.hash(id);

View File

@ -17,14 +17,13 @@ public class Order {
@Column(name = "date")
private Date date;
@ManyToMany
@ManyToMany (fetch = FetchType.EAGER)
@JoinTable(name = "order_favor",
joinColumns = @JoinColumn(name = "order_fk"),
inverseJoinColumns = @JoinColumn(name = "favor_fk"))
private List<Favor> favorsList;
public Order(){
}
public Order(Date date) {
this.date = date;
@ -52,6 +51,20 @@ public class Order {
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
public int hashCode() {
return Objects.hash(getId(), getDate());

View File

@ -20,11 +20,11 @@ public class ComponentService {
private EntityManager em;
@Transactional
public Component addComponent(String componentName, Integer price) {
if (!StringUtils.hasText(componentName) || price == 0) {
public Component addComponent(String componentName, Integer amount) {
if (!StringUtils.hasText(componentName) || amount == 0) {
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);
return component;
}
@ -58,7 +58,6 @@ public class ComponentService {
@Transactional
public Component deleteComponent(Long id) {
final Component currentComponent = findComponent(id);
//int size = currentComponent.getFavors().size();
em.remove(currentComponent);
return currentComponent;
}

View File

@ -6,6 +6,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
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 javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@ -66,6 +67,21 @@ public class FavorService {
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
public void deleteAllFavor() {
em.createQuery("delete from Favor").executeUpdate();

View File

@ -20,8 +20,8 @@ public class OrderService {
private EntityManager em;
@Transactional
public Order addOrder(String date, Integer price) {
if (!StringUtils.hasText(date) || price == 0) {
public Order addOrder(String date) {
if (!StringUtils.hasText(date)) {
throw new IllegalArgumentException("Order is null or empty");
}
Date correctDate = getDate(date);
@ -52,7 +52,7 @@ public class OrderService {
}
@Transactional(readOnly = true)
public List<Order> findAllOrder() {
public List<Order> findAllOrders() {
return em.createQuery("select o from Order o", Order.class)
.getResultList();
}
@ -74,6 +74,21 @@ public class OrderService {
em.remove(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
public void deleteAllOrder() {
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.springframework.beans.factory.annotation.Autowired;
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 javax.persistence.EntityNotFoundException;
@SpringBootTest
class SbappApplicationTests {
@Autowired
TypeService Service;
private ComponentService componentService;
@Autowired
private FavorService favorService;
@Autowired
private OrderService orderService;
@Test
void testIntPlus() {
final String res = (String) Service.Sum( "100", "10", "int");
Assertions.assertEquals("110", res);
void testOrder(){
componentService.deleteAllComponent();
//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
void testIntMinus() {
final String res = (String)Service.Min( "100", "10", "int");
Assertions.assertEquals("90", res);
}
void testFavor(){
componentService.deleteAllComponent();
orderService.deleteAllOrder();
favorService.deleteAllFavor();
@Test
void testIntMultiply() {
final String res = (String)Service.Mul( "100", "10", "int");
Assertions.assertEquals("1000", res);
}
final Component component = componentService.addComponent("Favor1", 100);
@Test
void testIntDivision() {
final String res = (String)Service.Del( "100", "10", "int");
Assertions.assertEquals("10", res);
}
final Favor favor0 = favorService.addFavor("fvr", 100);
final Favor favor1 = favorService.findFavor(favor0.getId());
Assertions.assertEquals(favor0, favor1);
@Test
void testStringPlus() {
final String res = (String)Service.Sum( "abc", "dfe", "str");
Assertions.assertEquals("abcdfe", res);
}
@Test
void testStringMin() {
final String res = (String)Service.Min( "abcd", "ef", "str");
Assertions.assertEquals("cd", res);
}
favorService.addComponentToFavor(favor0.getId(), component);
Assertions.assertEquals(favorService.findFavor(favor0.getId()).getComponents().size(), 1);
//Assertions.assertEquals(componentService.findComponent(component.getId()).getFavor(), favor0);
@Test
void testStringMul() {
final String res = (String)Service.Mul( "adc", "de", "str");
Assertions.assertEquals("adcdeadcde", res);
}
favorService.deleteFavor(favor0.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> favorService.findFavor(favor0.getId()));
@Test
void testStringDiv() {
final String res = (String)Service.Del( "adcdef", "de", "str");
Assertions.assertEquals("adcf", res);
}
}