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); } }