fuzzy-controller/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java
Anton Romanov 17759d3d83
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m4s
#21 -- Add rules generation
2025-03-15 12:49:49 +04:00

58 lines
2.0 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.FuzzyRule;
import ru.ulstu.fc.rule.model.dto.FuzzyRuleDto;
import ru.ulstu.fc.rule.repository.FuzzyRuleRepository;
@Service
public class FuzzyRuleService {
private final FuzzyRuleRepository ruleRepository;
private final ProjectService projectService;
private final FuzzyRuleRepository fuzzyRuleRepository;
public FuzzyRuleService(FuzzyRuleRepository ruleRepository,
ProjectService projectService, FuzzyRuleRepository fuzzyRuleRepository) {
this.ruleRepository = ruleRepository;
this.projectService = projectService;
this.fuzzyRuleRepository = fuzzyRuleRepository;
}
public FuzzyRule getById(Integer id) {
FuzzyRule fuzzyRule = ruleRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Rule not found by id"));
checkIsCurrentUserFuzzyRuleWithThrow(fuzzyRule);
return fuzzyRule;
}
public FuzzyRuleDto getByIdDto(Integer id) {
return new FuzzyRuleDto(getById(id));
}
public FuzzyRule save(FuzzyRuleDto fuzzyRuleDto) {
FuzzyRule rule;
if (fuzzyRuleDto.getId() == null || fuzzyRuleDto.getId() == 0) {
rule = new FuzzyRule();
} else {
rule = getById(fuzzyRuleDto.getId());
}
rule.setProject(projectService.getById(fuzzyRuleDto.getProjectId()));
rule.setContent(fuzzyRuleDto.getContent());
return ruleRepository.save(rule);
}
public void delete(FuzzyRuleDto ruleForm) {
ruleRepository.delete(getById(ruleForm.getId()));
}
public void checkIsCurrentUserFuzzyRuleWithThrow(FuzzyRule fuzzyRule) {
projectService.checkIsCurrentUserProjectWithThrow(fuzzyRule.getProject());
}
public void clearRules(Integer projectId) {
fuzzyRuleRepository.deleteAllByProjectId(projectId);
}
}