Anton Romanov d10db93950
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m4s
#17 -- REST CRUD for variables
2025-02-25 16:09:22 +04:00

69 lines
2.3 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.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) {
return variableRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Variable not found by id"));
}
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));
}
}