Skip to content

Commit

Permalink
fix: composite key in CartItem
Browse files Browse the repository at this point in the history
  • Loading branch information
oksana-miazina committed May 26, 2024
1 parent ada5086 commit a0709c5
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
@Mapper(config = MapperConfig.class)
public interface CartItemMapper {
@Mappings({
@Mapping(source = "id.book.id", target = "bookId"),
@Mapping(source = "id.book.title", target = "bookTitle")
@Mapping(source = "book.id", target = "bookId"),
@Mapping(source = "book.title", target = "bookTitle")
})
CartItemResponseDto toDto(CartItem cartItem);

@Mappings({
@Mapping(source = "bookId", target = "id.book.id")
@Mapping(source = "bookId", target = "book.id")
})
CartItem toModel(CartItemRequestDto dto);
}
13 changes: 13 additions & 0 deletions src/main/java/mate/academy/bookstore/model/CartItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import jakarta.persistence.Column;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapsId;
import jakarta.persistence.Table;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand All @@ -19,6 +22,16 @@ public class CartItem {
@EmbeddedId
private CartItemKey id;

@MapsId("shoppingCartId")
@ManyToOne(optional = false)
@JoinColumn(name = "shopping_cart_id", nullable = false)
private ShoppingCart shoppingCart;

@MapsId("bookId")
@ManyToOne(optional = false)
@JoinColumn(name = "book_id", nullable = false)
private Book book;

@Column(nullable = false)
@EqualsAndHashCode.Exclude
private int quantity;
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/mate/academy/bookstore/model/CartItemKey.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package mate.academy.bookstore.model;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -13,11 +12,9 @@
@NoArgsConstructor
@AllArgsConstructor
public class CartItemKey implements Serializable {
@ManyToOne(optional = false)
@JoinColumn(name = "shopping_cart_id", nullable = false)
private ShoppingCart shoppingCart;
@Column(name = "shopping_cart_id", nullable = false)
private Long shoppingCartId;

@ManyToOne(optional = false)
@JoinColumn(nullable = false)
private Book book;
@Column(name = "book_id", nullable = false)
private Long bookId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ShoppingCart {
@JoinColumn(name = "user_id", nullable = false, unique = true)
private User user;

@OneToMany(mappedBy = "id.shoppingCart", fetch = FetchType.LAZY,
@OneToMany(mappedBy = "shoppingCart", fetch = FetchType.LAZY,
cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@EqualsAndHashCode.Exclude
@ToString.Exclude
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package mate.academy.bookstore.repository;

import java.util.List;
import java.util.Set;
import mate.academy.bookstore.model.CartItem;
import mate.academy.bookstore.model.CartItemKey;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CartItemRepository extends JpaRepository<CartItem, CartItemKey> {
Set<CartItem> getById_Book_IdIn(List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public interface ShoppingCartRepository extends JpaRepository<ShoppingCart, Long> {
@Query("FROM ShoppingCart sc "
+ "LEFT JOIN FETCH sc.cartItems ci "
+ "JOIN FETCH ci.id.book "
+ "JOIN FETCH ci.book "
+ "WHERE sc.user.id = :userId")
ShoppingCart getByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mate.academy.bookstore.service.impl;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import mate.academy.bookstore.dto.CartItemRequestDto;
Expand All @@ -10,7 +9,6 @@
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;
Expand Down Expand Up @@ -50,14 +48,14 @@ public ShoppingCartDto save(ShoppingCartRequestDto dto, User user) {
shoppingCart.setId(user.getId());

shoppingCart.setCartItems(shoppingCart.getCartItems().stream()
.peek(i -> i.getId().setShoppingCart(shoppingCart))
.peek(cartItem -> cartItem.setShoppingCart(shoppingCart))
.peek(cartItem -> cartItem.setId(
new CartItemKey(shoppingCart.getId(), cartItem.getBook().getId())
))
.collect(Collectors.toSet())
);

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

return shoppingCartMapper.toDto(savedShoppingCart);
}

Expand All @@ -71,7 +69,7 @@ public ShoppingCartDto getByUserId(Long id) {
public void deleteCartItem(Long bookId, Long userId) {
ShoppingCart shoppingCart = new ShoppingCart(userId);
Book book = new Book(bookId);
CartItemKey id = new CartItemKey(shoppingCart, book);
CartItemKey id = new CartItemKey(shoppingCart.getId(), book.getId());

if (!cartItemRepository.existsById(id)) {
throw new EntityNotFoundException(
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ databaseChangeLog:
file: /db/changelog/changes/10-loaddata-categories.yml
- include:
file: /db/changelog/changes/11-create-books-categories-table.yml
- include:
file: /db/changelog/changes/12-loaddata-books-categories.yml
- include:
file: /db/changelog/changes/13-create-shopping-carts-table.yml
- include:
Expand Down

0 comments on commit a0709c5

Please sign in to comment.