package ru.ulstu.fc.rule.controller; import jakarta.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import ru.ulstu.fc.rule.model.FuzzyRuleForm; import ru.ulstu.fc.rule.service.FuzzyRuleService; @Controller @RequestMapping("rule") public class FuzzyRuleController { private final FuzzyRuleService ruleService; public FuzzyRuleController(FuzzyRuleService ruleService) { this.ruleService = ruleService; } @GetMapping("/edit/{projectId}/{ruleId}") public String edit(@PathVariable(value = "projectId") Integer projectId, @PathVariable(value = "ruleId") Integer id, Model model) { model.addAttribute("projectId", projectId); model.addAttribute("fuzzyRuleForm", (id != null && id != 0) ? new FuzzyRuleForm(id, ruleService.getById(id)) : new FuzzyRuleForm(id, projectId)); return "rule/edit"; } @PostMapping(value = "save", params = "save") public String save(@Valid FuzzyRuleForm fuzzyRuleForm, BindingResult bindingResult, Model model) { if (bindingResult.hasErrors()) { model.addAttribute("projectId", fuzzyRuleForm.getProjectId()); return "rule/edit"; } ruleService.save(fuzzyRuleForm); return "redirect:/project/edit/" + fuzzyRuleForm.getProjectId(); } @PostMapping(value = "save", params = "delete") public String delete(FuzzyRuleForm fuzzyRuleForm) { if (fuzzyRuleForm != null && fuzzyRuleForm.getId() != null) { ruleService.delete(fuzzyRuleForm); } return "redirect:/project/edit/" + fuzzyRuleForm.getProjectId(); } }