diff --git a/src/main/java/ru/ulstu/admin/controller/AdminManagerController.java b/src/main/java/ru/ulstu/admin/controller/AdminManagerController.java new file mode 100644 index 0000000..e6c4ce8 --- /dev/null +++ b/src/main/java/ru/ulstu/admin/controller/AdminManagerController.java @@ -0,0 +1,48 @@ +package ru.ulstu.admin.controller; + +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.ManagerForm; +import ru.ulstu.admin.service.AdminManagerService; +import ru.ulstu.manager.model.Manager; + +@Controller +@RequestMapping("admin") +public class AdminManagerController { + private final AdminManagerService adminManagerService; + + public AdminManagerController(AdminManagerService adminManagerService) { + this.adminManagerService = adminManagerService; + } + + @GetMapping("managers") + public String getListOfManagers(Model model) { + model.addAttribute("managers", adminManagerService.getManagers()); + return "admin/managersList"; + } + + @GetMapping("/editManager/{managerId}") + public String editManager(@PathVariable(value = "managerId") Integer id, Model model) { + model.addAttribute("manager", + (id != null && id != 0) + ? adminManagerService.getManagerById(id) + : new Manager()); + return "admin/editManager"; + } + + @PostMapping(value = "saveManager", params = "save") + public String saveManager(ManagerForm managerForm, Model model) { + adminManagerService.saveManager(managerForm); + return "redirect:/admin/managers"; + } + + @PostMapping(value = "saveManager", params = "delete") + public String deleteIndicator(ManagerForm managerForm, Model model) { + adminManagerService.deleteManager(managerForm); + return "redirect:/admin/managers"; + } +} diff --git a/src/main/java/ru/ulstu/admin/model/ManagerForm.java b/src/main/java/ru/ulstu/admin/model/ManagerForm.java new file mode 100644 index 0000000..e9544d3 --- /dev/null +++ b/src/main/java/ru/ulstu/admin/model/ManagerForm.java @@ -0,0 +1,32 @@ +package ru.ulstu.admin.model; + +import ru.ulstu.manager.model.Manager; + +public class ManagerForm { + private Integer id; + private String name; + + public ManagerForm() { + } + + public ManagerForm(Manager manager) { + this.id = manager.getId(); + this.name = manager.getName(); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/ru/ulstu/admin/service/AdminManagerService.java b/src/main/java/ru/ulstu/admin/service/AdminManagerService.java new file mode 100644 index 0000000..e8ec791 --- /dev/null +++ b/src/main/java/ru/ulstu/admin/service/AdminManagerService.java @@ -0,0 +1,37 @@ +package ru.ulstu.admin.service; + +import org.springframework.stereotype.Service; +import ru.ulstu.admin.model.ManagerForm; +import ru.ulstu.manager.model.Manager; +import ru.ulstu.manager.service.ManagerService; + +import java.util.List; + +@Service +public class AdminManagerService { + private final ManagerService managerService; + + public AdminManagerService(ManagerService managerService) { + this.managerService = managerService; + } + + public List getManagers() { + return managerService.getManagers(); + } + + public Manager getManagerById(Integer id) { + return managerService.getManagerById(id); + } + + public void saveManager(ManagerForm managerForm) { + Manager manager = managerForm.getId() == null + ? new Manager(managerForm) + : managerService.getManagerById(managerForm.getId()); + manager.setName(managerForm.getName()); + managerService.save(manager); + } + + public void deleteManager(ManagerForm managerForm) { + managerService.deleteManager(managerForm); + } +} diff --git a/src/main/java/ru/ulstu/manager/model/Manager.java b/src/main/java/ru/ulstu/manager/model/Manager.java index e44da80..993bf66 100644 --- a/src/main/java/ru/ulstu/manager/model/Manager.java +++ b/src/main/java/ru/ulstu/manager/model/Manager.java @@ -1,12 +1,20 @@ package ru.ulstu.manager.model; import jakarta.persistence.Entity; +import ru.ulstu.admin.model.ManagerForm; import ru.ulstu.model.BaseEntity; @Entity public class Manager extends BaseEntity { private String name; + public Manager() { + } + + public Manager(ManagerForm managerForm) { + this.name = managerForm.getName(); + } + public String getName() { return name; } diff --git a/src/main/java/ru/ulstu/manager/service/ManagerService.java b/src/main/java/ru/ulstu/manager/service/ManagerService.java index 6e20cef..b911050 100644 --- a/src/main/java/ru/ulstu/manager/service/ManagerService.java +++ b/src/main/java/ru/ulstu/manager/service/ManagerService.java @@ -1,6 +1,7 @@ package ru.ulstu.manager.service; import org.springframework.stereotype.Service; +import ru.ulstu.admin.model.ManagerForm; import ru.ulstu.manager.model.Manager; import ru.ulstu.manager.repository.ManagerRepository; @@ -17,4 +18,16 @@ public class ManagerService { public List getManagers() { return managerRepository.findAll(); } + + public Manager getManagerById(Integer id) { + return managerRepository.findById(id).orElseThrow(() -> new RuntimeException("Manager not found by id")); + } + + public void save(Manager manager) { + managerRepository.save(manager); + } + + public void deleteManager(ManagerForm managerForm) { + managerRepository.deleteById(managerForm.getId()); + } } diff --git a/src/main/resources/templates/admin/editManager.html b/src/main/resources/templates/admin/editManager.html new file mode 100644 index 0000000..6b02dc8 --- /dev/null +++ b/src/main/resources/templates/admin/editManager.html @@ -0,0 +1,37 @@ + + +
+

Редактирование научного руководителя:

+
+ +
+ + +

+ Не может быть пустым +

+
+ + + + Отмена +
+ + +
+ diff --git a/src/main/resources/templates/admin/managersList.html b/src/main/resources/templates/admin/managersList.html new file mode 100644 index 0000000..217f228 --- /dev/null +++ b/src/main/resources/templates/admin/managersList.html @@ -0,0 +1,17 @@ + + + + diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index a39832c..2f47d7f 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -60,6 +60,7 @@ aria-haspopup="true" aria-expanded="false">Администратору