diff --git a/src/main/java/ru/ulstu/fc/rule/controller/RuleController.java b/src/main/java/ru/ulstu/fc/rule/controller/RuleController.java index ef6f365..628a315 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/RuleController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/RuleController.java @@ -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"; } diff --git a/src/main/java/ru/ulstu/fc/rule/model/AddVariableForm.java b/src/main/java/ru/ulstu/fc/rule/model/AddVariableForm.java index 2058006..b6e5619 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/AddVariableForm.java +++ b/src/main/java/ru/ulstu/fc/rule/model/AddVariableForm.java @@ -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; + } } diff --git a/src/main/java/ru/ulstu/fc/rule/model/Variable.java b/src/main/java/ru/ulstu/fc/rule/model/Variable.java index 66753db..8101f53 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/Variable.java +++ b/src/main/java/ru/ulstu/fc/rule/model/Variable.java @@ -12,6 +12,13 @@ public class Variable extends BaseEntity { @OneToMany private List terms; + public Variable() { + } + + public Variable(String name) { + this.name = name; + } + public String getName() { return name; } diff --git a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java index 0a9890c..7837533 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java @@ -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 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"))); + } } diff --git a/src/main/resources/templates/addVariable.html b/src/main/resources/templates/addVariable.html index bfb1cee..ec3ad83 100644 --- a/src/main/resources/templates/addVariable.html +++ b/src/main/resources/templates/addVariable.html @@ -3,37 +3,21 @@ xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml" layout:decorate="~{default}"> - Добавить терм + Добавить переменную
-
+ +
- - -
-
- -
-
- -
-
- +
- +
diff --git a/src/main/resources/templates/listVars.html b/src/main/resources/templates/listVars.html index 705ac4e..90d97ab 100644 --- a/src/main/resources/templates/listVars.html +++ b/src/main/resources/templates/listVars.html @@ -14,10 +14,10 @@ - + - + @@ -30,6 +30,6 @@ - Добавить переменную + Добавить переменную