5-save-rules #15

Open
romanov73 wants to merge 11 commits from 5-save-rules into master
9 changed files with 50 additions and 15 deletions
Showing only changes of commit 4449902185 - Show all commits

View File

@ -2,11 +2,13 @@ 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.FuzzyRuleForm;
import ru.ulstu.fc.rule.service.FuzzyRuleService;
@ -23,7 +25,7 @@ public class RuleController {
public String edit(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "ruleId") Integer id, Model model) {
model.addAttribute("projectId", projectId);
model.addAttribute("rule",
model.addAttribute("fuzzyRuleForm",
new FuzzyRuleForm(id, (id != null && id != 0)
? ruleService.getById(id).getProject().getId()
: projectId));
@ -32,16 +34,20 @@ public class RuleController {
}
@PostMapping(value = "save", params = "save")
public String save(FuzzyRuleForm ruleForm, Model model) {
model.addAttribute("rule", ruleService.save(ruleForm));
return "redirect:/project/edit/" + ruleForm.getProjectId();
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 ruleForm) {
if (ruleForm != null && ruleForm.getId() != null) {
ruleService.delete(ruleForm);
public String delete(FuzzyRuleForm fuzzyRuleForm) {
if (fuzzyRuleForm != null && fuzzyRuleForm.getId() != null) {
ruleService.delete(fuzzyRuleForm);
}
return "redirect:/project/edit/" + ruleForm.getProjectId();
return "redirect:/project/edit/" + fuzzyRuleForm.getProjectId();
}
}

View File

@ -2,11 +2,14 @@ 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.ModelAttribute;
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;
@ -23,7 +26,7 @@ public class VariableController {
public String edit(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "varId") Integer id, Model model) {
model.addAttribute("projectId", projectId);
model.addAttribute("var",
model.addAttribute("variableForm",
new VariableForm(id, (id != null && id != 0)
? variableService.getById(id).getProject().getId()
: projectId));
@ -32,8 +35,12 @@ public class VariableController {
}
@PostMapping(value = "save", params = "save")
public String save(VariableForm variableForm, Model model) {
model.addAttribute("rule", variableService.save(variableForm));
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();
}

View File

@ -2,13 +2,17 @@ package ru.ulstu.fc.rule.model;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import ru.ulstu.fc.core.model.BaseEntity;
import ru.ulstu.fc.project.model.Project;
@Entity
public class FuzzyRule extends BaseEntity {
@ManyToOne
@NotNull
private Project project;
@Size(min = 5, max = 250, message = "Длина от 5 до 250 символов")
private String content;
public FuzzyRule() {

View File

@ -1,8 +1,14 @@
package ru.ulstu.fc.rule.model;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
public class FuzzyRuleForm {
private Integer id;
@NotNull
private Integer projectId;
@Size(min = 5, max = 250, message = "Длина от 5 до 250 символов")
private String content;
public FuzzyRuleForm() {

View File

@ -9,15 +9,22 @@ import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import ru.ulstu.fc.core.model.BaseEntity;
import ru.ulstu.fc.project.model.Project;
@Entity
public class Variable extends BaseEntity {
@Size(min = 3, max = 250, message = "Длина должна быть от 3 до 250")
private String name;
@ManyToOne
@NotNull
private Project project;
private boolean isInput = true;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "variable_id", unique = true)
private List<FuzzyTerm> fuzzyTerms = new ArrayList<>();

View File

@ -1,8 +1,13 @@
package ru.ulstu.fc.rule.model;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
public class VariableForm {
private Integer id;
@NotNull
private Integer projectId;
@Size(min = 3, max = 250, message = "Длина должна быть от 3 до 250")
private String name;
public VariableForm() {

View File

@ -2,10 +2,10 @@ package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.rule.repository.VariableRepository;
import ru.ulstu.fc.project.service.ProjectService;
import ru.ulstu.fc.rule.model.Variable;
import ru.ulstu.fc.rule.model.VariableForm;
import ru.ulstu.fc.rule.repository.VariableRepository;
@Service
public class VariableService {

View File

@ -8,7 +8,7 @@
</head>
<div class="container" layout:fragment="content">
<h3>Редактирование правила:</h3>
<form th:action="@{/rule/save}" th:object="${rule}" method="post">
<form th:action="@{/rule/save}" th:object="${fuzzyRuleForm}" method="post">
<input type="hidden" th:field="*{projectId}">
<input type="hidden" th:field="*{id}">
<div class="form-group">

View File

@ -8,7 +8,7 @@
</head>
<div class="container" layout:fragment="content">
<h3>Редактирование переменной:</h3>
<form th:action="@{/var/save}" th:object="${var}" method="post">
<form th:action="@{/var/save}" th:object="${variableForm}" method="post">
<input type="hidden" th:field="*{projectId}">
<input type="hidden" th:field="*{id}">
<div class="form-group">