lab 4 component service done
This commit is contained in:
parent
b74f213730
commit
72080f1504
@ -7,6 +7,7 @@ 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.ComponentDTO;
|
||||||
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.repository.ComponentRepository;
|
||||||
import ru.ulstu.is.sbapp.repair.util.validation.ValidatorUtil;
|
import ru.ulstu.is.sbapp.repair.util.validation.ValidatorUtil;
|
||||||
@ -20,7 +21,7 @@ import java.util.Optional;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
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;
|
||||||
@ -31,11 +32,8 @@ public class ComponentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Component addComponent(String componentName, Integer amount) {
|
public Component addComponent(ComponentDTO componentDTO) {
|
||||||
if (!StringUtils.hasText(componentName) || amount == 0) {
|
final Component component = new Component(componentDTO.getComponentName(), componentDTO.getAmount());
|
||||||
throw new IllegalArgumentException("Component is null or empty");
|
|
||||||
}
|
|
||||||
final Component component = new Component(componentName, amount);
|
|
||||||
validatorUtil.validate(component);
|
validatorUtil.validate(component);
|
||||||
return componentRepository.save(component);
|
return componentRepository.save(component);
|
||||||
}
|
}
|
||||||
@ -52,13 +50,10 @@ public class ComponentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Component updateComponent(Long id, String componentName, Integer amount) {
|
public Component updateComponent(Long id, ComponentDTO componentDto) {
|
||||||
if (!StringUtils.hasText(componentName) || amount == 0) {
|
|
||||||
throw new IllegalArgumentException("Component is null or empty");
|
|
||||||
}
|
|
||||||
final Component currentComponent = findComponent(id);
|
final Component currentComponent = findComponent(id);
|
||||||
currentComponent.setComponentName(componentName);
|
currentComponent.setComponentName(componentDto.getComponentName());
|
||||||
currentComponent.setAmount(amount);
|
currentComponent.setAmount(componentDto.getAmount());
|
||||||
validatorUtil.validate(currentComponent);
|
validatorUtil.validate(currentComponent);
|
||||||
return componentRepository.save(currentComponent);
|
return componentRepository.save(currentComponent);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user