Исправленная 4

This commit is contained in:
DyCTaTOR 2024-06-07 16:51:34 +04:00
parent 855813a4d5
commit 92dd97a119
10 changed files with 155 additions and 49 deletions

5
.hintrc Normal file
View File

@ -0,0 +1,5 @@
{
"extends": [
"development"
]
}

Binary file not shown.

View File

@ -67,8 +67,12 @@ public class DirectionsController {
@GetMapping
public String getAllWithDepartment(
@RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page,
@RequestParam(name="reset", required = false, defaultValue = "false") String reset,
Model model,
String name) {
if (reset.contains("true")){
name = "";
}
final Map<String, Object> attributes = PageAttributesMapper.
toAttributes(directionsService.getAllWithDepartment(page, Constants.DEFAULT_PAGE_SIZE, name), this::toGroupedDto);
model.addAllAttributes(attributes);

View File

@ -4,22 +4,23 @@ import java.util.List;
import java.util.Map;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.example.demo.core.api.PageAttributesMapper;
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.entrysData.model.EntrysDataEntity;
import com.example.demo.entrysData.model.EntrysDataGrouped;
import com.example.demo.entrysData.model.EntrysDataGroupedDepartment;
import com.example.demo.entrysData.service.EntrysDataService;
@ -31,6 +32,8 @@ public class EntrysDataController {
public static final String URL = "/users";
private static final String USERS_VIEW = "users";
private static final String USERS_EDIT_VIEW = "user-edit";
private static final String DEPARTMENTS_ATTRIBUTE = "departments";
private static final String DEPARTMENT_ITEM_ATTRIBUTE = "departmentItem";
private static final String USERS_ATTRIBUTE = "users";
private static final String PAGE_ATTRIBUTE = "page";
private final EntrysDataService entrysDataService;
@ -44,14 +47,10 @@ public class EntrysDataController {
this.departmentService = departmentService;
}
private EntrysDataDto toDto(EntrysDataEntity entity) {
private EntrysDataDto toDto(EntrysDataEntity entity){
return modelMapper.map(entity, EntrysDataDto.class);
}
private EntrysDataGroupedDto toDto(EntrysDataGrouped entity) {
return modelMapper.map(entity, EntrysDataGroupedDto.class);
}
private EntrysDataGroupedDepartmentDto toDto(EntrysDataGroupedDepartment entity){
return modelMapper.map(entity, EntrysDataGroupedDepartmentDto.class);
}
@ -62,17 +61,16 @@ public class EntrysDataController {
return entity;
}
// @GetMapping
// public PageDto<EntrysDataDto> getAll(
// @RequestParam(name = "departmentId", defaultValue = "0") Long departmentId,
// @RequestParam(name = "page", defaultValue = "0") int page,
// @RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
// return PageDtoMapper.toDto(entrysDataService.getAll(departmentId, page, size), this::toDto);
// }
private List<DepartmentDto> getDepartments(){
return modelMapper.map(departmentService.getAll(), new TypeToken<List<DepartmentDto>>(){}.getType());
}
private DepartmentDto getDepartment(Long id){
return modelMapper.map(departmentService.get(id), new TypeToken<DepartmentDto>(){}.getType());
}
@GetMapping
public String getAll(
@RequestParam(name = "departmentId", defaultValue = "0") Long departmentId,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model){
final Map<String, Object> attributes = PageAttributesMapper.
@ -83,35 +81,72 @@ public class EntrysDataController {
return USERS_VIEW;
}
@GetMapping("/{id}")
public EntrysDataDto get(@PathVariable(name = "id") Long id) {
return toDto(entrysDataService.get(id));
@GetMapping("/edit/")
public String create(Model model,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page) {
model.addAttribute(USERS_ATTRIBUTE, new EntrysDataDto());
model.addAttribute(DEPARTMENTS_ATTRIBUTE, getDepartments());
model.addAttribute(PAGE_ATTRIBUTE, page);
return USERS_EDIT_VIEW;
}
@PostMapping
public EntrysDataDto create(@RequestBody @Valid EntrysDataDto dto) {
return toDto(entrysDataService.create(toEntity(dto)));
@PostMapping("/edit/")
public String create(
@ModelAttribute(name = USERS_ATTRIBUTE) @Valid EntrysDataDto dto,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes){
if (bindingResult.hasErrors()) {
model.addAttribute(PAGE_ATTRIBUTE, page);
return USERS_EDIT_VIEW;
}
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
entrysDataService.create(toEntity(dto));
return Constants.REDIRECT_VIEW + URL;
}
@GetMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
if (id <= 0) {
throw new IllegalArgumentException();
}
EntrysDataEntity entity = entrysDataService.get(id);
model.addAttribute(USERS_ATTRIBUTE, toDto(entity));
model.addAttribute(DEPARTMENTS_ATTRIBUTE, getDepartments());
model.addAttribute(PAGE_ATTRIBUTE, page);
return USERS_EDIT_VIEW;
}
@PostMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
@ModelAttribute(name = USERS_ATTRIBUTE) @Valid EntrysDataDto dto,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
return USERS_EDIT_VIEW;
}
if (id <= 0) {
throw new IllegalArgumentException();
}
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
entrysDataService.update(id, toEntity(dto));
return Constants.REDIRECT_VIEW + URL;
}
@PutMapping("/{id}")
public EntrysDataDto update(@PathVariable(name = "id") Long id, @RequestBody EntrysDataDto dto) {
return toDto(entrysDataService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public EntrysDataDto delete(@PathVariable(name = "id") Long id) {
return toDto(entrysDataService.delete(id));
}
@PutMapping("/{id}/password")
public EntrysDataDto updatePassword(@PathVariable(name = "id") Long id,
@RequestParam(name = "newPassword") String newPas,
@RequestParam(name = "oldPassword") String oldPas) {
return toDto(entrysDataService.updatePassword(id, newPas, oldPas));
}
@GetMapping("/count")
public List<EntrysDataGroupedDto> getCount() {
return entrysDataService.getCount().stream().map(this::toDto).toList();
}
@PostMapping("/delete/{id}")
public String delete(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
RedirectAttributes redirectAttributes) {
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
entrysDataService.delete(id);
return Constants.REDIRECT_VIEW + URL;
}
}

View File

@ -68,7 +68,12 @@ public class NewsController {
@RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page,
@RequestParam(name="departmentId", required = false) Long departmentId,
@RequestParam(name=DESCRIPTION_ATTRIBUTE, required = false) String description,
@RequestParam(name="reset", required = false, defaultValue = "false") String reset,
Model model) {
if (reset.contains("true")){
description = "";
departmentId = null;
}
final Map<String, Object> attributes = PageAttributesMapper.
toAttributes(newsService.getAll(page, Constants.DEFAULT_PAGE_SIZE_FOR_NEWS, departmentId, description), this::toDto);
model.addAllAttributes(attributes);

View File

@ -16,7 +16,7 @@
<body class="h-100 d-flex flex-column">
<nav class="navbar navbar-expand-md my-navbar" data-bs-theme="dark">
<div class="container-fluid">
<a class="navbar-brand" href="/">
<a class="navbar-brand" href="/news">
<i></i>
UlSTU
</a>
@ -50,7 +50,7 @@
<main class="container-fluid p-2" layout:fragment="content">
</main>
<footer class="my-footer mt-auto d-flex flex-shrink-0 justify-content-center align-items-center">
Автор, [[${#dates.year(#dates.createNow())}]]
Адреса: ул. Северный Венец, 32; ул. Андрея Блаженного, 3
</footer>
</body>

View File

@ -2,7 +2,7 @@
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Редактировать новость</title>
<title>Редактировать направление</title>
</head>
<body>

View File

@ -14,7 +14,10 @@
</label>
<div class="d-flex justify-content-center">
<form class="w-25" method="get">
<div class="input-group">
<button class = "btn button-link" name="reset" value="true" type="submit">
<i class = "fa fa-refresh">Сброс</i>
</button>
<div class="input-group mt-1">
<input class="form-control" type="text" name="name" th:value="${name}"
placeholder="Поиск направлений..." />
<button class="btn btn-primary" type="submit">

View File

@ -14,7 +14,10 @@
</span>
<div class="d-flex justify-content-center">
<form class="w-25" method="get">
<div class="input-group">
<button class = "btn button-link" name="reset" value="true" type="submit">
<i class = "fa fa-refresh">Сброс</i>
</button>
<div class="input-group mt-1">
<input class="form-control" type="text" name="description" th:value="${description}"
placeholder="Поиск новостей..." />
<button class="btn btn-primary" type="submit">
@ -26,6 +29,7 @@
<label for="departmentId" class="form-label" />
<select id="departmentId" class="form-control" name="departmentId">
<option th:if="${departmentId} == null" value="" selected>Без фильтра</option>
<option th:if="${departmentId} != null" value="">Без фильтра</option>
<option th:each="departmentItem : ${departments}" th:value="${departmentItem.id}"
th:text="${departmentItem.name}" th:selected="${departmentId} == ${departmentItem.id}">
</option>

View File

@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Редактировать пользователя</title>
</head>
<body>
<main layout:fragment="content">
<form action="#" th:action="@{/users/edit/{id}(id=${users.id},(page=${page}))}"
th:object="${users}" method="post">
<div class="mb-3">
<label for="id" class="form-label">ID</label>
<input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
</div>
<div class="mb-3">
<label for="role" class="form-label">Роль</label>
<input type="text" th:field="*{role}" id="role" class="form-control" readonly disabled>
</div>
<div class="mb-3">
<label for="login" class="form-label">Логин</label>
<input type="text" th:field="*{login}" id="login" class="form-control">
<div th:if="${#fields.hasErrors('login')}" th:errors="*{login}" class="invalid-feedback"></div>
</div>
<div class="mb-3">
<label for="password" class="form-label">Пароль</label>
<input type="password" th:field="*{password}" id="password" class="form-control">
<div th:if="${#fields.hasErrors('password')}" th:errors="*{password}" class="invalid-feedback">
</div>
</div>
<div class="mb-3">
<label for="departmentId" class="form-label">Кафедра</label>
<select th:field="*{departmentId}" id="departmentId" class="form-control">
<option th:if="${departmentId} == null" value="" selected>Выберите кафедру</option>
<option th:each="departmentItem : ${departments}" th:value="${departmentItem.id}"
th:text="${departmentItem.name}" th:selected="${departmentId} == ${departmentItem.id}">
</option>
</select>
<div th:if="${#fields.hasErrors('departmentId')}" th:errors="*{departmentId}" class="invalid-feedback">
</div>
</div>
<div class="mb-3 d-flex flex-row">
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>
<a class="btn btn-secondary button-fixed-width" th:href="@{/users(page=${page})}">Отмена</a>
</div>
</form>
</main>
</body>
</html>