-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
09-implemented-shopping-cart-Cart-item #9
Conversation
…ce interface and implementation of this service and DTO classes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like there is a lot of work that needs to be done in the services. If your application is not functioning properly, it is recommended to post a thread in the chat section discussing the issue and then provide a working solution for review. Service methods should not be overly complex. Try delegating some of the work to mappers and utility methods in entities that are responsible for updating relationships.
|
||
@PutMapping("/cart-items/{id}") | ||
@PreAuthorize("hasRole('USER')") | ||
@Operation(summary = "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add description
|
||
@DeleteMapping("/{cartItemId}") | ||
@PreAuthorize("hasRole('USER')") | ||
@Operation(summary = "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add description
@RestController | ||
@RequestMapping("/cart") | ||
public class ShoppingCartController { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private final CartItemService cartItemService; | ||
|
||
@PostMapping | ||
@Operation(summary = "add new item") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Operation(summary = "add new item") | |
@Operation(summary = "Add new item to a shopping cart") |
cartItemRepository.deleteById(id); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove line
CartItem cartItem = new CartItem(); | ||
cartItem.setBook(bookRepository.getById(cartItemRequestDto.getBookId())); | ||
cartItem.setQuantity(cartItemRequestDto.getQuantity()); | ||
Long id = userService.getAuthenticated().getId(); | ||
cartItem.setShoppingCart(shoppingCartRepository.getById(id)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use mapper to transform request dto to CartItem model
import spring.boot.bookstore.model.ShoppingCart; | ||
|
||
public interface ShoppingCartService { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else { | ||
return null; | ||
} | ||
} else { | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bad practice
|
||
@Repository | ||
public interface ShoppingCartRepository extends JpaRepository<ShoppingCart, Long> { | ||
Optional<ShoppingCart> getUserById(Long id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional<ShoppingCart> getUserById(Long id); | |
Optional<ShoppingCart> getById(Long id); |
cartItem.setBook(bookRepository.getById(cartItemRequestDto.getBookId())); | ||
cartItem.setQuantity(cartItemRequestDto.getQuantity()); | ||
Long id = userService.getAuthenticated().getId(); | ||
cartItem.setShoppingCart(shoppingCartRepository.getById(id)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
foreign key constraint error may be occuring here - what if getById returns null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, fix the previous comments
@Repository | ||
public interface CartItemRepository extends JpaRepository<CartItem, Long> { | ||
@Query("SELECT c FROM CartItem c WHERE c.shoppingCart.id =:shoppingCartId") | ||
Set<CartItem> findCartItemByShoppingCartId(@Param("shoppingCartId") Long shoppingCart); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
such simple queries should be handled by proper method naming, please recheck work without them and fix just naming method if required
@Service | ||
@RequiredArgsConstructor | ||
public class ShoppingCartServiceImpl implements ShoppingCartService { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not fixed
@Service | ||
@RequiredArgsConstructor | ||
public class CartItemServiceImpl implements CartItemService { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not fixed
@@ -13,9 +14,10 @@ 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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
such simple queries should be handled by proper method naming, please recheck work without them and fix just the naming method if required
1. Deleted Query 2. deleted empty lines 3. deleted comment
import spring.boot.bookstore.model.User; | ||
|
||
public interface CartItemService { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public User getAuthenticated() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
return userRepository.findByEmail(authentication.getName()).orElseThrow( | ||
() -> new RuntimeException("Can`t find user with according email" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't use generic runtime exception
return shoppingCartDto; | ||
} | ||
|
||
private ShoppingCart registerNewCart(User user) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicated method in two services
cartItemRepository.findById(id) | ||
.orElseThrow(() -> new EntityNotFoundException("Can`t find cart by ID : " + id)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cartItemRepository.findById(id) | |
.orElseThrow(() -> new EntityNotFoundException("Can`t find cart by ID : " + id)); |
} | ||
|
||
@Override | ||
public CartItemResponseDto update(UpdateQuantityDto updateQuantityDto, Long id) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check if quantity is positive
…hod in service classes
…oid the repetitive method
No description provided.