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