Исправленная 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 @GetMapping
public String getAllWithDepartment( public String getAllWithDepartment(
@RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page, @RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page,
@RequestParam(name="reset", required = false, defaultValue = "false") String reset,
Model model, Model model,
String name) { String name) {
if (reset.contains("true")){
name = "";
}
final Map<String, Object> attributes = PageAttributesMapper. final Map<String, Object> attributes = PageAttributesMapper.
toAttributes(directionsService.getAllWithDepartment(page, Constants.DEFAULT_PAGE_SIZE, name), this::toGroupedDto); toAttributes(directionsService.getAllWithDepartment(page, Constants.DEFAULT_PAGE_SIZE, name), this::toGroupedDto);
model.addAllAttributes(attributes); model.addAllAttributes(attributes);

View File

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

View File

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

View File

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

View File

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

View File

@ -14,7 +14,10 @@
</label> </label>
<div class="d-flex justify-content-center"> <div class="d-flex justify-content-center">
<form class="w-25" method="get"> <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}" <input class="form-control" type="text" name="name" th:value="${name}"
placeholder="Поиск направлений..." /> placeholder="Поиск направлений..." />
<button class="btn btn-primary" type="submit"> <button class="btn btn-primary" type="submit">

View File

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