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.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.validation.Valid;
import ru.ulstu.fc.rule.model.FuzzyRuleForm; import ru.ulstu.fc.rule.model.FuzzyRuleForm;
import ru.ulstu.fc.rule.service.FuzzyRuleService; import ru.ulstu.fc.rule.service.FuzzyRuleService;
@ -23,7 +25,7 @@ public class RuleController {
public String edit(@PathVariable(value = "projectId") Integer projectId, public String edit(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "ruleId") Integer id, Model model) { @PathVariable(value = "ruleId") Integer id, Model model) {
model.addAttribute("projectId", projectId); model.addAttribute("projectId", projectId);
model.addAttribute("rule", model.addAttribute("fuzzyRuleForm",
new FuzzyRuleForm(id, (id != null && id != 0) new FuzzyRuleForm(id, (id != null && id != 0)
? ruleService.getById(id).getProject().getId() ? ruleService.getById(id).getProject().getId()
: projectId)); : projectId));
@ -32,16 +34,20 @@ public class RuleController {
} }
@PostMapping(value = "save", params = "save") @PostMapping(value = "save", params = "save")
public String save(FuzzyRuleForm ruleForm, Model model) { public String save(@Valid FuzzyRuleForm fuzzyRuleForm, BindingResult bindingResult, Model model) {
model.addAttribute("rule", ruleService.save(ruleForm)); if (bindingResult.hasErrors()) {
return "redirect:/project/edit/" + ruleForm.getProjectId(); model.addAttribute("projectId", fuzzyRuleForm.getProjectId());
return "rule/edit";
}
ruleService.save(fuzzyRuleForm);
return "redirect:/project/edit/" + fuzzyRuleForm.getProjectId();
} }
@PostMapping(value = "save", params = "delete") @PostMapping(value = "save", params = "delete")
public String delete(FuzzyRuleForm ruleForm) { public String delete(FuzzyRuleForm fuzzyRuleForm) {
if (ruleForm != null && ruleForm.getId() != null) { if (fuzzyRuleForm != null && fuzzyRuleForm.getId() != null) {
ruleService.delete(ruleForm); 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.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping; 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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.validation.Valid;
import ru.ulstu.fc.rule.model.VariableForm; import ru.ulstu.fc.rule.model.VariableForm;
import ru.ulstu.fc.rule.service.VariableService; import ru.ulstu.fc.rule.service.VariableService;
@ -23,7 +26,7 @@ public class VariableController {
public String edit(@PathVariable(value = "projectId") Integer projectId, public String edit(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "varId") Integer id, Model model) { @PathVariable(value = "varId") Integer id, Model model) {
model.addAttribute("projectId", projectId); model.addAttribute("projectId", projectId);
model.addAttribute("var", model.addAttribute("variableForm",
new VariableForm(id, (id != null && id != 0) new VariableForm(id, (id != null && id != 0)
? variableService.getById(id).getProject().getId() ? variableService.getById(id).getProject().getId()
: projectId)); : projectId));
@ -32,8 +35,12 @@ public class VariableController {
} }
@PostMapping(value = "save", params = "save") @PostMapping(value = "save", params = "save")
public String save(VariableForm variableForm, Model model) { public String save(@Valid VariableForm variableForm, BindingResult result, Model model) {
model.addAttribute("rule", variableService.save(variableForm)); if (result.hasErrors()) {
model.addAttribute("projectId", variableForm.getProjectId());
return "var/edit";
}
variableService.save(variableForm);
return "redirect:/project/edit/" + variableForm.getProjectId(); 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.Entity;
import jakarta.persistence.ManyToOne; 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.core.model.BaseEntity;
import ru.ulstu.fc.project.model.Project; import ru.ulstu.fc.project.model.Project;
@Entity @Entity
public class FuzzyRule extends BaseEntity { public class FuzzyRule extends BaseEntity {
@ManyToOne @ManyToOne
@NotNull
private Project project; private Project project;
@Size(min = 5, max = 250, message = "Длина от 5 до 250 символов")
private String content; private String content;
public FuzzyRule() { public FuzzyRule() {

View File

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

View File

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

View File

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

View File

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

View File

@ -8,7 +8,7 @@
</head> </head>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<h3>Редактирование правила:</h3> <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="*{projectId}">
<input type="hidden" th:field="*{id}"> <input type="hidden" th:field="*{id}">
<div class="form-group"> <div class="form-group">

View File

@ -8,7 +8,7 @@
</head> </head>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<h3>Редактирование переменной:</h3> <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="*{projectId}">
<input type="hidden" th:field="*{id}"> <input type="hidden" th:field="*{id}">
<div class="form-group"> <div class="form-group">