подправил заказ

This commit is contained in:
Максим Яковлев 2024-05-12 14:27:43 +04:00
parent 884b2aaad6
commit 386203ef31
5 changed files with 56 additions and 15 deletions

View File

@ -1,6 +1,6 @@
plugins { plugins {
id 'java' id 'java'
id 'org.springframework.boot' version '3.2.4' id 'org.springframework.boot' version '3.2.5'
id 'io.spring.dependency-management' version '1.1.4' id 'io.spring.dependency-management' version '1.1.4'
} }

Binary file not shown.

View File

@ -61,13 +61,13 @@ public class OrderService {
} }
@Transactional @Transactional
public List<OrderEntity> createAll(long userId, List<OrderEntity> entities) { public OrderEntity createAll(long userId, OrderEntity entitiy) {
if (entities == null || entities.isEmpty()) { if (entitiy == null) {
throw new IllegalArgumentException("Orders list is null or empty"); throw new IllegalArgumentException("Orders list is null or empty");
} }
final UserEntity existsUser = userService.get(userId); final UserEntity existsUser = userService.get(userId);
entities.forEach(entity -> entity.setUser(existsUser)); entitiy.setUser(existsUser);
return StreamSupport.stream(repository.saveAll(entities).spliterator(), false).toList(); return repository.save(entitiy);
} }
@Transactional @Transactional

View File

@ -67,18 +67,23 @@ public class UserCartController {
return dto; return dto;
} }
private List<OrderEntity> toOrderEntities(Collection<UserCartDto> dtos) { // private List<GameEntity> toGameEntities(Collection<UserCartDto> dtos){
// final GameEntity entity = modelMapper.map(dto, GameEntity.class);
// entity.setType(typeService.get(dto.getTypeId()));
// var genres = dto.getGenres();
// List<GenreEntity> genresList = genreService.getAllById(genres);
// for (var genre : genresList) {
// entity.setGenres(genre);
// }
// return entity;
// }
private OrderEntity toOrderEntities(Collection<UserCartDto> dtos) {
final Set<Long> gameIds = dtos.stream() final Set<Long> gameIds = dtos.stream()
.map(UserCartDto::getGame) .map(UserCartDto::getGame)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
final Map<Long, GameEntity> games = gameService.getByIds(gameIds).stream() final List<GameEntity> games = gameService.getByIds(gameIds);
.collect(Collectors.toMap(GameEntity::getId, Function.identity())); return new OrderEntity(games);
return dtos.stream()
.map(dto -> {
final OrderEntity entity = modelMapper.map(dto, OrderEntity.class);
entity.setGames(games.get(dto.getGame()));
return entity;
}).toList();
} }
@GetMapping @GetMapping
@ -115,7 +120,7 @@ public class UserCartController {
public String saveCart( public String saveCart(
Model model, Model model,
@AuthenticationPrincipal UserPrincipal principal) { @AuthenticationPrincipal UserPrincipal principal) {
orderService.createAll(principal.getId(), toOrderEntities(cart.values())); orderService.create(principal.getId(), toOrderEntities(cart.values()));
cart.clear(); cart.clear();
return Constants.REDIRECT_VIEW + URL; return Constants.REDIRECT_VIEW + URL;
} }

View File

@ -0,0 +1,36 @@
<!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="@{/admin/game/edit/{id}(id=${type.id})}" th:object="${type}" 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-2">
<label for="type" class="form-label">Тип</label>
<select th:field="*{type}" id="type" class="form-select">
<option selected value="">Укажите тип</option>
<option th:each="type : ${types}" th:value="${type.id}">[[${type.name}]]</option>
</select>
<div th:if="${#fields.hasErrors('type')}" th:errors="*{type}" 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="/admin/type">Отмена</a>
</div>
</form>
</main>
</body>
</html>