Skip to content
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

feat: updated cart module #37

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.github.nhatoriginal.spring.model.MenuItemOption;
import com.github.nhatoriginal.spring.model.User;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
public class CartIdClassConfig implements Serializable {
private User user;
private MenuItemOption menuItemOption;
public class CartIdClassConfig implements Serializable {
private User user;
private MenuItemOption menuItemOption;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public static final class User {

public static final class Cart {
public static final String BASE = "/cart";
public static final String GET_ALL = "/user/{id}";
public static final String GET_ALL = "/user/{userId}";
public static final String CREATE = "";
public static final String DELETE = "/user/{id}";
public static final String DELETE = "/{userId}/{menuItemOptionId}";
public static final String UPDATE_QUANTITY = "/{userId}/{menuItemOptionId}";
}

public static final class Review {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public ResponseEntity<String> save(@Validated @RequestBody SaveAddressDto saveAd
}

@GetMapping(Endpoint.Address.GET_ALL)
public List<AddressDto> findByUserId(@PathVariable("userId") UUID userId) {
public List<AddressDto> findByUserId(@PathVariable UUID userId) {
return addressService.findByUserId(userId);
}

@DeleteMapping(Endpoint.Address.DELETE)
public ResponseEntity<String> delete(@PathVariable("id") UUID id) {
public ResponseEntity<String> delete(@PathVariable UUID id) {
addressService.delete(id);
return ResponseEntity.ok("Deleted address successfully");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.nhatoriginal.spring.constant.Endpoint;
import com.github.nhatoriginal.spring.dto.cartList.CartItemDTO;
import com.github.nhatoriginal.spring.dto.cartList.SaveCartItemDto;
import com.github.nhatoriginal.spring.dto.cartList.UpdateCartItemQuantityDto;
import com.github.nhatoriginal.spring.model.Cart;
import com.github.nhatoriginal.spring.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -20,8 +21,8 @@ public class CartController {
public CartService cartService;

@GetMapping(Endpoint.Cart.GET_ALL)
public List<CartItemDTO> getCartItemList(@PathVariable UUID id) {
return cartService.getCartItemList(id);
public List<CartItemDTO> getCartItemList(@PathVariable UUID userId) {
return cartService.getCartItemList(userId);
}

@PostMapping(Endpoint.Cart.CREATE)
Expand All @@ -35,8 +36,17 @@ public ResponseEntity<String> save(@Validated @RequestBody SaveCartItemDto saveC
return new ResponseEntity<>("Saved cart item successfully", HttpStatus.CREATED);
}

// @DeleteMapping(Endpoint.Cart.DELETE)
// public void deleteItemCart(@PathVariable UUID){
//
// }
@DeleteMapping(Endpoint.Cart.DELETE)
public ResponseEntity<String> delete(@PathVariable("userId") UUID userId,
@PathVariable("menuItemOptionId") UUID menuItemOptionId) {
cartService.delete(userId, menuItemOptionId);
return new ResponseEntity<>("Deleted cart item successfully", HttpStatus.OK);
}

@PatchMapping(Endpoint.Cart.UPDATE_QUANTITY)
public ResponseEntity<String> updateQuantity(@PathVariable("userId") UUID userId,
@PathVariable("menuItemOptionId") UUID menuItemOptionId, @RequestBody UpdateCartItemQuantityDto body) {
cartService.updateQuantity(userId, menuItemOptionId, body);
return new ResponseEntity<>("Updated cart item quantity successfully", HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

@Component
public class CartDTOConverter {


public CartItemDTO toCartItemDTO(Cart cart){
CartItemDTO cartItemDTO = new CartItemDTO();
cartItemDTO.setImageUrl(cart.getMenuItemOption().getMenuItem().getImageUrl());
cartItemDTO.setName(cart.getMenuItemOption().getMenuItem().getName());
cartItemDTO.setPrice(cart.getMenuItemOption().getPrice());
cartItemDTO.setQuantity(cart.getQuantity());
return cartItemDTO;
}
public CartItemDTO toCartItemDTO(Cart cart) {
CartItemDTO cartItemDTO = new CartItemDTO();
cartItemDTO.setMenuItemOptionId(cart.getMenuItemOption().getId());
cartItemDTO.setEateryId(cart.getMenuItemOption().getMenuItem().getMenu().getEatery().getId());
cartItemDTO.setImageUrl(cart.getMenuItemOption().getMenuItem().getImageUrl());
cartItemDTO.setName(cart.getMenuItemOption().getMenuItem().getName());
cartItemDTO.setSize(cart.getMenuItemOption().getSize());
cartItemDTO.setPrice(cart.getMenuItemOption().getPrice());
cartItemDTO.setQuantity(cart.getQuantity());
return cartItemDTO;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.github.nhatoriginal.spring.dto.cartList;

import java.util.UUID;

import com.github.nhatoriginal.spring.model.Size;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter

public class CartItemDTO {
private UUID menuItemOptionId;
private UUID eateryId;
private String imageUrl;
private String name;
private Size size;
private double price;
private int quantity;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.nhatoriginal.spring.dto.cartList;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.Data;

@Data
public class UpdateCartItemQuantityDto {
@Max(10)
@Min(1)
private int quantity;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.nhatoriginal.spring.service;

import com.github.nhatoriginal.spring.config.CartIdClassConfig;
import com.github.nhatoriginal.spring.dto.cartList.CartDTOConverter;
import com.github.nhatoriginal.spring.dto.cartList.CartItemDTO;
import com.github.nhatoriginal.spring.dto.cartList.SaveCartItemDto;
import com.github.nhatoriginal.spring.dto.cartList.UpdateCartItemQuantityDto;
import com.github.nhatoriginal.spring.model.Cart;
import com.github.nhatoriginal.spring.model.MenuItemOption;
import com.github.nhatoriginal.spring.model.User;
Expand Down Expand Up @@ -30,8 +32,8 @@ public class CartService {
@Autowired
private MenuItemOptionRepository menuItemOptionRepository;

public List<CartItemDTO> getCartItemList(UUID id) {
User user = userRepository.findById(id).get();
public List<CartItemDTO> getCartItemList(UUID userId) {
User user = userRepository.findById(userId).get();
List<Cart> cartList = cartRepository.findByUser(user);
List<CartItemDTO> cartItemDTOS = new ArrayList<CartItemDTO>();
for (Cart cart : cartList) {
Expand All @@ -55,4 +57,36 @@ public Cart save(SaveCartItemDto saveCartItemDto) {

return cartRepository.save(cart);
}

public void delete(UUID userId, UUID menuItemOptionId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));

MenuItemOption menuItemOption = menuItemOptionRepository.findById(menuItemOptionId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Menu item option not found"));

CartIdClassConfig id = new CartIdClassConfig();
id.setUser(user);
id.setMenuItemOption(menuItemOption);

cartRepository.deleteById(id);
}

public void updateQuantity(UUID userId, UUID menuItemOptionId, UpdateCartItemQuantityDto body) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));

MenuItemOption menuItemOption = menuItemOptionRepository.findById(menuItemOptionId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Menu item option not found"));

CartIdClassConfig id = new CartIdClassConfig();
id.setUser(user);
id.setMenuItemOption(menuItemOption);

Cart cart = cartRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Cart item not found"));

cart.setQuantity(body.getQuantity());
cartRepository.save(cart);
}
}
Loading