From 63d490acbdb9cc00945fcccae58d3218661a3d55 Mon Sep 17 00:00:00 2001 From: Ros Date: Wed, 27 Sep 2023 14:43:48 +0100 Subject: [PATCH] Was modified all necessary classes --- .../boot/bookstore/model/ShoppingCart.java | 14 ++++++++------ .../spring/boot/bookstore/model/User.java | 5 ++++- .../bookstore/repository/BookRepository.java | 7 +++++-- .../cartitem/CartItemRepository.java | 3 ++- .../shoppingcart/ShoppingCartRepository.java | 2 ++ .../service/cartitem/CartItemService.java | 5 +++-- .../cartitem/impl/CartItemServiceImpl.java | 19 ++++++++++++------- .../impl/ShoppingCartServiceImpl.java | 15 ++++++++++----- 8 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/main/java/spring/boot/bookstore/model/ShoppingCart.java b/src/main/java/spring/boot/bookstore/model/ShoppingCart.java index e692f7c..5b24709 100644 --- a/src/main/java/spring/boot/bookstore/model/ShoppingCart.java +++ b/src/main/java/spring/boot/bookstore/model/ShoppingCart.java @@ -2,12 +2,13 @@ import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; import jakarta.persistence.ManyToMany; -import jakarta.persistence.ManyToOne; -import jakarta.persistence.MapsId; +import jakarta.persistence.OneToOne; import jakarta.persistence.Table; import java.util.ArrayList; import java.util.List; @@ -27,10 +28,11 @@ @Table(name = "shopping_carts") public class ShoppingCart { @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) // added generated strategy private Long id; - @MapsId - @ManyToOne - @JoinColumn(name = "user_id",nullable = false) + // @MapsId deleted + @OneToOne // change to OneToOne + @JoinColumn(name = "user_id", nullable = false) private User user; @ManyToMany @@ -39,7 +41,7 @@ public class ShoppingCart { inverseJoinColumns = @JoinColumn(name = "cart_items_id")) @ToString.Exclude @EqualsAndHashCode.Exclude - private List cartItems = new ArrayList<>(); + private List cartItems = new ArrayList<>(); @Column(name = "is_deleted", nullable = false) private boolean isDeleted = false; diff --git a/src/main/java/spring/boot/bookstore/model/User.java b/src/main/java/spring/boot/bookstore/model/User.java index bd25515..78e3ab2 100644 --- a/src/main/java/spring/boot/bookstore/model/User.java +++ b/src/main/java/spring/boot/bookstore/model/User.java @@ -10,9 +10,12 @@ import jakarta.persistence.JoinTable; import jakarta.persistence.ManyToMany; import jakarta.persistence.Table; + import java.util.Collection; +import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; + import lombok.Getter; import lombok.Setter; import org.hibernate.annotations.SQLDelete; @@ -45,7 +48,7 @@ public class User implements UserDetails { @JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id")) - private Set roles; + private Set roles = new HashSet<>(); // added HashSet @Column(name = "is_deleted", nullable = false) private boolean isDeleted = false; diff --git a/src/main/java/spring/boot/bookstore/repository/BookRepository.java b/src/main/java/spring/boot/bookstore/repository/BookRepository.java index 307cdc4..ecff507 100644 --- a/src/main/java/spring/boot/bookstore/repository/BookRepository.java +++ b/src/main/java/spring/boot/bookstore/repository/BookRepository.java @@ -1,10 +1,13 @@ package spring.boot.bookstore.repository; import java.util.List; +import java.util.Optional; + import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import spring.boot.bookstore.model.Book; @@ -13,9 +16,9 @@ public interface BookRepository extends JpaRepository, JpaSpecificat List findAllByTitleContainsIgnoreCase(String title); @Query("FROM Book b JOIN b.categories c WHERE c.id =:categoryId") - List findByCategoryId(Long categoryId, Pageable pageable); + List findByCategoryId(@Param("categoryId") Long categoryId, Pageable pageable); // was added @Param("categoryId") and Long categoryId @Query("FROM Book b INNER JOIN FETCH b.categories") - List findAllWithCategories(Pageable peageable); + List findAllWithCategories(Pageable pageable); } diff --git a/src/main/java/spring/boot/bookstore/repository/cartitem/CartItemRepository.java b/src/main/java/spring/boot/bookstore/repository/cartitem/CartItemRepository.java index 96ee460..9fe7168 100644 --- a/src/main/java/spring/boot/bookstore/repository/cartitem/CartItemRepository.java +++ b/src/main/java/spring/boot/bookstore/repository/cartitem/CartItemRepository.java @@ -3,11 +3,12 @@ import java.util.Set; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import spring.boot.bookstore.model.CartItem; @Repository public interface CartItemRepository extends JpaRepository { @Query("SELECT c FROM CartItem c WHERE c.shoppingCart.id =:shoppingCartId") - Set findCartItemByShoppingCartId(Long shoppingCart); + Set findCartItemByShoppingCartId(@Param("shoppingCartId") Long shoppingCart); } diff --git a/src/main/java/spring/boot/bookstore/repository/shoppingcart/ShoppingCartRepository.java b/src/main/java/spring/boot/bookstore/repository/shoppingcart/ShoppingCartRepository.java index 6135c1a..ae05044 100644 --- a/src/main/java/spring/boot/bookstore/repository/shoppingcart/ShoppingCartRepository.java +++ b/src/main/java/spring/boot/bookstore/repository/shoppingcart/ShoppingCartRepository.java @@ -1,9 +1,11 @@ package spring.boot.bookstore.repository.shoppingcart; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import spring.boot.bookstore.model.ShoppingCart; @Repository public interface ShoppingCartRepository extends JpaRepository { + Optional findUserById(Long id); } diff --git a/src/main/java/spring/boot/bookstore/service/cartitem/CartItemService.java b/src/main/java/spring/boot/bookstore/service/cartitem/CartItemService.java index fa0faee..f8af63a 100644 --- a/src/main/java/spring/boot/bookstore/service/cartitem/CartItemService.java +++ b/src/main/java/spring/boot/bookstore/service/cartitem/CartItemService.java @@ -5,6 +5,7 @@ import spring.boot.bookstore.dto.cartitem.CartItemResponseDto; import spring.boot.bookstore.dto.cartitem.UpdateQuantityDto; import spring.boot.bookstore.model.CartItem; +import spring.boot.bookstore.model.User; public interface CartItemService { @@ -14,7 +15,7 @@ public interface CartItemService { CartItemResponseDto update(UpdateQuantityDto updateQuantityDto, Long id); - void setShoppingCartAndCartItems(Long id, CartItem cartItem); + void setShoppingCartAndCartItems(User user, CartItem cartItem); - void delete(Long id); + void delete(Long cartItemId); } diff --git a/src/main/java/spring/boot/bookstore/service/cartitem/impl/CartItemServiceImpl.java b/src/main/java/spring/boot/bookstore/service/cartitem/impl/CartItemServiceImpl.java index ce187f7..5c3e3f4 100644 --- a/src/main/java/spring/boot/bookstore/service/cartitem/impl/CartItemServiceImpl.java +++ b/src/main/java/spring/boot/bookstore/service/cartitem/impl/CartItemServiceImpl.java @@ -14,6 +14,7 @@ import spring.boot.bookstore.mapper.CartItemMapper; import spring.boot.bookstore.model.CartItem; import spring.boot.bookstore.model.ShoppingCart; +import spring.boot.bookstore.model.User; import spring.boot.bookstore.repository.BookRepository; import spring.boot.bookstore.repository.cartitem.CartItemRepository; import spring.boot.bookstore.repository.shoppingcart.ShoppingCartRepository; @@ -36,8 +37,8 @@ public CartItemResponseDto save(CartItemRequestDto cartItemRequestDto) { CartItem cartItem = new CartItem(); cartItem.setBook(bookRepository.getById(cartItemRequestDto.getBookId())); cartItem.setQuantity(cartItemRequestDto.getQuantity()); - Long id = userService.getAuthenticated().getId(); - setShoppingCartAndCartItems(id, cartItem); + User user = userService.getAuthenticated(); + setShoppingCartAndCartItems(user, cartItem); return mapper.toDto(cartItemRepository.save(cartItem)); } @@ -64,11 +65,8 @@ public void delete(Long id) { } @Override - public void setShoppingCartAndCartItems(Long id, CartItem cartItem) { - ShoppingCart shoppingCart = shoppingCartRepository.findById(id) - .orElseThrow(() -> - new EntityNotFoundException("Can't find shopping cart" - + " by ID : " + id)); + public void setShoppingCartAndCartItems(User user, CartItem cartItem) { + ShoppingCart shoppingCart = shoppingCartRepository.findUserById(user.getId()).orElseGet(()-> registerNewCart(user)); cartItem.setShoppingCart(shoppingCart); List cartItems = new ArrayList<>(); cartItems.add(cartItem); @@ -78,4 +76,11 @@ public void setShoppingCartAndCartItems(Long id, CartItem cartItem) { shoppingCart.getCartItems().add(cartItem); } } + + private ShoppingCart registerNewCart(User user) { // was created this method + ShoppingCart shoppingCart = new ShoppingCart(); + shoppingCart.setUser(user); + shoppingCartRepository.save(shoppingCart); + return shoppingCart; + } } diff --git a/src/main/java/spring/boot/bookstore/service/shoppingcart/impl/ShoppingCartServiceImpl.java b/src/main/java/spring/boot/bookstore/service/shoppingcart/impl/ShoppingCartServiceImpl.java index 088f82f..f0b010a 100644 --- a/src/main/java/spring/boot/bookstore/service/shoppingcart/impl/ShoppingCartServiceImpl.java +++ b/src/main/java/spring/boot/bookstore/service/shoppingcart/impl/ShoppingCartServiceImpl.java @@ -28,14 +28,12 @@ public CartItemResponseDto save(CartItemRequestDto requestDto) { } @Override - @Transactional(readOnly = true) + @Transactional public ShoppingCartResponseDto getShoppingCart() { User authenticatedUser = userService.getAuthenticated(); ShoppingCart shoppingCart = shoppingCartRepository - .findById(authenticatedUser.getId()) - .orElseThrow(() -> - new EntityNotFoundException("Can't find a shopping cart with ID : " - + authenticatedUser.getId())); + .findUserById(authenticatedUser.getId()) // change to findUserById + .orElseGet(() -> registerNewCart(authenticatedUser)); Long id = shoppingCart.getId(); ShoppingCartResponseDto shoppingCartDto = new ShoppingCartResponseDto(); shoppingCartDto.setId(id); @@ -43,4 +41,11 @@ public ShoppingCartResponseDto getShoppingCart() { shoppingCartDto.setCartItems(cartItemService.findByShoppingCartId(id)); return shoppingCartDto; } + + private ShoppingCart registerNewCart(User user) { // was created this method + ShoppingCart shoppingCart = new ShoppingCart(); + shoppingCart.setUser(user); + shoppingCartRepository.save(shoppingCart); + return shoppingCart; + } }