lab 4 favor service done

This commit is contained in:
VictoriaPresnyakova 2023-04-24 16:48:12 +04:00
parent 72080f1504
commit d3be1156d2
2 changed files with 29 additions and 38 deletions

View File

@ -23,7 +23,7 @@ import java.util.Optional;
public class ComponentService {
// @Autowired
private final ComponentRepository componentRepository;
@Autowired
//@Autowired
private final ValidatorUtil validatorUtil;
public ComponentService(ComponentRepository componentRepository, ValidatorUtil validatorUtil) {

View File

@ -1,86 +1,77 @@
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.controller.FavorDTO;
import ru.ulstu.is.sbapp.repair.model.Component;
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.PersistenceContext;
import javax.persistence.EntityNotFoundException;
import java.util.List;
import java.util.Optional;
@Service
public class FavorService {
@PersistenceContext
private EntityManager em;
private final FavorRepository favorRepository;
private final ValidatorUtil validatorUtil;
public FavorService(FavorRepository favorRepository, ValidatorUtil validatorUtil){
this.favorRepository = favorRepository;
this.validatorUtil = validatorUtil;
}
@Transactional
public Favor addFavor(String favorName, Integer price) {
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);
public Favor addFavor(FavorDTO favorDTO) {
return favor;
final Favor favor = new Favor(favorDTO.getFavorName(), favorDTO.getPrice());
validatorUtil.validate(favor);
return favorRepository.save(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;
final Optional<Favor> favor = favorRepository.findById(id);
return favor.orElseThrow(() -> new FavorNotFoundException(id));
}
@Transactional(readOnly = true)
public List<Favor> findAllFavor() {
return em.createQuery("select p from Favor p", Favor.class)
.getResultList();
return favorRepository.findAll();
}
@Transactional
public Favor updateFavor(Long id, String favorName, Integer price) {
if (!StringUtils.hasText(favorName) || price == 0) {
throw new IllegalArgumentException("Favor name is null or empty");
}
public Favor updateFavor(Long id, FavorDTO favorDTO) {
final Favor currentFavor = findFavor(id);
currentFavor.setFavorName(favorName);
currentFavor.setPrice(price);
em.merge(currentFavor);
return currentFavor;
currentFavor.setFavorName(favorDTO.getFavorName());
currentFavor.setPrice(favorDTO.getPrice());
validatorUtil.validate(currentFavor);
return favorRepository.save(currentFavor);
}
@Transactional
public Favor deleteFavor(Long id) {
final Favor currentFavor = findFavor(id);
em.remove(currentFavor);
favorRepository.delete(currentFavor);
return currentFavor;
}
@Transactional
public Favor addComponentToFavor(Long id, Component component){
final Favor currentFavor = findFavor(id);
if (currentFavor == null){
throw new IllegalArgumentException("Favor with id: " + id + " not found");
}
if (component == null){
throw new IllegalArgumentException("favor not found");
}
public Favor addComponentToFavor(FavorDTO favorDTO, Component component){
final Favor currentFavor = findFavor(favorDTO.getId());
currentFavor.addComponent(component);
//component.addFavor(currentFavor);
em.merge(component);
return em.merge(currentFavor);
return currentFavor;
}
@Transactional
public void deleteAllFavor() {
em.createQuery("delete from Favor").executeUpdate();
favorRepository.deleteAll();
}
}