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

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
@RequestMapping(DepartmentController.URL)
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_EDIT_VIEW = "department-edit";
private static final String DEPARTMENTS_ATTRIBUTE = "departments";

View File

@ -23,7 +23,7 @@ import com.example.demo.entrysData.service.EntrysDataService;
@Controller
@RequestMapping(EntrysDataController.URL)
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_EDIT_VIEW = "user-edit";
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.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 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.service.NewsService;
import jakarta.validation.Valid;
@Controller
@RequestMapping(NewsController.URL)
public class NewsController {
public static final String URL = "/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 DEPARTMENT_ATTRIBUTE = "departments";
private static final String DEPARTMENT_ITEM_ATTRIBUTE = "department";
@ -49,20 +41,10 @@ public class NewsController {
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());
}
private DepartmentDto getDepartment(Long departmentId){
return modelMapper.map(departmentService.get(departmentId), new TypeToken<DepartmentDto>(){}.getType());
}
@GetMapping
public String getAll(
@RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page,
@ -81,58 +63,4 @@ public class NewsController {
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
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>
</span>
<div>
<a href="/departments/edit/" class="btn btn-danger">Добавить кафедру</a>
<a href="/admin/departments/edit/" class="btn btn-danger">Добавить кафедру</a>
</div>
<table class="table">
<caption></caption>
@ -33,14 +33,14 @@
<th scope="row" th:text="${department.id}"></th>
<td th:text="${department.name}"></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">
<i class="fa fa-edit">Изменить</i>
</button>
</form>
</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"
onclick="return confirm('Вы уверены?')">
<i class="fa fa-trash">Удалить</i>

View File

@ -27,7 +27,8 @@
<h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*">
<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>
<table class="table">
<caption></caption>
@ -50,7 +51,7 @@
<td th:text="${direction.departmentName}"></td>
<td th:text="${direction.things}"></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}">
<button type="submit" class="btn btn-link button-link">
<i class="fa fa-edit">Изменить</i>
@ -58,7 +59,7 @@
</form>
</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}">
<button type="submit" class="btn btn-link button-link"
onclick="return confirm('Вы уверены?')">

View File

@ -39,12 +39,14 @@
</div>
</form>
</div>
<th:block sec:authorize="hasRole('ADMIN')">
<div>
<a th:href="@{/admin/news/edit/(page=${page})}" class="btn btn-danger mt-1">Добавить новость</a>
</div>
</th:block>
<th:block th:switch="${items.size()}">
<h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*">
<div>
<a th:href="@{/admin/news/edit/(page=${page})}" class="btn btn-danger mt-1">Добавить новость</a>
</div>
<div class="row">
<div class="col mt-4 colStyle" th:each="newItem : ${items}">
<div class="rectNews d-flex flex-column">
@ -55,20 +57,22 @@
<b scope="row" th:text="${newItem.description}"></b>
</span>
</div>
<div class="forButtons">
<form th:action="@{/admin/news/edit/{id}(id=${newItem.id})}" method="get">
<input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link">
<i class="fa fa-edit">Изменить</i>
</form>
<form th:action="@{/admin/news/delete/{id}(id=${newItem.id})}" method="post">
<input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link"
onclick="return confirm('Вы уверены?')">
<i class="fa fa-trash">Удалить</i>
</button>
</form>
</div>
<th:block sec:authorize="hasRole('ADMIN')">
<div class="forButtons">
<form th:action="@{/admin/news/edit/{id}(id=${newItem.id})}" method="get">
<input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link">
<i class="fa fa-edit">Изменить</i>
</form>
<form th:action="@{/admin/news/delete/{id}(id=${newItem.id})}" method="post">
<input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link"
onclick="return confirm('Вы уверены?')">
<i class="fa fa-trash">Удалить</i>
</button>
</form>
</div>
</th:block>
</div>
</div>
</th:block>