Skip to content

Commit

Permalink
Was modified all necessary classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Optic-Okulist committed Sep 27, 2023
1 parent bd0e7b9 commit 63d490a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 24 deletions.
14 changes: 8 additions & 6 deletions src/main/java/spring/boot/bookstore/model/ShoppingCart.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -39,7 +41,7 @@ public class ShoppingCart {
inverseJoinColumns = @JoinColumn(name = "cart_items_id"))
@ToString.Exclude
@EqualsAndHashCode.Exclude
private List<CartItem> cartItems = new ArrayList<>();
private List<CartItem> cartItems = new ArrayList<>();

@Column(name = "is_deleted", nullable = false)
private boolean isDeleted = false;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/spring/boot/bookstore/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Role> roles;
private Set<Role> roles = new HashSet<>(); // added HashSet
@Column(name = "is_deleted", nullable = false)
private boolean isDeleted = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,9 +16,9 @@ public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificat
List<Book> findAllByTitleContainsIgnoreCase(String title);

@Query("FROM Book b JOIN b.categories c WHERE c.id =:categoryId")
List<Book> findByCategoryId(Long categoryId, Pageable pageable);
List<Book> findByCategoryId(@Param("categoryId") Long categoryId, Pageable pageable); // was added @Param("categoryId") and Long categoryId

@Query("FROM Book b INNER JOIN FETCH b.categories")
List<Book> findAllWithCategories(Pageable peageable);
List<Book> findAllWithCategories(Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CartItem, Long> {
@Query("SELECT c FROM CartItem c WHERE c.shoppingCart.id =:shoppingCartId")
Set<CartItem> findCartItemByShoppingCartId(Long shoppingCart);
Set<CartItem> findCartItemByShoppingCartId(@Param("shoppingCartId") Long shoppingCart);
}
Original file line number Diff line number Diff line change
@@ -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<ShoppingCart, Long> {
Optional<ShoppingCart> findUserById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}

Expand All @@ -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<CartItem> cartItems = new ArrayList<>();
cartItems.add(cartItem);
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ 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);
shoppingCartDto.setUserId(authenticatedUser.getId());
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;
}
}

0 comments on commit 63d490a

Please sign in to comment.