Skip to content

Commit c6456ef

Browse files
committed
CartItem implementation finished.
1 parent e1a718c commit c6456ef

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

src/main/java/com/manir/springbootecommercerestapi/controller/ShoppingCartController.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,22 @@ public ResponseEntity<CartItemDto> addCartItem(@PathVariable Long customerId,
3333

3434
return new ResponseEntity<>(responseCartItem, HttpStatus.CREATED);
3535
}
36+
37+
//update item quantity api
38+
@PutMapping("/updateItemQuantity/{customerId}/{productId}/{quantity}")
39+
public ResponseEntity<CartItemDto> updateItemQuantity(@PathVariable Long customerId,
40+
@PathVariable Long productId,
41+
@PathVariable Integer quantity){
42+
43+
CartItemDto responseCartItem = shoppingCartService.updateItemQuantity(customerId, productId, quantity);
44+
45+
return new ResponseEntity<>(responseCartItem, HttpStatus.OK);
46+
}
47+
48+
//delete item product api
49+
@DeleteMapping("/deleteItemProduct/{customerId}/{productId}")
50+
public ResponseEntity<String> deleteItemProduct(@PathVariable Long customerId, @PathVariable Long productId){
51+
shoppingCartService.deleteItemProduct(customerId, productId);
52+
return ResponseEntity.ok("Product deleted successfully from cart item");
53+
}
3654
}

src/main/java/com/manir/springbootecommercerestapi/repository/CartItemRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.manir.springbootecommercerestapi.model.CartItem;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Modifying;
6+
import org.springframework.data.jpa.repository.Query;
57

68
import java.util.List;
79

@@ -12,4 +14,11 @@ public interface CartItemRepository extends JpaRepository<CartItem, Long> {
1214
//CartItem findByCustomerAndProduct(Customer customer, Product product);
1315
CartItem findByCustomerIdAndProductId(Long customerId, Long productId);
1416

17+
@Query("UPDATE CartItem c SET c.quantity = ?3 WHERE c.product.id = ?2 AND c.customer.id = ?1")
18+
void updateItemQuantity(Long customerId, Long productId, Integer quantity);
19+
20+
@Query("DELETE FROM CartItem c WHERE c.customer.id = ?1 AND c.product.id = ?2")
21+
@Modifying
22+
void deleteByCustomerIdAndProductId(Long customerId, Long productId);
23+
1524
}

src/main/java/com/manir/springbootecommercerestapi/service/Impl/ShoppingCartServiceImpl.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.manir.springbootecommercerestapi.service.Impl;
22

33
import com.manir.springbootecommercerestapi.dto.CartItemDto;
4+
import com.manir.springbootecommercerestapi.exception.EcommerceApiException;
45
import com.manir.springbootecommercerestapi.exception.ResourceNotFoundException;
56
import com.manir.springbootecommercerestapi.model.CartItem;
67
import com.manir.springbootecommercerestapi.model.Customer;
@@ -11,14 +12,17 @@
1112
import com.manir.springbootecommercerestapi.service.ShoppingCartService;
1213
import lombok.extern.slf4j.Slf4j;
1314
import org.modelmapper.ModelMapper;
15+
import org.springframework.http.HttpStatus;
1416
import org.springframework.stereotype.Service;
1517

1618
import javax.annotation.Resource;
19+
import javax.transaction.Transactional;
1720
import java.util.List;
1821
import java.util.stream.Collectors;
1922

2023
@Service
2124
@Slf4j
25+
2226
public class ShoppingCartServiceImpl implements ShoppingCartService {
2327

2428
@Resource
@@ -32,7 +36,13 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
3236

3337
@Override
3438
public List<CartItemDto> findByCustomerId(Long customerId) {
39+
3540
List<CartItem> cartItems = cartItemRepository.findByCustomerId(customerId);
41+
42+
if (cartItems.size() == 0){
43+
throw new EcommerceApiException("Customer has no product in cart item", HttpStatus.BAD_REQUEST);
44+
}
45+
3646
List<CartItemDto> cartItemDtoList = cartItems.stream()
3747
.map(cartItem -> mapToDto(cartItem))
3848
.collect(Collectors.toList());
@@ -49,7 +59,7 @@ public CartItemDto addCartItem(Long customerId, Long productId, Integer quantity
4959
);
5060
Product product = productRepository.findById(productId)
5161
.orElseThrow(
52-
() -> new ResourceNotFoundException("Customer", customerId)
62+
() -> new ResourceNotFoundException("Product", productId)
5363
);
5464

5565
CartItem cartItem = cartItemRepository.findByCustomerIdAndProductId(customerId, productId);
@@ -68,6 +78,47 @@ public CartItemDto addCartItem(Long customerId, Long productId, Integer quantity
6878
return mapToDto(addedCartItem);
6979
}
7080

81+
@Override
82+
public CartItemDto updateItemQuantity(Long customerId, Long productId, Integer quantity) {
83+
84+
Customer customer = customerRepository.findById(customerId)
85+
.orElseThrow(
86+
() -> new ResourceNotFoundException("Customer", customerId)
87+
);
88+
Product product = productRepository.findById(productId)
89+
.orElseThrow(
90+
() -> new ResourceNotFoundException("Product", productId)
91+
);
92+
93+
CartItem cartItem = cartItemRepository.findByCustomerIdAndProductId(customerId, productId);
94+
if (!cartItem.getCustomer().getId().equals(customer.getId()) || !cartItem.getProduct().getId().equals(product.getId())){
95+
throw new EcommerceApiException(" this cart item does not belong to Customer or Product ", HttpStatus.BAD_REQUEST);
96+
}
97+
cartItem.setQuantity(quantity);
98+
CartItem updatedItemQuantity = cartItemRepository.save(cartItem);
99+
return mapToDto(updatedItemQuantity);
100+
}
101+
102+
@Override
103+
@Transactional
104+
public void deleteItemProduct(Long customerId, Long productId) {
105+
Customer customer = customerRepository.findById(customerId)
106+
.orElseThrow(
107+
() -> new ResourceNotFoundException("Customer", customerId)
108+
);
109+
Product product = productRepository.findById(productId)
110+
.orElseThrow(
111+
() -> new ResourceNotFoundException("Product", productId)
112+
);
113+
CartItem cartItem = cartItemRepository.findByCustomerIdAndProductId(customerId, productId);
114+
if (cartItem != null){
115+
cartItemRepository.deleteByCustomerIdAndProductId(customerId, productId);
116+
}
117+
if (!cartItem.getCustomer().getId().equals(customer.getId()) || !cartItem.getProduct().getId().equals(product.getId())){
118+
throw new EcommerceApiException(" this cart item does not belong to Customer or Product ", HttpStatus.BAD_REQUEST);
119+
}
120+
}
121+
71122
//map to dto
72123
private CartItemDto mapToDto(CartItem cartItem){
73124
CartItemDto cartItemDto = modelMapper.map(cartItem, CartItemDto.class);

src/main/java/com/manir/springbootecommercerestapi/service/ShoppingCartService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ public interface ShoppingCartService {
99
List<CartItemDto> findByCustomerId(Long customerId);
1010

1111
CartItemDto addCartItem(Long customerId, Long productId, Integer quantity);
12+
13+
CartItemDto updateItemQuantity(Long customerId, Long productId, Integer quantity);
14+
15+
void deleteItemProduct(Long customerId, Long productId);
1216
}

0 commit comments

Comments
 (0)