Anton Romanov 495b04e51d
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 2m1s
#13 -- Add rule crud
2025-02-17 14:08:58 +04:00

49 lines
1.6 KiB
Java

package ru.ulstu.fc.rule.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.project.model.Project;
import ru.ulstu.fc.project.model.ProjectForm;
import ru.ulstu.fc.project.service.ProjectService;
import ru.ulstu.fc.rule.model.Rule;
import ru.ulstu.fc.rule.model.RuleForm;
@Controller
@RequestMapping("rule")
public class RuleController {
private final RuleService ruleService;
public RuleController(RuleService ruleService) {
this.ruleService = ruleService;
}
@GetMapping("/edit/{ruleId}")
public String edit(@PathVariable(value = "ruleId") Integer id, Model model) {
model.addAttribute("rule",
new RuleForm((id != null && id != 0)
? ruleService.getById(id)
: new Rule()));
return "rule/edit";
}
@PostMapping(value = "save", params = "save")
public String save(RuleForm ruleForm, Model model) {
model.addAttribute("rule", ruleService.save(ruleForm));
return "redirect:/project/edit/"+ruleForm.getProjectId();
}
@PostMapping(value = "save", params = "delete")
public String delete(RuleForm ruleForm) {
if (ruleForm != null && ruleForm.getId() != null) {
ruleService.delete(ruleForm);
}
return "redirect:/project/edit/"+ruleForm.getProjectId();
}
}