#3 -- Save vars

This commit is contained in:
Anton Romanov 2023-09-26 22:29:53 +04:00
parent df4fd6d0ff
commit 0bbe406486
6 changed files with 55 additions and 26 deletions

View File

@ -5,6 +5,7 @@ import org.springframework.ui.Model;
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.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.fc.rule.model.AddRuleForm; import ru.ulstu.fc.rule.model.AddRuleForm;
import ru.ulstu.fc.rule.model.AddTermForm; import ru.ulstu.fc.rule.model.AddTermForm;
import ru.ulstu.fc.rule.model.AddVariableForm; import ru.ulstu.fc.rule.model.AddVariableForm;
@ -68,14 +69,16 @@ public class RuleController {
} }
@GetMapping("addVariable") @GetMapping("addVariable")
public String addVariable(Model model) { public String addVariable(Model model, @RequestParam(required = false) Integer id) {
model.addAttribute("addVariableForm", new AddVariableForm()); model.addAttribute("addVariableForm", variableService.getAddVariableFormOrDefault(id));
return "addVariable"; return "addVariable";
} }
@PostMapping("addVariable") @PostMapping("addVariable")
public String addVariable(@ModelAttribute AddVariableForm addVariableForm, Model model) { public String addVariable(@ModelAttribute AddVariableForm addVariableForm, Model model) {
model.addAttribute("addVariableForm", addVariableForm); model.addAttribute("addVariableForm", addVariableForm);
variableService.save(addVariableForm);
model.addAttribute("vars", variableService.getVars());
return "listVars"; return "listVars";
} }

View File

@ -1,8 +1,18 @@
package ru.ulstu.fc.rule.model; package ru.ulstu.fc.rule.model;
public class AddVariableForm { public class AddVariableForm {
private Integer id;
private String name; private String name;
public AddVariableForm(Variable variable) {
this.id = variable.getId();
this.name = variable.getName();
}
public AddVariableForm() {
}
public String getName() { public String getName() {
return name; return name;
} }
@ -10,4 +20,12 @@ public class AddVariableForm {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
} }

View File

@ -12,6 +12,13 @@ public class Variable extends BaseEntity {
@OneToMany @OneToMany
private List<Term> terms; private List<Term> terms;
public Variable() {
}
public Variable(String name) {
this.name = name;
}
public String getName() { public String getName() {
return name; return name;
} }

View File

@ -1,6 +1,7 @@
package ru.ulstu.fc.rule.service; package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.fc.rule.model.AddVariableForm;
import ru.ulstu.fc.rule.model.Variable; import ru.ulstu.fc.rule.model.Variable;
import ru.ulstu.fc.rule.repository.VariableRepository; import ru.ulstu.fc.rule.repository.VariableRepository;
@ -18,4 +19,20 @@ public class VariableService {
public List<Variable> getVars() { public List<Variable> getVars() {
return variableRepository.findAll(); return variableRepository.findAll();
} }
public void save(AddVariableForm addVariableForm) {
if (addVariableForm.getId() == null) {
variableRepository.save(new Variable(addVariableForm.getName()));
} else {
Variable dbVar = variableRepository.findById(addVariableForm.getId()).orElseThrow(() -> new RuntimeException("Variable not found by id"));
dbVar.setName(addVariableForm.getName());
variableRepository.save(dbVar);
}
}
public AddVariableForm getAddVariableFormOrDefault(Integer id) {
return id == null
? new AddVariableForm()
: new AddVariableForm(variableRepository.findById(id).orElseThrow(() -> new RuntimeException("Var not foubd by id")));
}
} }

View File

@ -3,37 +3,21 @@
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
layout:decorate="~{default}"> layout:decorate="~{default}">
<head> <head>
<title>Добавить терм</title> <title>Добавить переменную</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head> </head>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<form action="/addTerm" th:object="${addTermForm}" method="post"> <form action="/addVariable" th:object="${addVariableForm}" method="post">
<input type="hidden" th:field="*{id}">
<div class="row"> <div class="row">
<div class="col-md-2 col-sm-12"> <div class="col-md-2 col-sm-12">
<select id="select-variable" class="selectpicker m-2" data-live-search="true" <input class="form-control" type="text" th:field="*{name}">
th:field="*{variable}"
data-width="90%">
<option th:each="var : ${variables}"
th:value="${var}"
th:utext="${var}">
</option>
</select>
</div>
<div class="col-md-2 col-sm-12">
<input class="form-control" type="text" th:field="*{term}">
</div>
<div class="col-md-2 col-sm-12">
<input class="form-control" type="text" th:field="*{min}">
</div>
<div class="col-md-2 col-sm-12">
<input class="form-control" type="text" th:field="*{max}">
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-md-4 col-sm-12"> <div class="col-md-4 col-sm-12">
<input type="submit" class="btn btn-outline-success m-2" value="Создать term"/> <input type="submit" class="btn btn-outline-success m-2" value="Создать переменную"/>
</div> </div>
</div> </div>
</form> </form>

View File

@ -14,10 +14,10 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr th:each="dbVar: ${variable}"> <tr th:each="dbVar: ${vars}">
<td><span class="badge badge-success" th:text="${dbVar.name}"></span></td> <td><span class="badge badge-success" th:text="${dbVar.name}"></span></td>
<td> <td>
<a role="button" class="btn btn-info" th:href="@{'addVar?varId=' + ${dbVar.id}}"> <a role="button" class="btn btn-info" th:href="@{'addVariable?id=' + ${dbVar.id}}">
<i class="fa fa-pencil-square-o" aria-hidden="true"></i> <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
</a> </a>
</td> </td>
@ -30,6 +30,6 @@
</tr> </tr>
</tbody> </tbody>
</table> </table>
<a href="/addVar" class="btn btn-outline-success">Добавить переменную</a> <a href="/addVariable" class="btn btn-outline-success">Добавить переменную</a>
</div> </div>
</html> </html>