diff --git a/src/main/java/ru/ulstu/admin/service/AdminController.java b/src/main/java/ru/ulstu/admin/service/AdminController.java index 488d398..ba05dc3 100644 --- a/src/main/java/ru/ulstu/admin/service/AdminController.java +++ b/src/main/java/ru/ulstu/admin/service/AdminController.java @@ -1,11 +1,15 @@ package ru.ulstu.admin.service; +import org.springframework.security.access.annotation.Secured; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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.RequestMapping; import ru.ulstu.admin.model.IndicatorForm; +import ru.ulstu.indicator.model.Indicator; +import ru.ulstu.model.UserRoleConstants; @Controller @RequestMapping("/admin") @@ -22,9 +26,17 @@ public class AdminController { return "indicatorsList"; } - @PostMapping("addIndicator") - public String addIndicator(IndicatorForm indicatorForm, Model model) { + @GetMapping("/editIndicator/{indicatorId}") + @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); + model.addAttribute("indicators", adminService.getIndicators()); return "indicatorsList"; } } diff --git a/src/main/java/ru/ulstu/admin/service/AdminService.java b/src/main/java/ru/ulstu/admin/service/AdminService.java index 147c821..e344311 100644 --- a/src/main/java/ru/ulstu/admin/service/AdminService.java +++ b/src/main/java/ru/ulstu/admin/service/AdminService.java @@ -27,4 +27,8 @@ public class AdminService { // TODO: update other fields } } + + public Indicator getIndicatorById(Integer id) { + return indicatorService.getIndicatorById(id); + } } diff --git a/src/main/java/ru/ulstu/indicator/model/Indicator.java b/src/main/java/ru/ulstu/indicator/model/Indicator.java index 92ba741..fbec5a2 100644 --- a/src/main/java/ru/ulstu/indicator/model/Indicator.java +++ b/src/main/java/ru/ulstu/indicator/model/Indicator.java @@ -1,10 +1,13 @@ package ru.ulstu.indicator.model; import jakarta.persistence.Entity; +import jakarta.validation.constraints.NotBlank; import ru.ulstu.model.BaseEntity; @Entity public class Indicator extends BaseEntity { + + @NotBlank private String name; public Indicator() { diff --git a/src/main/java/ru/ulstu/indicator/service/IndicatorService.java b/src/main/java/ru/ulstu/indicator/service/IndicatorService.java index 31286c6..eebdd71 100644 --- a/src/main/java/ru/ulstu/indicator/service/IndicatorService.java +++ b/src/main/java/ru/ulstu/indicator/service/IndicatorService.java @@ -25,4 +25,8 @@ public class IndicatorService { public Indicator findByName(String name) { return indicatorRepository.findByName(name); } + + public Indicator getIndicatorById(Integer id) { + return indicatorRepository.findById(id).orElseThrow(() -> new RuntimeException("Indicator not found")); + } } diff --git a/src/main/resources/templates/editIndicator.html b/src/main/resources/templates/editIndicator.html new file mode 100644 index 0000000..6ab2ab9 --- /dev/null +++ b/src/main/resources/templates/editIndicator.html @@ -0,0 +1,24 @@ + + +