all services done
This commit is contained in:
parent
6d3128d1d1
commit
9f1ac3ec31
@ -0,0 +1,89 @@
|
||||
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.FavorComponents;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ComponentService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Component addComponent(String componentName, Integer price) {
|
||||
if (!StringUtils.hasText(componentName) || price == 0) {
|
||||
throw new IllegalArgumentException("Component is null or empty");
|
||||
}
|
||||
final Component component = new Component(componentName, price);
|
||||
em.persist(component);
|
||||
return component;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Component findComponent(Long id) {
|
||||
final Component component = em.find(Component.class, id);
|
||||
if (component == null) {
|
||||
throw new EntityNotFoundException(String.format("Component with id [%s] is not found", id));
|
||||
}
|
||||
return component;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Component> findAllComponent() {
|
||||
return em.createQuery("select c from Component c", Component.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Component> findFiltredComponents(Long[] arr) {
|
||||
if (arr.length == 0) {
|
||||
throw new IllegalArgumentException("Array id is empty");
|
||||
}
|
||||
List<Component> componentList = new ArrayList<>();
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
componentList.add(em.find(Component.class, arr[i]));
|
||||
}
|
||||
return componentList;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Component updateComponent(Long id, String componentName, Integer amount) {
|
||||
if (!StringUtils.hasText(componentName) || amount == 0) {
|
||||
throw new IllegalArgumentException("Component is null or empty");
|
||||
}
|
||||
final Component currentComponent = findComponent(id);
|
||||
currentComponent.setComponentName(componentName);
|
||||
currentComponent.setAmount(amount);
|
||||
return em.merge(currentComponent);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Component deleteComponent(Long id) {
|
||||
final Component currentComponent = findComponent(id);
|
||||
int size = currentComponent.getFavors().size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
FavorComponents temp = currentComponent.getFavors().get(0);
|
||||
temp.getComponent().removeFavor(temp);
|
||||
temp.getFavor().removeComponent(temp);
|
||||
em.remove(temp);
|
||||
}
|
||||
em.remove(currentComponent);
|
||||
return currentComponent;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllComponent() {
|
||||
em.createQuery("delete from FavorComponents").executeUpdate();
|
||||
em.createQuery("delete from Component").executeUpdate();
|
||||
}
|
||||
}
|
131
src/main/java/ru/ulstu/is/sbapp/repair/service/FavorService.java
Normal file
131
src/main/java/ru/ulstu/is/sbapp/repair/service/FavorService.java
Normal file
@ -0,0 +1,131 @@
|
||||
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.Favor;
|
||||
import ru.ulstu.is.sbapp.repair.model.FavorComponents;
|
||||
import ru.ulstu.is.sbapp.repair.model.OrderFavors;
|
||||
|
||||
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 {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Favor addFavor(String favorName, Integer price, Integer[] count, List<Component> components) {
|
||||
if (!StringUtils.hasText(favorName) || price == 0 || count.length == 0 || Arrays.stream(count).filter(c -> c == 0).toList().size() != 0 || components.size() == 0 || components.stream().filter(Objects::isNull).toList().size() != 0 || count.length != components.size()) {
|
||||
throw new IllegalArgumentException("Favor name is null or empty");
|
||||
}
|
||||
final Favor favor = new Favor(favorName, price);
|
||||
em.persist(favor);
|
||||
for (int i = 0; i < components.size(); i++) {
|
||||
final FavorComponents favorComponents = new FavorComponents(components.get(i), favor, count[i]);
|
||||
favor.addComponent(favorComponents);
|
||||
components.get(i).addFavor(favorComponents);
|
||||
em.persist(favorComponents);
|
||||
}
|
||||
return favor;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Favor findFavor(Long id) {
|
||||
final Favor favor = em.find(Favor.class, id);
|
||||
if (favor == null) {
|
||||
throw new EntityNotFoundException(String.format("Favor with id [%s] is not found", id));
|
||||
}
|
||||
return favor;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Favor> findAllFavor() {
|
||||
return em.createQuery("select p from Favor p", Favor.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Favor updateFavor(Long id, String favorName, Integer price, Integer[] count, List<Component> components) {
|
||||
if (!StringUtils.hasText(favorName) || price == 0 || count.length == 0 || Arrays.stream(count).filter(c -> c == 0).toList().size() != 0 || components.size() == 0 || components.stream().filter(Objects::isNull).toList().size() != 0 || count.length != components.size()) {
|
||||
throw new IllegalArgumentException("Favor name is null or empty");
|
||||
}
|
||||
final Favor currentFavor = findFavor(id);
|
||||
currentFavor.setFavorName(favorName);
|
||||
currentFavor.setPrice(price);
|
||||
em.merge(currentFavor);
|
||||
List<FavorComponents> favorComponentsList = em.createQuery("select p from FavorComponents p where p.id.favorId = " + id, FavorComponents.class)
|
||||
.getResultList();
|
||||
List<Long> component_id = new ArrayList<>(favorComponentsList.stream().map(p -> p.getId().getComponentId()).toList());
|
||||
for (int i = 0; i < components.size(); i++) {
|
||||
final Long currentId = components.get(i).getId();
|
||||
if (component_id.contains(currentId)) {
|
||||
final FavorComponents favorComponents = favorComponentsList.stream().filter(x -> Objects.equals(x.getId().getComponentId(), currentId)).toList().get(0);
|
||||
favorComponentsList.remove(favorComponents);
|
||||
component_id.remove(components.get(i).getId());
|
||||
favorComponents.setCount(count[i]);
|
||||
em.merge(favorComponents);
|
||||
} else {
|
||||
final FavorComponents favorComponents = new FavorComponents(components.get(i), currentFavor, count[i]);
|
||||
currentFavor.addComponent(favorComponents);
|
||||
components.get(i).addFavor(favorComponents);
|
||||
em.persist(favorComponents);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < favorComponentsList.size(); i++) {
|
||||
favorComponentsList.get(i).getComponent().removeFavor(favorComponentsList.get(i));
|
||||
favorComponentsList.get(i).getFavor().removeComponent(favorComponentsList.get(i));
|
||||
em.remove(favorComponentsList.get(i));
|
||||
}
|
||||
return currentFavor;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Favor deleteFavor(Long id) {
|
||||
final Favor currentFavor = findFavor(id);
|
||||
int size = currentFavor.getComponents().size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
FavorComponents temp = currentFavor.getComponents().get(0);
|
||||
temp.getComponent().removeFavor(temp);
|
||||
temp.getFavor().removeComponent(temp);
|
||||
em.remove(temp);
|
||||
}
|
||||
int ordSize = currentFavor.getOrders().size();
|
||||
for (int i = 0; i < ordSize; i++){
|
||||
OrderFavors temp = currentFavor.getOrders().get(0);
|
||||
temp.getFavor().removeOrder(temp);
|
||||
temp.getOrder().removeFavors(temp);
|
||||
em.remove(temp);
|
||||
}
|
||||
em.remove(currentFavor);
|
||||
return currentFavor;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllFavor() {
|
||||
em.createQuery("delete from FavorComponents").executeUpdate();
|
||||
em.createQuery("delete from OrderFavors ").executeUpdate();
|
||||
em.createQuery("delete from Favor").executeUpdate();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Favor> findFiltredFavors(Long[] arr) {
|
||||
if (arr.length == 0) {
|
||||
throw new IllegalArgumentException("Array id is empty");
|
||||
}
|
||||
List<Favor> favorList = new ArrayList<>();
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
favorList.add(em.find(Favor.class, arr[i]));
|
||||
}
|
||||
return favorList;
|
||||
}
|
||||
}
|
117
src/main/java/ru/ulstu/is/sbapp/repair/service/OrderService.java
Normal file
117
src/main/java/ru/ulstu/is/sbapp/repair/service/OrderService.java
Normal file
@ -0,0 +1,117 @@
|
||||
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.Favor;
|
||||
import ru.ulstu.is.sbapp.repair.model.Order;
|
||||
import ru.ulstu.is.sbapp.repair.model.OrderFavors;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
|
||||
public class OrderService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Order addOrder(String date, Integer price, Integer[] count, List<Favor> favors) {
|
||||
if (!StringUtils.hasText(date) || price == 0 || count.length == 0 || Arrays.stream(count).filter(c -> c == 0).toList().size() != 0 || favors.size() == 0 || favors.stream().filter(Objects::isNull).toList().size() != 0 || count.length != favors.size()) {
|
||||
throw new IllegalArgumentException("Order is null or empty");
|
||||
}
|
||||
Date correctDate = getDate(date);
|
||||
final Order order = new Order(correctDate);
|
||||
em.persist(order);
|
||||
for (int i = 0; i < favors.size(); i++) {
|
||||
final OrderFavors orderFavor = new OrderFavors(order, favors.get(i));
|
||||
order.addFavor(orderFavor);
|
||||
favors.get(i).addOrder(orderFavor);
|
||||
em.persist(orderFavor);
|
||||
}
|
||||
return order;
|
||||
}
|
||||
|
||||
public Date getDate(String date) {
|
||||
SimpleDateFormat format = new SimpleDateFormat();
|
||||
format.applyPattern("dd.MM.yyyy");
|
||||
Date newDate;
|
||||
try {
|
||||
newDate = format.parse(date);
|
||||
} catch (Exception exception) {
|
||||
newDate = new Date();
|
||||
}
|
||||
return newDate;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Order findOrder(Long id) {
|
||||
final Order order = em.find(Order.class, id);
|
||||
if (order == null) {
|
||||
throw new EntityNotFoundException(String.format("Order with id [%s] is not found", id));
|
||||
}
|
||||
return order;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Order> findAllOrder() {
|
||||
return em.createQuery("select o from Order o", Order.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Order updateOrder(Long id, String date, Integer price, Integer[] count, List<Favor> favors) {
|
||||
if (!StringUtils.hasText(date) || price == 0 || count.length == 0 || Arrays.stream(count).filter(c -> c == 0).toList().size() != 0 || favors.size() == 0 || favors.stream().filter(Objects::isNull).toList().size() != 0 || count.length != favors.size()) {
|
||||
throw new IllegalArgumentException("Order is null or empty");
|
||||
}
|
||||
final Order currentOrder = findOrder(id);
|
||||
currentOrder.setDate(getDate(date));
|
||||
em.merge(currentOrder);
|
||||
List<OrderFavors> orderFavorsList = em.createQuery("select o from OrderFavors o where o.id.orderId = " + id, OrderFavors.class).getResultList();
|
||||
List<Long> favor_id = new ArrayList<>(orderFavorsList.stream().map(p -> p.getId().getFavorId()).toList());
|
||||
for (int i = 0; i < favors.size(); i++) {
|
||||
final Long currentId = favors.get(i).getId();
|
||||
if (favor_id.contains(currentId)) {
|
||||
final OrderFavors orderFavors = orderFavorsList.stream().filter(x -> Objects.equals(x.getId().getFavorId(), currentId)).toList().get(0);
|
||||
orderFavorsList.remove(orderFavors);
|
||||
favor_id.remove(favors.get(i).getId());
|
||||
em.merge(orderFavors);
|
||||
} else {
|
||||
final OrderFavors orderFavors = new OrderFavors(currentOrder, favors.get(i));
|
||||
currentOrder.addFavor(orderFavors);
|
||||
favors.get(i).addOrder(orderFavors);
|
||||
em.persist(orderFavors);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < orderFavorsList.size(); i++) {
|
||||
orderFavorsList.get(i).getFavor().removeOrder(orderFavorsList.get(i));
|
||||
orderFavorsList.get(i).getOrder().removeFavors(orderFavorsList.get(i));
|
||||
em.remove(orderFavorsList.get(i));
|
||||
}
|
||||
return currentOrder;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Order deleteOrder(Long id) {
|
||||
final Order currentOrder = findOrder(id);
|
||||
int size = currentOrder.getFavors().size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
OrderFavors temp = currentOrder.getFavors().get(0);
|
||||
temp.getFavor().removeOrder(temp);
|
||||
temp.getOrder().removeFavors(temp);
|
||||
em.remove(temp);
|
||||
}
|
||||
em.remove(currentOrder);
|
||||
return currentOrder;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllOrder() {
|
||||
em.createQuery("delete from OrderFavors").executeUpdate();
|
||||
em.createQuery("delete from Order").executeUpdate();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user