lab 4 ComponentService
This commit is contained in:
parent
320927a3b7
commit
8adec16025
@ -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));
|
||||||
|
}
|
||||||
|
}
|
@ -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.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
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.repository.ComponentRepository;
|
||||||
|
import ru.ulstu.is.sbapp.repair.util.validation.ValidatorUtil;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityNotFoundException;
|
import javax.persistence.EntityNotFoundException;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ComponentService {
|
public class ComponentService {
|
||||||
@PersistenceContext
|
@Autowired
|
||||||
private EntityManager em;
|
private final ComponentRepository componentRepository;
|
||||||
|
@Autowired
|
||||||
|
private final ValidatorUtil validatorUtil;
|
||||||
|
|
||||||
|
public ComponentService(ComponentRepository componentRepository, ValidatorUtil validatorUtil) {
|
||||||
|
this.componentRepository = componentRepository;
|
||||||
|
this.validatorUtil = validatorUtil;
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Component addComponent(String componentName, Integer amount) {
|
public Component addComponent(String componentName, Integer amount) {
|
||||||
@ -25,23 +36,19 @@ public class ComponentService {
|
|||||||
throw new IllegalArgumentException("Component is null or empty");
|
throw new IllegalArgumentException("Component is null or empty");
|
||||||
}
|
}
|
||||||
final Component component = new Component(componentName, amount);
|
final Component component = new Component(componentName, amount);
|
||||||
em.persist(component);
|
validatorUtil.validate(component);
|
||||||
return component;
|
return componentRepository.save(component);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Component findComponent(Long id) {
|
public Component findComponent(Long id) {
|
||||||
final Component component = em.find(Component.class, id);
|
final Optional <Component> component = componentRepository.findById(id);
|
||||||
if (component == null) {
|
return component.orElseThrow(() -> new ComponentNotFoundException(id));
|
||||||
throw new EntityNotFoundException(String.format("Component with id [%s] is not found", id));
|
|
||||||
}
|
|
||||||
return component;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<Component> findAllComponent() {
|
public List<Component> findAllComponent() {
|
||||||
return em.createQuery("select c from Component c", Component.class)
|
return componentRepository.findAll();
|
||||||
.getResultList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@ -52,17 +59,18 @@ public class ComponentService {
|
|||||||
final Component currentComponent = findComponent(id);
|
final Component currentComponent = findComponent(id);
|
||||||
currentComponent.setComponentName(componentName);
|
currentComponent.setComponentName(componentName);
|
||||||
currentComponent.setAmount(amount);
|
currentComponent.setAmount(amount);
|
||||||
return em.merge(currentComponent);
|
validatorUtil.validate(currentComponent);
|
||||||
|
return componentRepository.save(currentComponent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Component deleteComponent(Long id) {
|
public Component deleteComponent(Long id) {
|
||||||
final Component currentComponent = findComponent(id);
|
final Component currentComponent = findComponent(id);
|
||||||
em.remove(currentComponent);
|
componentRepository.delete(currentComponent);
|
||||||
return currentComponent;
|
return currentComponent;
|
||||||
}
|
}
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAllComponent() {
|
public void deleteAllComponent() {
|
||||||
em.createQuery("delete from Component").executeUpdate();
|
componentRepository.deleteAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user