Anton Romanov 11fc4e46d1
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m47s
#5 -- Show and edit rules
2025-02-19 14:33:29 +04:00

44 lines
1.5 KiB
Java

package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.rule.repository.RuleRepository;
import ru.ulstu.fc.project.service.ProjectService;
import ru.ulstu.fc.rule.model.FuzzyRule;
import ru.ulstu.fc.rule.model.RuleForm;
@Service
public class RuleService {
private final RuleRepository ruleRepository;
private final ProjectService projectService;
public RuleService(RuleRepository ruleRepository, ProjectService projectService) {
this.ruleRepository = ruleRepository;
this.projectService = projectService;
}
public FuzzyRule getById(Integer id) {
return ruleRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Rule not found by id"));
}
public Object save(RuleForm ruleForm) {
if (ruleForm.getId() == null || ruleForm.getId() == 0) {
FuzzyRule rule = new FuzzyRule();
rule.setProject(projectService.getById(ruleForm.getProjectId()));
rule.setContent(ruleForm.getContent());
return ruleRepository.save(rule);
}
FuzzyRule dbRule = getById(ruleForm.getId());
dbRule.setProject(projectService.getById(ruleForm.getProjectId()));
dbRule.setContent(ruleForm.getContent());
return ruleRepository.save(dbRule);
}
public void delete(RuleForm ruleForm) {
getById(ruleForm.getId());
ruleRepository.deleteById(ruleForm.getId());
}
}