Новые доработки и новые ошибки

This commit is contained in:
DyCTaTOR 2024-05-20 20:50:40 +04:00
parent d8b298fc5f
commit ca5b3d06f1
7 changed files with 125 additions and 51 deletions

Binary file not shown.

View File

@ -51,7 +51,7 @@ public class DirectionsController {
@GetMapping
public String getAllWithDepartment(
@RequestParam(name="PAGE_ATTRIBUTE", defaultValue = "0") int page,
@RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
final Map<String, Object> attributes = PageAttributesMapper.
toAttributes(directionsService.getAllWithDepartment(page, Constants.DEFAULT_PAGE_SIZE), this::toGroupedDto);

View File

@ -6,6 +6,7 @@ import java.util.stream.StreamSupport;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -81,9 +82,8 @@ public class DirectionsService {
return existsEntity;
}
@Transactional
@Transactional(readOnly = true)
public Page<DirectionsGrouped> getAllWithDepartment(int page, int size) {
final Pageable pageRequest = PageRequest.of(page, size);
return repository.findAllWithDepartment(pageRequest);
return repository.findAllWithDepartment(PageRequest.of(page, size, Sort.by("id")));
}
}

View File

@ -1,15 +1,20 @@
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.RequestBody;
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;
@ -23,6 +28,7 @@ public class NewsController {
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 DEPARTMENT_ATTRIBUTE = "departments";
private final NewsService newsService;
private final ModelMapper modelMapper;
private final DepartmentService departmentService;
@ -43,11 +49,9 @@ public class NewsController {
return entity;
}
// @GetMapping
// public List<NewsDto> getAll(@RequestParam(name = "departmentId", defaultValue
// = "0") Long departmentId) {
// return newsService.getAll(departmentId).stream().map(this::toDto).toList();
// }
private List<DepartmentDto> getDepartments(){
return modelMapper.map(departmentService.getAll(), new TypeToken<List<DepartmentDto>>(){}.getType());
}
@GetMapping
public String getAll(Model model) {
@ -56,14 +60,22 @@ public class NewsController {
return NEWS_VIEW;
}
@GetMapping("/{id}")
public NewsDto get(@PathVariable(name = "id") Long id) {
return toDto(newsService.get(id));
@GetMapping("/edit/")
public String create(Model model) {
model.addAttribute(NEWS_ATTRIBUTE, new NewsDto());
return NEWS_EDIT_VIEW;
}
@PostMapping
public NewsDto create(@RequestBody @Valid NewsDto dto) {
return toDto(newsService.create(toEntity(dto)));
@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}")
@ -81,17 +93,23 @@ public class NewsController {
throw new IllegalArgumentException();
}
model.addAttribute(NEWS_ATTRIBUTE, toDto(newsService.get(id)));
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
return NEWS_EDIT_VIEW;
}
// @PutMapping("/{id}")
// public NewsDto update(@PathVariable(name = "id") Long id, @RequestBody
// NewsDto dto) {
// return toDto(newsService.update(id, toEntity(dto)));
// }
// @DeleteMapping("/{id}")
// public NewsDto delete(@PathVariable(name = "id") Long id) {
// return toDto(newsService.delete(id));
// }
@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

@ -86,7 +86,7 @@ td form {
}
.mainSt {
color: #060647;
color: #008B8B;
font-size: 50px;
}

View File

@ -0,0 +1,45 @@
<!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="@{/news/edit/{id}(id=${news.id})}" th:object="${news}" 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="name" class="form-label">Заголовок</label>
<input type="text" th:field="*{name}" id="name" class="form-control">
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="invalid-feedback"></div>
</div>
<div class="mb-3">
<label for="description" class="form-label">Описание</label>
<input type="text" th:field="*{description}" id="description" class="form-control">
<div th:if="${#fields.hasErrors('description')}" th:errors="*{description}" 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="">Выберите кафедру</option>
<option th:unless="*{departmentId} == null" th:value="*{departmentId}" th:text="*{name}"></option>
<option th:each=" department : ${departments}" th:value="${department.id}"
th:text="${department.name}" th:selected="*{departmentId} == ${department.id}">
</option>
</select>
<div th:if="${#fields.hasErrors('clientId')}" th:errors="*{clientId}" 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" href="/news">Отмена</a>
</div>
</form>
</main>
</body>
</html>

View File

@ -13,31 +13,42 @@
<b>Новости</b>
</span>
<div>
<a href="/news/edit/" class="btn btn-primary">Добавить новость</a>
</div>
<div class="row">
<div class="col mt-4" th:each="newItem : ${news}">
<div class="rectNews d-flex flex-column">
<img class="imageNew1" src="./images/New1.png" alt th:alt="${newItem.name}" width="100%">
<div class="rectNewsTextBox">
<span class="rectNewsText">
<b scope="row" th:text="${newItem.description}"></b>
<form th:action="@{/news/edit/{id}(id=${newItem.id})}" method="get">
<a type="submit" class="btn btn-link button-link">
<i class="fa fa-edit"></i>
</a>
</form>
<form th:action="@{/news/delete/{id}(id=${newItem.id})}" method="post">
<a type="submit" class="btn btn-link button-link"
onclick="return confirm('Вы уверены?')">
<i class="fa fa-trash"></i>
</a>
</form>
</span>
</div>
</div>
</div>
<a href="/news/edit/" class="btn btn-danger">Добавить новость</a>
</div>
<table class="table">
<caption></caption>
<thead>
<tr>
<th scope="col" class="w-5">ID</th>
<th scope="col" class="w-25">Заголовок</th>
<th scope="col" class="w-50">Описание</th>
<th scope="col" class="w-10"></th>
<th scope="col" class="w-10"></th>
</tr>
</thead>
<tbody>
<tr th:each="newItem : ${news}">
<th scope="row" th:text="${newItem.id}"></th>
<td th:text="${newItem.name}"></td>
<td th:text="${newItem.description}"></td>
<td>
<form th:action="@{/news/edit/{id}(id=${newItem.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="@{/news/delete/{id}(id=${newItem.id})}" method="post">
<button type="submit" class="btn btn-link button-link"
onclick="return confirm('Вы уверены?')">
<i class="fa fa-trash">Удалить</i>
</button>
</form>
</td>
</tr>
</tbody>
</table>
</main>
</body>