Начало решения проблем

This commit is contained in:
DyCTaTOR 2024-06-05 20:37:05 +04:00
parent e2d54386c9
commit b08c3f8162
9 changed files with 1184 additions and 97 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -19,7 +19,7 @@ import jakarta.validation.Valid;
@Controller @Controller
@RequestMapping(DepartmentController.URL) @RequestMapping(DepartmentController.URL)
public class DepartmentController { public class DepartmentController {
public static final String URL = "/departments"; public static final String URL = Constants.ADMIN_PREFIX + "/departments";
private static final String DEPARTMENTS_VIEW = "departments"; private static final String DEPARTMENTS_VIEW = "departments";
private static final String DEPARTMENTS_EDIT_VIEW = "department-edit"; private static final String DEPARTMENTS_EDIT_VIEW = "department-edit";
private static final String DEPARTMENTS_ATTRIBUTE = "departments"; private static final String DEPARTMENTS_ATTRIBUTE = "departments";

View File

@ -23,7 +23,7 @@ import com.example.demo.entrysData.service.EntrysDataService;
@Controller @Controller
@RequestMapping(EntrysDataController.URL) @RequestMapping(EntrysDataController.URL)
public class EntrysDataController { public class EntrysDataController {
public static final String URL = "/users"; public static final String URL = Constants.ADMIN_PREFIX + "/users";
private static final String USERS_VIEW = "users"; private static final String USERS_VIEW = "users";
private static final String USERS_EDIT_VIEW = "user-edit"; private static final String USERS_EDIT_VIEW = "user-edit";
private static final String USERS_ATTRIBUTE = "users"; private static final String USERS_ATTRIBUTE = "users";

View File

@ -0,0 +1,108 @@
package com.example.demo.news.api;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.core.configuration.Constants;
import com.example.demo.department.api.DepartmentDto;
import com.example.demo.department.service.DepartmentService;
import com.example.demo.news.model.NewsEntity;
import com.example.demo.news.service.NewsService;
import jakarta.validation.Valid;
@Controller
@RequestMapping(NewsAdminController.URL)
public class NewsAdminController {
public static final String URL = Constants.ADMIN_PREFIX + "/news";
private static final String NEWS_EDIT_VIEW = "new-edit";
private static final String NEWS_ATTRIBUTE = "news";
private static final String DEPARTMENT_ATTRIBUTE = "departments";
private final NewsService newsService;
private final ModelMapper modelMapper;
private final DepartmentService departmentService;
public NewsAdminController(NewsService newsService, ModelMapper modelMapper, DepartmentService departmentService) {
this.newsService = newsService;
this.modelMapper = modelMapper;
this.departmentService = departmentService;
}
private NewsDto toDto(NewsEntity entity) {
return modelMapper.map(entity, NewsDto.class);
}
private NewsEntity toEntity(NewsDto dto) {
final NewsEntity entity = modelMapper.map(dto, NewsEntity.class);
entity.setDepartment(departmentService.get(dto.getDepartmentId()));
return entity;
}
private List<DepartmentDto> getDepartments(){
return modelMapper.map(departmentService.getAll(), new TypeToken<List<DepartmentDto>>(){}.getType());
}
@GetMapping("/edit/")
public String create(Model model) {
model.addAttribute(NEWS_ATTRIBUTE, new NewsDto());
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
return NEWS_EDIT_VIEW;
}
@PostMapping("/edit/")
public String create(
@ModelAttribute(name = NEWS_ATTRIBUTE) @Valid NewsDto newItem,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
return NEWS_EDIT_VIEW;
}
newsService.create(toEntity(newItem));
return Constants.REDIRECT_VIEW + URL;
}
@PostMapping("/delete/{id}")
public String delete(
@PathVariable(name = "id") Long id) {
newsService.delete(id);
return Constants.REDIRECT_VIEW + URL;
}
@GetMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
Model model) {
if (id <= 0) {
throw new IllegalArgumentException();
}
model.addAttribute(NEWS_ATTRIBUTE, toDto(newsService.get(id)));
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
return NEWS_EDIT_VIEW;
}
@PostMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@ModelAttribute(name = NEWS_ATTRIBUTE) @Valid NewsDto newItem,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
return NEWS_EDIT_VIEW;
}
if (id <= 0) {
throw new IllegalArgumentException();
}
newsService.update(id, toEntity(newItem));
return Constants.REDIRECT_VIEW + URL;
}
}

View File

@ -7,11 +7,7 @@ import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken; import org.modelmapper.TypeToken;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@ -22,15 +18,11 @@ import com.example.demo.department.service.DepartmentService;
import com.example.demo.news.model.NewsEntity; import com.example.demo.news.model.NewsEntity;
import com.example.demo.news.service.NewsService; import com.example.demo.news.service.NewsService;
import jakarta.validation.Valid;
@Controller @Controller
@RequestMapping(NewsController.URL) @RequestMapping(NewsController.URL)
public class NewsController { public class NewsController {
public static final String URL = "/news"; public static final String URL = "/news";
private static final String NEWS_VIEW = "news"; private static final String NEWS_VIEW = "news";
private static final String NEWS_EDIT_VIEW = "new-edit";
private static final String NEWS_ATTRIBUTE = "news";
private static final String PAGE_ATTRIBUTE = "page"; private static final String PAGE_ATTRIBUTE = "page";
private static final String DEPARTMENT_ATTRIBUTE = "departments"; private static final String DEPARTMENT_ATTRIBUTE = "departments";
private static final String DEPARTMENT_ITEM_ATTRIBUTE = "department"; private static final String DEPARTMENT_ITEM_ATTRIBUTE = "department";
@ -49,20 +41,10 @@ public class NewsController {
return modelMapper.map(entity, NewsDto.class); return modelMapper.map(entity, NewsDto.class);
} }
private NewsEntity toEntity(NewsDto dto) {
final NewsEntity entity = modelMapper.map(dto, NewsEntity.class);
entity.setDepartment(departmentService.get(dto.getDepartmentId()));
return entity;
}
private List<DepartmentDto> getDepartments(){ private List<DepartmentDto> getDepartments(){
return modelMapper.map(departmentService.getAll(), new TypeToken<List<DepartmentDto>>(){}.getType()); return modelMapper.map(departmentService.getAll(), new TypeToken<List<DepartmentDto>>(){}.getType());
} }
private DepartmentDto getDepartment(Long departmentId){
return modelMapper.map(departmentService.get(departmentId), new TypeToken<DepartmentDto>(){}.getType());
}
@GetMapping @GetMapping
public String getAll( public String getAll(
@RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page, @RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page,
@ -81,58 +63,4 @@ public class NewsController {
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments()); model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
return NEWS_VIEW; return NEWS_VIEW;
} }
@GetMapping("/edit/")
public String create(Model model) {
model.addAttribute(NEWS_ATTRIBUTE, new NewsDto());
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
return NEWS_EDIT_VIEW;
}
@PostMapping("/edit/")
public String create(
@ModelAttribute(name = NEWS_ATTRIBUTE) @Valid NewsDto newItem,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
return NEWS_EDIT_VIEW;
}
newsService.create(toEntity(newItem));
return Constants.REDIRECT_VIEW + URL;
}
@PostMapping("/delete/{id}")
public String delete(
@PathVariable(name = "id") Long id) {
newsService.delete(id);
return Constants.REDIRECT_VIEW + URL;
}
@GetMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
Model model) {
if (id <= 0) {
throw new IllegalArgumentException();
}
model.addAttribute(NEWS_ATTRIBUTE, toDto(newsService.get(id)));
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
return NEWS_EDIT_VIEW;
}
@PostMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@ModelAttribute(name = NEWS_ATTRIBUTE) @Valid NewsDto newItem,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
return NEWS_EDIT_VIEW;
}
if (id <= 0) {
throw new IllegalArgumentException();
}
newsService.update(id, toEntity(newItem));
return Constants.REDIRECT_VIEW + URL;
}
} }

View File

@ -16,7 +16,7 @@
<b>Кафедры</b> <b>Кафедры</b>
</span> </span>
<div> <div>
<a href="/departments/edit/" class="btn btn-danger">Добавить кафедру</a> <a href="/admin/departments/edit/" class="btn btn-danger">Добавить кафедру</a>
</div> </div>
<table class="table"> <table class="table">
<caption></caption> <caption></caption>
@ -33,14 +33,14 @@
<th scope="row" th:text="${department.id}"></th> <th scope="row" th:text="${department.id}"></th>
<td th:text="${department.name}"></td> <td th:text="${department.name}"></td>
<td> <td>
<form th:action="@{/departments/edit/{id}(id=${department.id})}" method="get"> <form th:action="@{/admin/departments/edit/{id}(id=${department.id})}" method="get">
<button type="submit" class="btn btn-link button-link"> <button type="submit" class="btn btn-link button-link">
<i class="fa fa-edit">Изменить</i> <i class="fa fa-edit">Изменить</i>
</button> </button>
</form> </form>
</td> </td>
<td> <td>
<form th:action="@{/departments/delete/{id}(id=${department.id})}" method="post"> <form th:action="@{/admin/departments/delete/{id}(id=${department.id})}" method="post">
<button type="submit" class="btn btn-link button-link" <button type="submit" class="btn btn-link button-link"
onclick="return confirm('Вы уверены?')"> onclick="return confirm('Вы уверены?')">
<i class="fa fa-trash">Удалить</i> <i class="fa fa-trash">Удалить</i>

View File

@ -27,7 +27,8 @@
<h2 th:case="0">Данные отсутствуют</h2> <h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*"> <th:block th:case="*">
<div> <div>
<a th:href="@{/directions/edit/(page=${page})}" class="btn btn-danger mt-4">Добавить направление</a> <a th:href="@{/admin/directions/edit/(page=${page})}" class="btn btn-danger mt-4">Добавить
направление</a>
</div> </div>
<table class="table"> <table class="table">
<caption></caption> <caption></caption>
@ -50,7 +51,7 @@
<td th:text="${direction.departmentName}"></td> <td th:text="${direction.departmentName}"></td>
<td th:text="${direction.things}"></td> <td th:text="${direction.things}"></td>
<td> <td>
<form th:action="@{/directions/edit/{id}(id=${direction.id})}" method="get"> <form th:action="@{/admin/directions/edit/{id}(id=${direction.id})}" method="get">
<input type="hidden" th:name="page" th:value="${page}"> <input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link"> <button type="submit" class="btn btn-link button-link">
<i class="fa fa-edit">Изменить</i> <i class="fa fa-edit">Изменить</i>
@ -58,7 +59,7 @@
</form> </form>
</td> </td>
<td> <td>
<form th:action="@{/directions/delete/{id}(id=${direction.id})}" method="post"> <form th:action="@{/admin/directions/delete/{id}(id=${direction.id})}" method="post">
<input type="hidden" th:name="page" th:value="${page}"> <input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link" <button type="submit" class="btn btn-link button-link"
onclick="return confirm('Вы уверены?')"> onclick="return confirm('Вы уверены?')">

View File

@ -39,12 +39,14 @@
</div> </div>
</form> </form>
</div> </div>
<th:block th:switch="${items.size()}"> <th:block sec:authorize="hasRole('ADMIN')">
<h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*">
<div> <div>
<a th:href="@{/admin/news/edit/(page=${page})}" class="btn btn-danger mt-1">Добавить новость</a> <a th:href="@{/admin/news/edit/(page=${page})}" class="btn btn-danger mt-1">Добавить новость</a>
</div> </div>
</th:block>
<th:block th:switch="${items.size()}">
<h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*">
<div class="row"> <div class="row">
<div class="col mt-4 colStyle" th:each="newItem : ${items}"> <div class="col mt-4 colStyle" th:each="newItem : ${items}">
<div class="rectNews d-flex flex-column"> <div class="rectNews d-flex flex-column">
@ -55,6 +57,7 @@
<b scope="row" th:text="${newItem.description}"></b> <b scope="row" th:text="${newItem.description}"></b>
</span> </span>
</div> </div>
<th:block sec:authorize="hasRole('ADMIN')">
<div class="forButtons"> <div class="forButtons">
<form th:action="@{/admin/news/edit/{id}(id=${newItem.id})}" method="get"> <form th:action="@{/admin/news/edit/{id}(id=${newItem.id})}" method="get">
<input type="hidden" th:name="page" th:value="${page}"> <input type="hidden" th:name="page" th:value="${page}">
@ -69,6 +72,7 @@
</button> </button>
</form> </form>
</div> </div>
</th:block>
</div> </div>
</div> </div>
</th:block> </th:block>