lab 4 ComponentService

This commit is contained in:
VictoriaPresnyakova 2023-04-23 22:12:56 +04:00
parent 320927a3b7
commit 8adec16025
4 changed files with 43 additions and 14 deletions

View File

@ -0,0 +1,7 @@
package ru.ulstu.is.sbapp.repair.service;
public class ComponentNotFoundException extends RuntimeException{
public ComponentNotFoundException(Long id) {
super(String.format("Component with id [%s] is not found", id));
}
}

View File

@ -3,21 +3,32 @@ package ru.ulstu.is.sbapp.repair.service;
import org.springframework.beans.factory.annotation.Autowired;
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.repository.ComponentRepository;
import ru.ulstu.is.sbapp.repair.util.validation.ValidatorUtil;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
public class ComponentService {
@PersistenceContext
private EntityManager em;
@Autowired
private final ComponentRepository componentRepository;
@Autowired
private final ValidatorUtil validatorUtil;
public ComponentService(ComponentRepository componentRepository, ValidatorUtil validatorUtil) {
this.componentRepository = componentRepository;
this.validatorUtil = validatorUtil;
}
@Transactional
public Component addComponent(String componentName, Integer amount) {
@ -25,23 +36,19 @@ public class ComponentService {
throw new IllegalArgumentException("Component is null or empty");
}
final Component component = new Component(componentName, amount);
em.persist(component);
return component;
validatorUtil.validate(component);
return componentRepository.save(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;
final Optional <Component> component = componentRepository.findById(id);
return component.orElseThrow(() -> new ComponentNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Component> findAllComponent() {
return em.createQuery("select c from Component c", Component.class)
.getResultList();
return componentRepository.findAll();
}
@Transactional
@ -52,17 +59,18 @@ public class ComponentService {
final Component currentComponent = findComponent(id);
currentComponent.setComponentName(componentName);
currentComponent.setAmount(amount);
return em.merge(currentComponent);
validatorUtil.validate(currentComponent);
return componentRepository.save(currentComponent);
}
@Transactional
public Component deleteComponent(Long id) {
final Component currentComponent = findComponent(id);
em.remove(currentComponent);
componentRepository.delete(currentComponent);
return currentComponent;
}
@Transactional
public void deleteAllComponent() {
em.createQuery("delete from Component").executeUpdate();
componentRepository.deleteAll();
}
}

View File

@ -0,0 +1,7 @@
package ru.ulstu.is.sbapp.repair.service;
public class FavorNotFoundException extends RuntimeException{
public FavorNotFoundException(Long id) {
super(String.format("Favor with id [%s] is not found", id));
}
}

View File

@ -0,0 +1,7 @@
package ru.ulstu.is.sbapp.repair.service;
public class OrderNotFoundException extends RuntimeException{
public OrderNotFoundException(Long id) {
super(String.format("Order with id [%s] is not found", id));
}
}