Add indicator edit

This commit is contained in:
Anton Romanov 2025-01-29 01:26:32 +04:00
parent 5a4a968015
commit 2ef33f1b68
6 changed files with 55 additions and 6 deletions

View File

@ -1,11 +1,15 @@
package ru.ulstu.admin.service; package ru.ulstu.admin.service;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; 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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import ru.ulstu.admin.model.IndicatorForm; import ru.ulstu.admin.model.IndicatorForm;
import ru.ulstu.indicator.model.Indicator;
import ru.ulstu.model.UserRoleConstants;
@Controller @Controller
@RequestMapping("/admin") @RequestMapping("/admin")
@ -22,9 +26,17 @@ public class AdminController {
return "indicatorsList"; return "indicatorsList";
} }
@PostMapping("addIndicator") @GetMapping("/editIndicator/{indicatorId}")
public String addIndicator(IndicatorForm indicatorForm, Model model) { @Secured({UserRoleConstants.ADMIN})
public String editIndicator(@PathVariable(value = "indicatorId") Integer id, Model model) {
model.addAttribute("indicator", (id != null && id != 0) ? adminService.getIndicatorById(id) : new Indicator());
return "editIndicator";
}
@PostMapping("saveIndicator")
public String saveIndicator(IndicatorForm indicatorForm, Model model) {
adminService.saveIndicator(indicatorForm); adminService.saveIndicator(indicatorForm);
model.addAttribute("indicators", adminService.getIndicators());
return "indicatorsList"; return "indicatorsList";
} }
} }

View File

@ -27,4 +27,8 @@ public class AdminService {
// TODO: update other fields // TODO: update other fields
} }
} }
public Indicator getIndicatorById(Integer id) {
return indicatorService.getIndicatorById(id);
}
} }

View File

@ -1,10 +1,13 @@
package ru.ulstu.indicator.model; package ru.ulstu.indicator.model;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.validation.constraints.NotBlank;
import ru.ulstu.model.BaseEntity; import ru.ulstu.model.BaseEntity;
@Entity @Entity
public class Indicator extends BaseEntity { public class Indicator extends BaseEntity {
@NotBlank
private String name; private String name;
public Indicator() { public Indicator() {

View File

@ -25,4 +25,8 @@ public class IndicatorService {
public Indicator findByName(String name) { public Indicator findByName(String name) {
return indicatorRepository.findByName(name); return indicatorRepository.findByName(name);
} }
public Indicator getIndicatorById(Integer id) {
return indicatorRepository.findById(id).orElseThrow(() -> new RuntimeException("Indicator not found"));
}
} }

View File

@ -0,0 +1,24 @@
<!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}">
<div class="container" layout:fragment="content">
<h3>Редактирование показателя:</h3>
<form action="#" th:action="@{/admin/saveIndicator}" th:object="${indicator}" method="post"
enctype="multipart/form-data">
<input type="hidden" th:field="*{id}">
<input type="hidden" th:field="*{version}">
<div class="form-group">
<label for="name">Название</label>
<input type="text" class="form-control" id="name" th:field="*{name}" placeholder="Название показателя">
<p th:if="${#fields.hasErrors('name')}" th:class="${#fields.hasErrors('name')}? error">
Не может быть пустым</p>
</div>
<button type="submit" class="btn btn-outline-dark">Сохранить</button>
<a href="javascript:history.back()" class="btn btn-outline-dark">Отмена</a>
</form>
<link rel="stylesheet" href="/webjars/font-awesome/4.7.0/css/font-awesome.min.css"/>
<link rel="stylesheet" href="/webjars/bootstrap-glyphicons/bdd2cbfba0/css/bootstrap-glyphicons.css"/>
</div>
</html>

View File

@ -2,12 +2,14 @@
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml" <html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
layout:decorate="~{default}"> layout:decorate="~{default}">
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<a href="/editIndicator/0" class="btn btn-outline-dark"> <a href="/admin/editIndicator/0" class="btn btn-outline-dark">
<i class="fa fa-plus-square" aria-hidden="true"> Добавить показатель</i> <i class="fa fa-plus-square" aria-hidden="true"> Добавить показатель</i>
</a> </a>
<div th:each="i : ${indicators}"> <ul>
<div th:text="${i.name}"></div> <li th:each="i : ${indicators}">
</div> <span th:text="${i.name}"></span>
</li>
</ul>
</div> </div>
</html> </html>