package ru.ulstu.fc.rule.controller; 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 jakarta.validation.Valid; import ru.ulstu.fc.rule.model.VariableForm; import ru.ulstu.fc.rule.service.VariableService; @Controller @RequestMapping("var") public class VariableController { private final VariableService variableService; public VariableController(VariableService variableService) { this.variableService = variableService; } @GetMapping("/edit/{projectId}/{varId}") public String edit(@PathVariable(value = "projectId") Integer projectId, @PathVariable(value = "varId") Integer id, Model model) { model.addAttribute("projectId", projectId); model.addAttribute("variableForm", (id != null && id != 0) ? new VariableForm(id, variableService.getById(id)) : new VariableForm(id, projectId)); return "var/edit"; } @PostMapping(value = "save", params = "save") public String save(@Valid VariableForm variableForm, BindingResult result, Model model) { if (result.hasErrors()) { model.addAttribute("projectId", variableForm.getProjectId()); return "var/edit"; } variableService.save(variableForm); return "redirect:/project/edit/" + variableForm.getProjectId(); } @PostMapping(value = "save", params = "delete") public String delete(VariableForm variableForm) { if (variableForm != null && variableForm.getId() != null) { variableService.delete(variableForm); } return "redirect:/project/edit/" + variableForm.getProjectId(); } }