Skip to content

Commit

Permalink
fix: response on save cart
Browse files Browse the repository at this point in the history
  • Loading branch information
oksana-miazina committed May 22, 2024
1 parent fa41eb5 commit 1cf4029
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package mate.academy.bookstore.dto;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import java.util.Set;
import lombok.Data;

@Data
public class ShoppingCartRequestDto {
@NotEmpty
@Valid
private Set<CartItemRequestDto> cartItems;
}
1 change: 1 addition & 0 deletions src/main/java/mate/academy/bookstore/model/CartItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public class CartItem {
private CartItemKey id;

@Column(nullable = false)
@EqualsAndHashCode.Exclude
private int quantity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
import mate.academy.bookstore.model.CartItemKey;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Set;

public interface CartItemRepository extends JpaRepository<CartItem, CartItemKey> {
Set<CartItem> getById_Book_IdIn(List<Long> ids);
}
2 changes: 2 additions & 0 deletions src/main/java/mate/academy/bookstore/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public interface BookService {
BookDto updateById(Long id, BookRequestDto bookRequestDto);

void deleteById(Long id);

void validateBooksExistence(List<Long> booksIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ public void deleteById(Long id) {
bookRepository.deleteById(id);
}

public void validateBooksExistence(List<Long> booksIds) {
Set<Long> existingBooks = bookRepository.findByIdIn(booksIds)
.stream()
.map(Book::getId)
.collect(Collectors.toSet());

if (existingBooks.size() != booksIds.size()) {
Set<Long> notFoundBooks = new HashSet<>(booksIds);
notFoundBooks.removeAll(existingBooks);
throw new EntityNotFoundException(
localeService.getMessage("exception.notfound.books") + notFoundBooks
);
}
}

private Book getBookByIdOrThrowException(Long id) {
return bookRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
package mate.academy.bookstore.service.impl;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import mate.academy.bookstore.dto.CartItemRequestDto;
import mate.academy.bookstore.dto.ShoppingCartDto;
import mate.academy.bookstore.dto.ShoppingCartRequestDto;
import mate.academy.bookstore.exception.EntityNotFoundException;
import mate.academy.bookstore.mapper.ShoppingCartMapper;
import mate.academy.bookstore.model.Book;
import mate.academy.bookstore.model.CartItem;
import mate.academy.bookstore.model.CartItemKey;
import mate.academy.bookstore.model.ShoppingCart;
import mate.academy.bookstore.model.User;
import mate.academy.bookstore.repository.BookRepository;
import mate.academy.bookstore.repository.CartItemRepository;
import mate.academy.bookstore.repository.ShoppingCartRepository;
import mate.academy.bookstore.service.BookService;
import mate.academy.bookstore.service.LocaleService;
import mate.academy.bookstore.service.ShoppingCartService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ShoppingCartServiceImpl implements ShoppingCartService {
private final ShoppingCartRepository shoppingCartRepository;
private final CartItemRepository cartItemRepository;
private final BookRepository bookRepository;
private final ShoppingCartMapper shoppingCartMapper;
private final LocaleService localeService;

@PersistenceContext
private EntityManager entityManager;
private final BookService bookService;

@Override
public void createFor(User user) {
Expand All @@ -41,8 +39,12 @@ public void createFor(User user) {
}

@Override
@Transactional
public ShoppingCartDto save(ShoppingCartRequestDto dto, User user) {
List<Long> newBookIds = dto.getCartItems().stream()
.map(CartItemRequestDto::getBookId)
.toList();
bookService.validateBooksExistence(newBookIds);

ShoppingCart shoppingCart = shoppingCartMapper.toModel(dto);
shoppingCart.setUser(user);
shoppingCart.setId(user.getId());
Expand All @@ -52,8 +54,10 @@ public ShoppingCartDto save(ShoppingCartRequestDto dto, User user) {
.collect(Collectors.toSet())
);

ShoppingCart savedShoppingCart = shoppingCartRepository.saveAndFlush(shoppingCart);
entityManager.refresh(savedShoppingCart);
ShoppingCart savedShoppingCart = shoppingCartRepository.save(shoppingCart);
Set<CartItem> savedCartItems = cartItemRepository.getById_Book_IdIn(newBookIds);
savedShoppingCart.setCartItems(savedCartItems);

return shoppingCartMapper.toDto(savedShoppingCart);
}

Expand Down

0 comments on commit 1cf4029

Please sign in to comment.