3-parse-rule #10

Open
romanov73 wants to merge 9 commits from 3-parse-rule into master
6 changed files with 55 additions and 26 deletions
Showing only changes of commit 0bbe406486 - Show all commits

View File

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

View File

@ -1,8 +1,18 @@
package ru.ulstu.fc.rule.model;
public class AddVariableForm {
private Integer id;
private String name;
public AddVariableForm(Variable variable) {
this.id = variable.getId();
this.name = variable.getName();
}
public AddVariableForm() {
}
public String getName() {
return name;
}
@ -10,4 +20,12 @@ public class AddVariableForm {
public void setName(String 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
private List<Term> terms;
public Variable() {
}
public Variable(String name) {
this.name = name;
}
public String getName() {
return name;
}

View File

@ -1,6 +1,7 @@
package ru.ulstu.fc.rule.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.repository.VariableRepository;
@ -18,4 +19,20 @@ public class VariableService {
public List<Variable> getVars() {
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"
layout:decorate="~{default}">
<head>
<title>Добавить терм</title>
<title>Добавить переменную</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<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="col-md-2 col-sm-12">
<select id="select-variable" class="selectpicker m-2" data-live-search="true"
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}">
<input class="form-control" type="text" th:field="*{name}">
</div>
</div>
<div class="row">
<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>
</form>

View File

@ -14,10 +14,10 @@
</tr>
</thead>
<tbody>
<tr th:each="dbVar: ${variable}">
<tr th:each="dbVar: ${vars}">
<td><span class="badge badge-success" th:text="${dbVar.name}"></span></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>
</a>
</td>
@ -30,6 +30,6 @@
</tr>
</tbody>
</table>
<a href="/addVar" class="btn btn-outline-success">Добавить переменную</a>
<a href="/addVariable" class="btn btn-outline-success">Добавить переменную</a>
</div>
</html>