#5 -- Show and edit rules
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m47s

This commit is contained in:
Anton Romanov 2025-02-19 14:33:29 +04:00
parent 486981c0ff
commit 11fc4e46d1
5 changed files with 24 additions and 25 deletions

View File

@ -25,9 +25,9 @@ public class RuleController {
@PathVariable(value = "ruleId") Integer id, Model model) {
model.addAttribute("projectId", projectId);
model.addAttribute("rule",
new RuleForm((id != null && id != 0)
? ruleService.getById(id)
: new FuzzyRule()));
new RuleForm(id, (id != null && id != 0)
? ruleService.getById(id).getProject().getId()
: projectId));
return "rule/edit";
}

View File

@ -3,15 +3,14 @@ package ru.ulstu.fc.rule.model;
public class RuleForm {
private Integer id;
private Integer projectId;
private String value;
private String content;
public RuleForm() {
}
public RuleForm(FuzzyRule rule) {
this.projectId = (rule == null || rule.getProject() == null)
? null
: rule.getProject().getId();
public RuleForm(Integer id, Integer projectId) {
this.id = id;
this.projectId = projectId;
}
public Integer getProjectId() {
@ -30,12 +29,12 @@ public class RuleForm {
this.projectId = projectId;
}
public String getValue() {
return value;
public String getContent() {
return content;
}
public void setValue(String value) {
this.value = value;
public void setContent(String content) {
this.content = content;
}
}

View File

@ -24,15 +24,15 @@ public class RuleService {
}
public Object save(RuleForm ruleForm) {
if (ruleForm.getId() == null) {
if (ruleForm.getId() == null || ruleForm.getId() == 0) {
FuzzyRule rule = new FuzzyRule();
rule.setProject(projectService.getById(ruleForm.getProjectId()));
rule.setContent(ruleForm.getValue());
rule.setContent(ruleForm.getContent());
return ruleRepository.save(rule);
}
FuzzyRule dbRule = getById(ruleForm.getId());
dbRule.setProject(projectService.getById(ruleForm.getProjectId()));
dbRule.setContent(ruleForm.getValue());
dbRule.setContent(ruleForm.getContent());
return ruleRepository.save(dbRule);
}

View File

@ -40,9 +40,9 @@
<h4> Список правил</h4>
<div class="row" th:each="r, iter : ${rules}">
<div class="col col-md-12">
<span class="badge badge-light" th:text="${iter} + ' Если'"></span>
<span class="badge badge-light" th:text="${iter.index+1} + '. ' + ${r.content}"></span>
</div>
<div class="col col-md-2 offset-md-3">
<!-- <div class="col col-md-2 offset-md-3">
<span class="badge badge-primary">Переменная</span>
</div>
<div class="col col-md-2">
@ -66,8 +66,8 @@
</div>
<div class="col col-md-1">
<span class="badge badge-danger">И / ИЛИ</span>
</div>
</div>
</div>-->
</div>
<a th:href="@{'/rule/edit/' + ${projectId}+'/0'}" class="btn btn-outline-dark">Добавить правило</a>
</div>
</html>

View File

@ -9,18 +9,18 @@
<div class="container" layout:fragment="content">
<h3>Редактирование правила:</h3>
<form th:action="@{/rule/save}" th:object="${rule}" method="post">
<input type="hidden" th:field="${projectId}">
<input type="hidden" th:field="*{projectId}">
<input type="hidden" th:field="*{id}">
<div class="form-group">
<label for="value">Правило</label>
<input th:field="*{value}"
id="value"
<label for="content">Правило</label>
<input th:field="*{content}"
id="content"
type="text"
required
class="form-control"
placeholder="Правило">
<p th:if="${#fields.hasErrors('value')}"
th:class="${#fields.hasErrors('value')}? error">
<p th:if="${#fields.hasErrors('content')}"
th:class="${#fields.hasErrors('content')}? error">
Не может быть пустым
</p>
</div>