Anton Romanov efd0cb66c7
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m48s
#2 -- Add async-await
2025-03-03 20:15:30 +04:00

98 lines
3.4 KiB
Java

package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.project.service.ProjectService;
import ru.ulstu.fc.rule.model.FuzzyTerm;
import ru.ulstu.fc.rule.model.Variable;
import ru.ulstu.fc.rule.model.VariableForm;
import ru.ulstu.fc.rule.model.dto.VariableDto;
import ru.ulstu.fc.rule.repository.VariableRepository;
import java.util.List;
@Service
public class VariableService {
private final VariableRepository variableRepository;
private final ProjectService projectService;
public VariableService(VariableRepository variableRepository,
ProjectService projectService) {
this.variableRepository = variableRepository;
this.projectService = projectService;
}
public Variable getById(Integer id) {
Variable variable = variableRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Variable not found by id"));
checkIsCurrentUserVariableWithThrow(variable);
return variable;
}
public Variable save(VariableForm variableForm) {
Variable variable;
if (variableForm.getId() == null || variableForm.getId() == 0) {
variable = new Variable();
} else {
variable = getById(variableForm.getId());
}
variable.setProject(projectService.getById(variableForm.getProjectId()));
variable.setName(variableForm.getName());
variable.setInput(variableForm.isInput());
return variableRepository.save(variable);
}
public Variable save(Variable variable, Integer projectId) {
Variable dbVariable;
if (variable.getId() == null || variable.getId() == 0) {
dbVariable = new Variable();
} else {
dbVariable = getById(variable.getId());
}
dbVariable.setProject(projectService.getById(projectId));
dbVariable.setName(variable.getName());
dbVariable.setInput(variable.isInput());
return variableRepository.save(variable);
}
public void delete(VariableForm ruleForm) {
getById(ruleForm.getId());
variableRepository.deleteById(ruleForm.getId());
}
public void addFuzzyTerm(Integer variableId, FuzzyTerm ft) {
Variable var = getById(variableId);
var.getFuzzyTerms().add(ft);
variableRepository.save(var);
}
public List<Variable> getAllByProject(Integer projectId) {
return variableRepository.getByProject(projectService.getById(projectId));
}
public List<VariableDto> getAllDtoByProject(Integer projectId) {
return variableRepository.getByProject(projectService.getById(projectId))
.stream()
.map(VariableDto::new)
.toList();
}
public void checkIsCurrentUserVariableWithThrow(Variable variable) {
projectService.checkIsCurrentUserProjectWithThrow(variable.getProject());
}
public List<VariableDto> getInputVariablesDtoByProject(Integer projectId) {
return variableRepository.getInputByProject(projectService.getById(projectId))
.stream()
.map(VariableDto::new)
.toList();
}
public List<VariableDto> getOutputVariablesDtoByProject(Integer projectId) {
return variableRepository.getOutputByProject(projectService.getById(projectId))
.stream()
.map(VariableDto::new)
.toList();
}
}