Add crud for manager

This commit is contained in:
Anton Romanov 2025-01-29 19:56:56 +04:00
parent 85936721aa
commit 32d8779fd4
8 changed files with 193 additions and 0 deletions

View File

@ -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";
}
}

View File

@ -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;
}
}

View File

@ -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<Manager> 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);
}
}

View File

@ -1,12 +1,20 @@
package ru.ulstu.manager.model; package ru.ulstu.manager.model;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import ru.ulstu.admin.model.ManagerForm;
import ru.ulstu.model.BaseEntity; import ru.ulstu.model.BaseEntity;
@Entity @Entity
public class Manager extends BaseEntity { public class Manager extends BaseEntity {
private String name; private String name;
public Manager() {
}
public Manager(ManagerForm managerForm) {
this.name = managerForm.getName();
}
public String getName() { public String getName() {
return name; return name;
} }

View File

@ -1,6 +1,7 @@
package ru.ulstu.manager.service; package ru.ulstu.manager.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.admin.model.ManagerForm;
import ru.ulstu.manager.model.Manager; import ru.ulstu.manager.model.Manager;
import ru.ulstu.manager.repository.ManagerRepository; import ru.ulstu.manager.repository.ManagerRepository;
@ -17,4 +18,16 @@ public class ManagerService {
public List<Manager> getManagers() { public List<Manager> getManagers() {
return managerRepository.findAll(); 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());
}
} }

View File

@ -0,0 +1,37 @@
<!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/saveManager}"
th:object="${manager}"
method="post">
<input type="hidden" th:field="*{id}">
<div class="form-group">
<label for="name">ФИО</label>
<input th:field="*{name}"
id="name"
type="text"
required
class="form-control"
placeholder="ФИО">
<p th:if="${#fields.hasErrors('name')}"
th:class="${#fields.hasErrors('name')}? error">
Не может быть пустым
</p>
</div>
<button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>
<button name="delete"
type="submit"
class="btn btn-outline-dark"
onclick="return confirm('Удалить запись?')">
Удалить
</button>
<a href="/admin/aspirants" 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

@ -0,0 +1,17 @@
<!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">
<a href="/admin/editManager/0" class="btn btn-outline-dark">
<i class="fa fa-plus-square" aria-hidden="true"> Добавить научного руководителя</i>
</a>
<ul>
<li th:each="m : ${managers}">
<a th:href="@{'/admin/editManager/' + ${m.id}}">
<span th:text="${m.name}"></span>
</a>
</li>
</ul>
</div>
</html>

View File

@ -60,6 +60,7 @@
aria-haspopup="true" aria-expanded="false">Администратору</a> aria-haspopup="true" aria-expanded="false">Администратору</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown"> <div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/admin/indicators">Список показателей</a> <a class="dropdown-item" href="/admin/indicators">Список показателей</a>
<a class="dropdown-item" href="/admin/managers">Список научных руководителей</a>
<a class="dropdown-item" href="/admin/aspirants">Список аспирантов</a> <a class="dropdown-item" href="/admin/aspirants">Список аспирантов</a>
<a class="dropdown-item" href="/admin">Новости и заседания</a> <a class="dropdown-item" href="/admin">Новости и заседания</a>
</div> </div>