вернула назад сущности

This commit is contained in:
VictoriaPresnyakova 2023-04-24 15:25:38 +04:00
parent 8adec16025
commit cf22eb790c
14 changed files with 47 additions and 462 deletions

Binary file not shown.

View File

@ -2,11 +2,9 @@ package ru.ulstu.is.sbapp.repair.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.repair.service.Order_FavorService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.Map;
@ -14,18 +12,18 @@ import java.util.Map;
@RestController
public class ReportController {
private final Order_FavorService orderFavorService;
@Autowired
public ReportController(Order_FavorService orderFavorService) {
this.orderFavorService = orderFavorService;
}
@GetMapping("/report")
public Map<String, Integer> GetReport (@RequestParam(value = "from") Object value1,
@RequestParam(value = "to") Object value2) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = formatter.parse(value1.toString());
Date d2 = formatter.parse(value2.toString());
return orderFavorService.makereport(d1, d2);
}
// private final Order_FavorService orderFavorService;
// @Autowired
// public ReportController(Order_FavorService orderFavorService) {
// this.orderFavorService = orderFavorService;
// }
// @GetMapping("/report")
// public Map<String, Integer> GetReport (@RequestParam(value = "from") Object value1,
// @RequestParam(value = "to") Object value2) throws ParseException {
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//
// Date d1 = formatter.parse(value1.toString());
// Date d2 = formatter.parse(value2.toString());
// return orderFavorService.makereport(d1, d2);
// }
}

View File

@ -18,12 +18,12 @@ public class Component {
@Column(name = "amount")
private Integer amount;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "component_fk")
private List<Component_Favor> favorsListFromComponents;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "componentsList", cascade = CascadeType.REMOVE)
private List<Favor> favorsListFromComponents;
public Component() {
favorsListFromComponents = new ArrayList<>();
}
public Component(String componentName, Integer amount) {
@ -52,21 +52,19 @@ public class Component {
this.amount = amount;
}
public List<Component_Favor> getFavors() {
public List<Favor> getFavors() {
return favorsListFromComponents;
}
public void addFavor(Component_Favor favor) {
public void addFavor(Favor favor) {
if (favorsListFromComponents == null)
favorsListFromComponents = new ArrayList<>();
if (!favorsListFromComponents.contains(favor)){
favorsListFromComponents.add(favor);
}
/*
if (!favor.getComponents().contains(this)) {
favor.addComponent(this);
}*/
}
}
@Override
@ -90,4 +88,4 @@ public class Component {
", amount='" + amount + '\'' +
'}';
}
}
}

View File

@ -1,68 +0,0 @@
package ru.ulstu.is.sbapp.repair.model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
@Table(name = "components_favors")
public class Component_Favor {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "amount")
private Integer amount;
public Component_Favor() {
}
public Component_Favor(Integer amount) {
this.amount = amount;
}
public Long getId() {
return id;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Component_Favor cf = (Component_Favor) o;
return Objects.equals(id, cf.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);
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
'}';
}
}

View File

@ -20,14 +20,12 @@ public class Favor {
@Column(name = "price")
private Integer price;
@OneToMany (cascade = CascadeType.ALL)
@JoinColumn(name = "favor_fk")
private List<Order_Favor> ordersList;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
private List<Order> ordersList;
@OneToMany (cascade = CascadeType.ALL)
@ManyToMany(cascade = CascadeType.REMOVE)
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name = "favor_fk")
private List<Component_Favor> componentsList;
private List<Component> componentsList;
public Favor() {
componentsList = new ArrayList<>();
@ -61,31 +59,21 @@ public class Favor {
this.price = price;
}
public List<Order_Favor> getOrders() {
public List<Order> getOrders() {
return ordersList;
}
public List<Component_Favor> getComponents() {return componentsList;}
public List<Component> getComponents() {return componentsList;}
public void addOrder(Order_Favor of) {
public void addOrder(Order order) {
if (ordersList == null)
ordersList = new ArrayList<>();
if (!ordersList.contains(of)){
ordersList.add(of);
of.setFavor_id(this);
if (!ordersList.contains(order)){
ordersList.add(order);
}
}
public void addCF(Component_Favor cf) {
if (componentsList == null)
componentsList = new ArrayList<>();
if (!componentsList.contains(cf)){
componentsList.add(cf);
}
/*
if (!order.getFavorsList().contains(this)) {
order.addFavor(this);
}*/
}
}
@ -97,7 +85,6 @@ public class Favor {
return Objects.equals(id, favor.id);
}
/*
public void addComponent(Component component){
if (componentsList == null)
componentsList = new ArrayList<>();
@ -108,7 +95,7 @@ public class Favor {
component.addFavor(this);
}
}
*/
@Override
public int hashCode() {
return Objects.hash(id);

View File

@ -17,9 +17,8 @@ public class Order {
@Column(name = "date")
private Date date;
@OneToMany (cascade = CascadeType.ALL)
@JoinColumn(name = "order_fk")
private List<Order_Favor> favorsList;
@ManyToMany (fetch = FetchType.EAGER, mappedBy = "ordersList", cascade = CascadeType.REMOVE)
private List<Favor> favorsList;
public Order(){
}
@ -49,18 +48,17 @@ public class Order {
return Objects.equals(getId(), order.getId()) && Objects.equals(getDate(), order.getDate());
}
public List<Order_Favor> getFavorsList() {
public List<Favor> getFavorsList() {
return favorsList;
}
public void addFavor(Order_Favor of){
public void addFavor(Favor favor){
if (favorsList == null)
favorsList = new ArrayList<>();
if (!favorsList.contains(of)) {
favorsList.add(of);
of.setOrder_id(this);
if (!favorsList.contains(favor)) {
favorsList.add(favor);
}
//favor.addOrder(this);
favor.addOrder(this);
}

View File

@ -1,89 +0,0 @@
package ru.ulstu.is.sbapp.repair.model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
@Table(name = "orders_favors")
public class Order_Favor {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "amount")
private Integer amount;
@ManyToOne
private Order Order_id;
@ManyToOne
private Favor Favor_id;
public Order_Favor() {
}
public Order_Favor(Integer amount) {
this.amount = amount;
}
public Long getId() {
return id;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public Favor getFavor_id() {
return Favor_id;
}
public void setFavor_id(Favor favor_id) {
Favor_id = favor_id;
}
public Order getOrder_id() {
return Order_id;
}
public void setOrder_id(Order order_id) {
Order_id = order_id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Order_Favor of = (Order_Favor) o;
return Objects.equals(id, of.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);
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
'}';
}
}

View File

@ -1,7 +0,0 @@
package ru.ulstu.is.sbapp.repair.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.is.sbapp.repair.model.Component_Favor;
public interface Component_FavorRepository extends JpaRepository<Component_Favor, Long> {
}

View File

@ -1,7 +0,0 @@
package ru.ulstu.is.sbapp.repair.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.is.sbapp.repair.model.Order_Favor;
public interface Order_FavorRepository extends JpaRepository<Order_Favor, Long> {
}

View File

@ -1,65 +0,0 @@
package ru.ulstu.is.sbapp.repair.service;
import org.springframework.stereotype.Service;
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.Component_Favor;
import ru.ulstu.is.sbapp.repair.model.Favor;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import java.util.List;
@Service
public class Component_FavorService {
@PersistenceContext
private EntityManager em;
@Transactional
public Component_Favor addComponent_Favor(Integer amount) {
if (amount == 0) {
throw new IllegalArgumentException("amount cant be 0");
}
final Component_Favor cf = new Component_Favor(amount);
em.persist(cf);
return cf;
}
@Transactional(readOnly = true)
public Component_Favor findComponent_Favor(Long id) {
final Component_Favor cf = em.find(Component_Favor.class, id);
if (cf == null) {
throw new EntityNotFoundException(String.format("cf with id [%s] is not found", id));
}
return cf;
}
@Transactional(readOnly = true)
public List<Component_Favor> findAllComponentFavors() {
return em.createQuery("select p from Component_Favor p", Component_Favor.class).getResultList();
}
@Transactional
public Component_Favor updateComponentFavor(Long id, Integer amount) {
if (amount == 0) {
throw new IllegalArgumentException("amount = 0");
}
final Component_Favor cf = findComponent_Favor(id);
cf.setAmount(amount);
em.merge(cf);
return cf;
}
@Transactional
public Component_Favor deleteFavor(Long id) {
final Component_Favor cf = findComponent_Favor(id);
em.remove(cf);
return cf;
}
@Transactional
public void deleteAllComponentFavor() {
em.createQuery("delete from Component_Favor ").executeUpdate();
}
}

View File

@ -5,18 +5,13 @@ import org.springframework.stereotype.Service;
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.Component_Favor;
import ru.ulstu.is.sbapp.repair.model.Favor;
import ru.ulstu.is.sbapp.repair.model.Order;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@Service
public class FavorService {
@ -69,7 +64,7 @@ public class FavorService {
}
@Transactional
public Favor addComponentToFavor(Long id, Component component, Component_Favor cf){
public Favor addComponentToFavor(Long id, Component component){
final Favor currentFavor = findFavor(id);
if (currentFavor == null){
throw new IllegalArgumentException("Favor with id: " + id + " not found");
@ -77,13 +72,8 @@ public class FavorService {
if (component == null){
throw new IllegalArgumentException("favor not found");
}
//currentFavor.addComponent(component);
currentFavor.addComponent(component);
//component.addFavor(currentFavor);
//final Component_Favor cf = new Component_Favor(amount);
currentFavor.addCF(cf);
component.addFavor(cf);
em.merge(component);
return em.merge(currentFavor);
}

View File

@ -6,7 +6,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.sbapp.repair.model.Favor;
import ru.ulstu.is.sbapp.repair.model.Order;
import ru.ulstu.is.sbapp.repair.model.Order_Favor;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@ -77,7 +76,7 @@ public class OrderService {
}
@Transactional
public Order addFavorToOrder(Long id, Favor favor, Order_Favor of){
public Order addFavorToOrder(Long id, Favor favor){
final Order currentOrder = findOrder(id);
if (currentOrder == null){
throw new IllegalArgumentException("Order with id: " + id + " not found");
@ -85,8 +84,7 @@ public class OrderService {
if (favor == null){
throw new IllegalArgumentException("favor not found");
}
currentOrder.addFavor(of);
favor.addOrder(of);
currentOrder.addFavor(favor);
em.merge(favor);
return em.merge(currentOrder);
}

View File

@ -1,80 +0,0 @@
package ru.ulstu.is.sbapp.repair.service;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.is.sbapp.repair.model.Component_Favor;
import ru.ulstu.is.sbapp.repair.model.Order_Favor;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Tuple;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class Order_FavorService {
@PersistenceContext
private EntityManager em;
@Transactional
public Order_Favor addOrder_Favor(Integer amount) {
if (amount == 0) {
throw new IllegalArgumentException("amount cant be 0");
}
final Order_Favor cf = new Order_Favor(amount);
em.persist(cf);
return cf;
}
@Transactional(readOnly = true)
public Order_Favor findOrder_Favor(Long id) {
final Order_Favor cf = em.find(Order_Favor.class, id);
if (cf == null) {
throw new EntityNotFoundException(String.format("cf with id [%s] is not found", id));
}
return cf;
}
@Transactional(readOnly = true)
public List<Order_Favor> findAllOrderFavors() {
return em.createQuery("select p from Order_Favor p", Order_Favor.class).getResultList();
}
@Transactional
public Order_Favor updateOrderFavor(Long id, Integer amount) {
if (amount == 0) {
throw new IllegalArgumentException("amount = 0");
}
final Order_Favor cf = findOrder_Favor(id);
cf.setAmount(amount);
em.merge(cf);
return cf;
}
@Transactional
public Order_Favor deleteFavor(Long id) {
final Order_Favor cf = findOrder_Favor(id);
em.remove(cf);
return cf;
}
@Transactional
public void deleteAllComponentFavor() {
em.createQuery("delete from Order_Favor ").executeUpdate();
}
@Transactional
public Map<String, Integer> makereport(Date d1, Date d2){
Map<String, Integer> answ = em.createQuery("SELECT f.favorName as n, sum(off.amount) * f.price as s from Order_Favor off join off.Favor_id f join off.Order_id o where o.date > :date1 and o.date < :date2 group by f.favorName", Tuple.class).setParameter("date1", d1).setParameter("date2", d2).getResultStream().collect(Collectors.toMap(tuple -> ((String)tuple.get("n")).toString(), tuple -> ((Number) tuple.get("s")).intValue()));
return answ;
}
}

View File

@ -18,53 +18,9 @@ class SbappApplicationTests {
private FavorService favorService;
@Autowired
private OrderService orderService;
@Autowired
private Component_FavorService cfService;
@Autowired
private Order_FavorService ofService;
@Test
void testReport(){
ofService.deleteAllComponentFavor();
cfService.deleteAllComponentFavor();
componentService.deleteAllComponent();
orderService.deleteAllOrder();
favorService.deleteAllFavor();
final Favor favor = favorService.addFavor("Favor1", 100);
final Favor favor1 = favorService.addFavor("Favor2", 200);
final Favor favor2 = favorService.addFavor("Favor3", 300);
final Order order0 = orderService.addOrder("11.02.2023");
final Order order1 = orderService.addOrder("12.02.2023");
final Order order2 = orderService.addOrder("13.02.2023");
final Order_Favor of = ofService.addOrder_Favor(10);
final Order_Favor of1 = ofService.addOrder_Favor(11);
final Order_Favor of2 = ofService.addOrder_Favor(12);
final Order_Favor of3 = ofService.addOrder_Favor(13);
orderService.addFavorToOrder(order0.getId(), favor, of);
orderService.addFavorToOrder(order0.getId(), favor1, of1);
orderService.addFavorToOrder(order1.getId(), favor, of2);
orderService.addFavorToOrder(order1.getId(), favor2, of3);
//final List<Order_Favor> lst = ofService.makereport();
//Assertions.assertEquals(lst.get(0).getAmount(), 10);
}
@Test
void testOrder(){
cfService.deleteAllComponentFavor();
ofService.deleteAllComponentFavor();
componentService.deleteAllComponent();
orderService.deleteAllOrder();
favorService.deleteAllFavor();
@ -73,13 +29,11 @@ class SbappApplicationTests {
final Order order0 = orderService.addOrder("11.02.2023");
final Order order1 = orderService.findOrder(order0.getId());
final Component component1 = componentService.addComponent("comp", 10);
favor.addComponent(component1);
Assertions.assertEquals(order0, order1);
final Order_Favor of = ofService.addOrder_Favor(10);
orderService.addFavorToOrder(order0.getId(), favor, of);
orderService.addFavorToOrder(order0.getId(), favor);
Assertions.assertEquals(favorService.findFavor(favor.getId()).getOrders().size(), 1);
Assertions.assertEquals(orderService.findOrder(order0.getId()).getFavorsList().size(), 1);
@ -87,26 +41,20 @@ class SbappApplicationTests {
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(order0.getId()));
}
@Test
void testFavor(){
cfService.deleteAllComponentFavor();
ofService.deleteAllComponentFavor();
componentService.deleteAllComponent();
orderService.deleteAllOrder();
favorService.deleteAllFavor();
final Component component = componentService.addComponent("Favor1", 100);
final Favor favor0 = favorService.addFavor("fvr", 100);
final Favor favor1 = favorService.findFavor(favor0.getId());
Assertions.assertEquals(favor0, favor1);
final Component_Favor cf = cfService.addComponent_Favor(10);
favorService.addComponentToFavor(favor0.getId(), component, cf);
favorService.addComponentToFavor(favor0.getId(), component);
Assertions.assertEquals(favorService.findFavor(favor0.getId()).getComponents().size(), 1);
Assertions.assertEquals(componentService.findComponent(component.getId()).getFavors().size(), 1);
@ -114,20 +62,4 @@ class SbappApplicationTests {
Assertions.assertThrows(EntityNotFoundException.class, () -> favorService.findFavor(favor0.getId()));
}
@Test
void testComponent(){
cfService.deleteAllComponentFavor();
ofService.deleteAllComponentFavor();
favorService.deleteAllFavor();
componentService.deleteAllComponent();
orderService.deleteAllOrder();
final Component component0 = componentService.addComponent("comp", 111);
final Component component1 = componentService.findComponent(component0.getId());
Assertions.assertEquals(component0, component1);
componentService.deleteComponent(component0.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> componentService.findComponent(component0.getId()));
}
}