#3 -- Add models and pages

This commit is contained in:
Anton Romanov 2023-09-26 21:58:00 +04:00
parent 950dde7f83
commit df4fd6d0ff
13 changed files with 276 additions and 4 deletions

View File

@ -7,16 +7,47 @@ import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
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.service.RuleParseService; import ru.ulstu.fc.rule.service.RuleParseService;
import ru.ulstu.fc.rule.service.RuleService;
import ru.ulstu.fc.rule.service.TermsService;
import ru.ulstu.fc.rule.service.VariableService;
import java.util.List; import java.util.List;
@Controller @Controller
public class RuleController { public class RuleController {
private final RuleParseService ruleParseService; private final RuleParseService ruleParseService;
private final RuleService ruleService;
private final TermsService termService;
private final VariableService variableService;
public RuleController(RuleParseService ruleParseService) { public RuleController(RuleParseService ruleParseService,
RuleService ruleService,
TermsService termService,
VariableService variableService) {
this.ruleParseService = ruleParseService; this.ruleParseService = ruleParseService;
this.ruleService = ruleService;
this.termService = termService;
this.variableService = variableService;
}
@GetMapping("listRules")
public String listRules(Model model) {
model.addAttribute("rules", ruleService.getRules());
return "listRules";
}
@GetMapping("listTerms")
public String listTerms(Model model) {
model.addAttribute("terms", termService.getTerms());
return "listTerms";
}
@GetMapping("listVars")
public String listVariables(Model model) {
model.addAttribute("vars", variableService.getVars());
return "listVars";
} }
@GetMapping("addRule") @GetMapping("addRule")
@ -36,6 +67,18 @@ public class RuleController {
return "listRules"; return "listRules";
} }
@GetMapping("addVariable")
public String addVariable(Model model) {
model.addAttribute("addVariableForm", new AddVariableForm());
return "addVariable";
}
@PostMapping("addVariable")
public String addVariable(@ModelAttribute AddVariableForm addVariableForm, Model model) {
model.addAttribute("addVariableForm", addVariableForm);
return "listVars";
}
@GetMapping("addTerm") @GetMapping("addTerm")
public String addTerm(Model model) { public String addTerm(Model model) {
model.addAttribute("addTermForm", new AddTermForm()); model.addAttribute("addTermForm", new AddTermForm());
@ -43,7 +86,7 @@ public class RuleController {
} }
@PostMapping("addTerm") @PostMapping("addTerm")
public String parse(@ModelAttribute AddTermForm addTermForm, Model model) { public String addTerm(@ModelAttribute AddTermForm addTermForm, Model model) {
model.addAttribute("addTermForm", addTermForm); model.addAttribute("addTermForm", addTermForm);
return "listTerms"; return "listTerms";
} }

View File

@ -0,0 +1,13 @@
package ru.ulstu.fc.rule.model;
public class AddVariableForm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,7 @@
package ru.ulstu.fc.rule.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.fc.rule.model.Rule;
public interface RuleRepository extends JpaRepository<Rule, Integer> {
}

View File

@ -0,0 +1,7 @@
package ru.ulstu.fc.rule.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.fc.rule.model.Term;
public interface TermRepository extends JpaRepository<Term, Integer> {
}

View File

@ -0,0 +1,7 @@
package ru.ulstu.fc.rule.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.fc.rule.model.Variable;
public interface VariableRepository extends JpaRepository<Variable, Integer> {
}

View File

@ -0,0 +1,20 @@
package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.rule.model.Rule;
import ru.ulstu.fc.rule.repository.RuleRepository;
import java.util.List;
@Service
public class RuleService {
private final RuleRepository ruleRepository;
public RuleService(RuleRepository ruleRepository) {
this.ruleRepository = ruleRepository;
}
public List<Rule> getRules() {
return ruleRepository.findAll();
}
}

View File

@ -0,0 +1,20 @@
package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.rule.model.Term;
import ru.ulstu.fc.rule.repository.TermRepository;
import java.util.List;
@Service
public class TermService {
private final TermRepository termRepository;
public TermService(TermRepository termRepository) {
this.termRepository = termRepository;
}
public List<Term> getTerms() {
return termRepository.findAll();
}
}

View File

@ -0,0 +1,20 @@
package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.rule.model.Term;
import ru.ulstu.fc.rule.repository.TermRepository;
import java.util.List;
@Service
public class TermsService {
private final TermRepository termRepository;
public TermsService(TermRepository termRepository) {
this.termRepository = termRepository;
}
public List<Term> getTerms() {
return termRepository.findAll();
}
}

View File

@ -0,0 +1,21 @@
package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.rule.model.Variable;
import ru.ulstu.fc.rule.repository.VariableRepository;
import java.util.List;
@Service
public class VariableService {
private final VariableRepository variableRepository;
public VariableService(VariableRepository variableRepository) {
this.variableRepository = variableRepository;
}
public List<Variable> getVars() {
return variableRepository.findAll();
}
}

View File

@ -0,0 +1,41 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
layout:decorate="~{default}">
<head>
<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">
<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}">
</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"/>
</div>
</div>
</form>
</div>
</html>

View File

@ -22,10 +22,13 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto"> <ul class="navbar-nav mr-auto">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/listRules">Правила</a> <a class="nav-link" href="/listVars">Переменные</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/addRule">Добавить правило</a> <a class="nav-link" href="/listTerms">Термы</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/listRules">Правила</a>
</li> </li>
</ul> </ul>
</div> </div>

View File

@ -0,0 +1,35 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
layout:decorate="~{default}">
<head>
<title>Список термов</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col" colspan="10">Термы</th>
</tr>
</thead>
<tbody>
<tr th:each="dbTerm: ${terms}">
<td><span class="badge badge-success" th:text="${dbTerm.name}"></span></td>
<td>
<a role="button" class="btn btn-info" th:href="@{'addTerm?termId=' + ${dbTerm.id}}">
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
</a>
</td>
<td>
<a role="button" class="btn btn-danger" th:href="@{'deleteTerm?id=' + ${dbTerm.id}}"
onclick="return confirm('Удалить терм?')">
<i class="fa fa-times" aria-hidden="true"></i>
</a>
</td>
</tr>
</tbody>
</table>
<a href="/addTerm" class="btn btn-outline-success">Добавить терм</a>
</div>
</html>

View File

@ -0,0 +1,35 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
layout:decorate="~{default}">
<head>
<title>Список переменных</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col" colspan="10">Переменные</th>
</tr>
</thead>
<tbody>
<tr th:each="dbVar: ${variable}">
<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}}">
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
</a>
</td>
<td>
<a role="button" class="btn btn-danger" th:href="@{'deleteVar?id=' + ${dbVar.id}}"
onclick="return confirm('Удалить переменную?')">
<i class="fa fa-times" aria-hidden="true"></i>
</a>
</td>
</tr>
</tbody>
</table>
<a href="/addVar" class="btn btn-outline-success">Добавить переменную</a>
</div>
</html>