From 8b8d64bd0094bbbe8ef688f4580d029c20beba79 Mon Sep 17 00:00:00 2001
From: Anton Romanov
Date: Wed, 29 Jan 2025 09:25:39 +0400
Subject: [PATCH] Add removing indicator
---
.../java/ru/ulstu/admin/model/IndicatorForm.java | 10 ++++++++++
.../ru/ulstu/admin/service/AdminController.java | 11 ++++++++---
.../java/ru/ulstu/admin/service/AdminService.java | 15 +++++++++------
.../ulstu/indicator/service/IndicatorService.java | 5 +++++
src/main/resources/templates/editIndicator.html | 10 ++++++++--
src/main/resources/templates/indicatorsList.html | 2 +-
6 files changed, 41 insertions(+), 12 deletions(-)
diff --git a/src/main/java/ru/ulstu/admin/model/IndicatorForm.java b/src/main/java/ru/ulstu/admin/model/IndicatorForm.java
index 57cd476..f143358 100644
--- a/src/main/java/ru/ulstu/admin/model/IndicatorForm.java
+++ b/src/main/java/ru/ulstu/admin/model/IndicatorForm.java
@@ -1,6 +1,8 @@
package ru.ulstu.admin.model;
public class IndicatorForm {
+ private Integer id;
+
private String name;
public String getName() {
@@ -10,4 +12,12 @@ public class IndicatorForm {
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/admin/service/AdminController.java b/src/main/java/ru/ulstu/admin/service/AdminController.java
index ba05dc3..3ae0e5c 100644
--- a/src/main/java/ru/ulstu/admin/service/AdminController.java
+++ b/src/main/java/ru/ulstu/admin/service/AdminController.java
@@ -33,10 +33,15 @@ public class AdminController {
return "editIndicator";
}
- @PostMapping("saveIndicator")
+ @PostMapping(value = "saveIndicator", params = "save")
public String saveIndicator(IndicatorForm indicatorForm, Model model) {
adminService.saveIndicator(indicatorForm);
- model.addAttribute("indicators", adminService.getIndicators());
- return "indicatorsList";
+ return "redirect:/admin/indicators";
+ }
+
+ @PostMapping(value = "saveIndicator", params = "delete")
+ public String deleteIndicator(IndicatorForm indicatorForm, Model model) {
+ adminService.deleteIndicator(indicatorForm);
+ return "redirect:/admin/indicators";
}
}
diff --git a/src/main/java/ru/ulstu/admin/service/AdminService.java b/src/main/java/ru/ulstu/admin/service/AdminService.java
index e344311..5a67910 100644
--- a/src/main/java/ru/ulstu/admin/service/AdminService.java
+++ b/src/main/java/ru/ulstu/admin/service/AdminService.java
@@ -20,15 +20,18 @@ public class AdminService {
}
public void saveIndicator(IndicatorForm indicatorForm) {
- Indicator indicator = indicatorService.findByName(indicatorForm.getName());
- if (indicator == null) {
- indicatorService.save(new Indicator(indicatorForm.getName()));
- } else {
- // TODO: update other fields
- }
+ Indicator indicator = indicatorForm.getId() == null
+ ? new Indicator(indicatorForm.getName())
+ : indicatorService.getIndicatorById(indicatorForm.getId());
+ indicator.setName(indicatorForm.getName());
+ indicatorService.save(indicator);
}
public Indicator getIndicatorById(Integer id) {
return indicatorService.getIndicatorById(id);
}
+
+ public void deleteIndicator(IndicatorForm indicatorForm) {
+ indicatorService.deleteIndicator(indicatorForm);
+ }
}
diff --git a/src/main/java/ru/ulstu/indicator/service/IndicatorService.java b/src/main/java/ru/ulstu/indicator/service/IndicatorService.java
index eebdd71..27f9286 100644
--- a/src/main/java/ru/ulstu/indicator/service/IndicatorService.java
+++ b/src/main/java/ru/ulstu/indicator/service/IndicatorService.java
@@ -1,6 +1,7 @@
package ru.ulstu.indicator.service;
import org.springframework.stereotype.Service;
+import ru.ulstu.admin.model.IndicatorForm;
import ru.ulstu.indicator.model.Indicator;
import ru.ulstu.indicator.repository.IndicatorRepository;
@@ -29,4 +30,8 @@ public class IndicatorService {
public Indicator getIndicatorById(Integer id) {
return indicatorRepository.findById(id).orElseThrow(() -> new RuntimeException("Indicator not found"));
}
+
+ public void deleteIndicator(IndicatorForm indicatorForm) {
+ indicatorRepository.deleteById(indicatorForm.getId());
+ }
}
diff --git a/src/main/resources/templates/editIndicator.html b/src/main/resources/templates/editIndicator.html
index 6ab2ab9..b13ee24 100644
--- a/src/main/resources/templates/editIndicator.html
+++ b/src/main/resources/templates/editIndicator.html
@@ -15,8 +15,14 @@
Не может быть пустым
-
- Отмена
+
+
+ Отмена
diff --git a/src/main/resources/templates/indicatorsList.html b/src/main/resources/templates/indicatorsList.html
index 6631abd..454e7ca 100644
--- a/src/main/resources/templates/indicatorsList.html
+++ b/src/main/resources/templates/indicatorsList.html
@@ -8,7 +8,7 @@