diff --git a/src/main/java/ru/ulstu/admin/model/IndicatorForm.java b/src/main/java/ru/ulstu/admin/model/IndicatorForm.java new file mode 100644 index 0000000..57cd476 --- /dev/null +++ b/src/main/java/ru/ulstu/admin/model/IndicatorForm.java @@ -0,0 +1,13 @@ +package ru.ulstu.admin.model; + +public class IndicatorForm { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/ru/ulstu/admin/service/AdminController.java b/src/main/java/ru/ulstu/admin/service/AdminController.java new file mode 100644 index 0000000..488d398 --- /dev/null +++ b/src/main/java/ru/ulstu/admin/service/AdminController.java @@ -0,0 +1,30 @@ +package ru.ulstu.admin.service; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import ru.ulstu.admin.model.IndicatorForm; + +@Controller +@RequestMapping("/admin") +public class AdminController { + private final AdminService adminService; + + public AdminController(AdminService adminService) { + this.adminService = adminService; + } + + @GetMapping("indicators") + public String getListOfIndicators(Model model) { + model.addAttribute("indicators", adminService.getIndicators()); + return "indicatorsList"; + } + + @PostMapping("addIndicator") + public String addIndicator(IndicatorForm indicatorForm, Model model) { + adminService.saveIndicator(indicatorForm); + return "indicatorsList"; + } +} diff --git a/src/main/java/ru/ulstu/admin/service/AdminService.java b/src/main/java/ru/ulstu/admin/service/AdminService.java new file mode 100644 index 0000000..147c821 --- /dev/null +++ b/src/main/java/ru/ulstu/admin/service/AdminService.java @@ -0,0 +1,30 @@ +package ru.ulstu.admin.service; + +import org.springframework.stereotype.Service; +import ru.ulstu.admin.model.IndicatorForm; +import ru.ulstu.indicator.model.Indicator; +import ru.ulstu.indicator.service.IndicatorService; + +import java.util.List; + +@Service +public class AdminService { + private final IndicatorService indicatorService; + + public AdminService(IndicatorService indicatorService) { + this.indicatorService = indicatorService; + } + + public List getIndicators() { + return indicatorService.getIndicatorList(); + } + + 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 + } + } +} diff --git a/src/main/java/ru/ulstu/indicator/model/Indicator.java b/src/main/java/ru/ulstu/indicator/model/Indicator.java new file mode 100644 index 0000000..92ba741 --- /dev/null +++ b/src/main/java/ru/ulstu/indicator/model/Indicator.java @@ -0,0 +1,24 @@ +package ru.ulstu.indicator.model; + +import jakarta.persistence.Entity; +import ru.ulstu.model.BaseEntity; + +@Entity +public class Indicator extends BaseEntity { + private String name; + + public Indicator() { + } + + public Indicator(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/ru/ulstu/indicator/repository/IndicatorRepository.java b/src/main/java/ru/ulstu/indicator/repository/IndicatorRepository.java new file mode 100644 index 0000000..dce70b2 --- /dev/null +++ b/src/main/java/ru/ulstu/indicator/repository/IndicatorRepository.java @@ -0,0 +1,8 @@ +package ru.ulstu.indicator.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.indicator.model.Indicator; + +public interface IndicatorRepository extends JpaRepository { + Indicator findByName(String name); +} diff --git a/src/main/java/ru/ulstu/indicator/service/IndicatorService.java b/src/main/java/ru/ulstu/indicator/service/IndicatorService.java new file mode 100644 index 0000000..31286c6 --- /dev/null +++ b/src/main/java/ru/ulstu/indicator/service/IndicatorService.java @@ -0,0 +1,28 @@ +package ru.ulstu.indicator.service; + +import org.springframework.stereotype.Service; +import ru.ulstu.indicator.model.Indicator; +import ru.ulstu.indicator.repository.IndicatorRepository; + +import java.util.List; + +@Service +public class IndicatorService { + private final IndicatorRepository indicatorRepository; + + public IndicatorService(IndicatorRepository indicatorRepository) { + this.indicatorRepository = indicatorRepository; + } + + public List getIndicatorList() { + return indicatorRepository.findAll(); + } + + public void save(Indicator indicator) { + indicatorRepository.save(indicator); + } + + public Indicator findByName(String name) { + return indicatorRepository.findByName(name); + } +} diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 6100fa6..7b3f377 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -58,6 +58,9 @@