lab 4 favor service done
This commit is contained in:
parent
72080f1504
commit
d3be1156d2
@ -23,7 +23,7 @@ import java.util.Optional;
|
|||||||
public class ComponentService {
|
public class ComponentService {
|
||||||
// @Autowired
|
// @Autowired
|
||||||
private final ComponentRepository componentRepository;
|
private final ComponentRepository componentRepository;
|
||||||
@Autowired
|
//@Autowired
|
||||||
private final ValidatorUtil validatorUtil;
|
private final ValidatorUtil validatorUtil;
|
||||||
|
|
||||||
public ComponentService(ComponentRepository componentRepository, ValidatorUtil validatorUtil) {
|
public ComponentService(ComponentRepository componentRepository, ValidatorUtil validatorUtil) {
|
||||||
|
@ -1,86 +1,77 @@
|
|||||||
package ru.ulstu.is.sbapp.repair.service;
|
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.controller.FavorDTO;
|
||||||
import ru.ulstu.is.sbapp.repair.model.Component;
|
import ru.ulstu.is.sbapp.repair.model.Component;
|
||||||
import ru.ulstu.is.sbapp.repair.model.Favor;
|
import ru.ulstu.is.sbapp.repair.model.Favor;
|
||||||
|
import ru.ulstu.is.sbapp.repair.repository.FavorRepository;
|
||||||
|
import ru.ulstu.is.sbapp.repair.util.validation.ValidatorUtil;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
import javax.persistence.EntityNotFoundException;
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class FavorService {
|
public class FavorService {
|
||||||
@PersistenceContext
|
private final FavorRepository favorRepository;
|
||||||
private EntityManager em;
|
private final ValidatorUtil validatorUtil;
|
||||||
|
public FavorService(FavorRepository favorRepository, ValidatorUtil validatorUtil){
|
||||||
|
this.favorRepository = favorRepository;
|
||||||
|
this.validatorUtil = validatorUtil;
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Favor addFavor(String favorName, Integer price) {
|
public Favor addFavor(FavorDTO favorDTO) {
|
||||||
if (!StringUtils.hasText(favorName) || price == 0) {
|
|
||||||
throw new IllegalArgumentException("Favor name is null or empty");
|
|
||||||
}
|
|
||||||
final Favor favor = new Favor(favorName, price);
|
|
||||||
em.persist(favor);
|
|
||||||
|
|
||||||
return favor;
|
final Favor favor = new Favor(favorDTO.getFavorName(), favorDTO.getPrice());
|
||||||
|
validatorUtil.validate(favor);
|
||||||
|
return favorRepository.save(favor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Favor findFavor(Long id) {
|
public Favor findFavor(Long id) {
|
||||||
final Favor favor = em.find(Favor.class, id);
|
final Optional<Favor> favor = favorRepository.findById(id);
|
||||||
if (favor == null) {
|
return favor.orElseThrow(() -> new FavorNotFoundException(id));
|
||||||
throw new EntityNotFoundException(String.format("Favor with id [%s] is not found", id));
|
|
||||||
}
|
|
||||||
return favor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<Favor> findAllFavor() {
|
public List<Favor> findAllFavor() {
|
||||||
return em.createQuery("select p from Favor p", Favor.class)
|
return favorRepository.findAll();
|
||||||
.getResultList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Favor updateFavor(Long id, String favorName, Integer price) {
|
public Favor updateFavor(Long id, FavorDTO favorDTO) {
|
||||||
if (!StringUtils.hasText(favorName) || price == 0) {
|
|
||||||
throw new IllegalArgumentException("Favor name is null or empty");
|
|
||||||
}
|
|
||||||
final Favor currentFavor = findFavor(id);
|
final Favor currentFavor = findFavor(id);
|
||||||
currentFavor.setFavorName(favorName);
|
currentFavor.setFavorName(favorDTO.getFavorName());
|
||||||
currentFavor.setPrice(price);
|
currentFavor.setPrice(favorDTO.getPrice());
|
||||||
em.merge(currentFavor);
|
validatorUtil.validate(currentFavor);
|
||||||
return currentFavor;
|
return favorRepository.save(currentFavor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Favor deleteFavor(Long id) {
|
public Favor deleteFavor(Long id) {
|
||||||
final Favor currentFavor = findFavor(id);
|
final Favor currentFavor = findFavor(id);
|
||||||
em.remove(currentFavor);
|
favorRepository.delete(currentFavor);
|
||||||
return currentFavor;
|
return currentFavor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Favor addComponentToFavor(Long id, Component component){
|
public Favor addComponentToFavor(FavorDTO favorDTO, Component component){
|
||||||
final Favor currentFavor = findFavor(id);
|
final Favor currentFavor = findFavor(favorDTO.getId());
|
||||||
if (currentFavor == null){
|
|
||||||
throw new IllegalArgumentException("Favor with id: " + id + " not found");
|
|
||||||
}
|
|
||||||
if (component == null){
|
|
||||||
throw new IllegalArgumentException("favor not found");
|
|
||||||
}
|
|
||||||
currentFavor.addComponent(component);
|
currentFavor.addComponent(component);
|
||||||
//component.addFavor(currentFavor);
|
return currentFavor;
|
||||||
em.merge(component);
|
|
||||||
return em.merge(currentFavor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAllFavor() {
|
public void deleteAllFavor() {
|
||||||
em.createQuery("delete from Favor").executeUpdate();
|
favorRepository.deleteAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user