Исправленная 4
This commit is contained in:
parent
ab784c2d69
commit
8f8d048296
@ -35,8 +35,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);
|
||||||
|
@ -4,28 +4,36 @@ 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.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;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(EntrysDataController.URL)
|
@RequestMapping(EntrysDataController.URL)
|
||||||
public class EntrysDataController {
|
public class EntrysDataController {
|
||||||
public static final String URL = Constants.ADMIN_PREFIX + "/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 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;
|
||||||
@ -39,27 +47,30 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// private EntrysDataEntity toEntity(EntrysDataDto dto) {
|
private EntrysDataEntity toEntity(EntrysDataDto dto) {
|
||||||
// final EntrysDataEntity entity = modelMapper.map(dto, EntrysDataEntity.class);
|
final EntrysDataEntity entity = modelMapper.map(dto, EntrysDataEntity.class);
|
||||||
// entity.setDepartment(departmentService.get(dto.getDepartmentId()));
|
entity.setDepartment(departmentService.get(dto.getDepartmentId()));
|
||||||
// return entity;
|
return entity;
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
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
|
@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.
|
||||||
@ -70,28 +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,
|
||||||
// @PutMapping("/{id}")
|
Model model,
|
||||||
// public EntrysDataDto update(@PathVariable(name = "id") Long id, @RequestBody EntrysDataDto dto) {
|
RedirectAttributes redirectAttributes){
|
||||||
// return toDto(entrysDataService.update(id, toEntity(dto)));
|
if (bindingResult.hasErrors()) {
|
||||||
// }
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return USERS_EDIT_VIEW;
|
||||||
@DeleteMapping("/{id}")
|
}
|
||||||
public EntrysDataDto delete(@PathVariable(name = "id") Long id) {
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
return toDto(entrysDataService.delete(id));
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/count")
|
@PostMapping("/delete/{id}")
|
||||||
public List<EntrysDataGroupedDto> getCount() {
|
public String delete(
|
||||||
return entrysDataService.getCount().stream().map(this::toDto).toList();
|
@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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,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);
|
||||||
|
@ -60,7 +60,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>
|
||||||
|
|
||||||
|
@ -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>
|
||||||
|
@ -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">
|
||||||
|
@ -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>
|
||||||
|
@ -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>
|
Loading…
Reference in New Issue
Block a user