Skip to content

Commit add5ffd

Browse files
committed
Shopping cart response and code refactor is done successfully :)
1 parent 0006643 commit add5ffd

File tree

6 files changed

+87
-58
lines changed

6 files changed

+87
-58
lines changed
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.manir.springbootecommercerestapi.controller;
22

33
import com.manir.springbootecommercerestapi.dto.CartItemDto;
4+
import com.manir.springbootecommercerestapi.response.CartItemResponse;
45
import com.manir.springbootecommercerestapi.service.ShoppingCartService;
56
import org.springframework.http.HttpStatus;
67
import org.springframework.http.ResponseEntity;
@@ -18,29 +19,29 @@ public class ShoppingCartController {
1819

1920
//find by customer api
2021
@GetMapping("/findByCustomer/{customerId}")
21-
public List<CartItemDto> findByCustomerId(@PathVariable Long customerId){
22-
List<CartItemDto> responseCartItems = shoppingCartService.findByCustomerId(customerId);
22+
public CartItemResponse findByCustomerId(@PathVariable Long customerId){
23+
CartItemResponse responseCartItems = shoppingCartService.findByCustomerId(customerId);
2324

2425
return responseCartItems;
2526
}
2627

2728
//add item to the cart api
2829
@PostMapping("/addItem/{customerId}/{productId}/{quantity}")
29-
public ResponseEntity<CartItemDto> addCartItem(@PathVariable Long customerId,
30-
@PathVariable Long productId,
31-
@PathVariable Integer quantity){
32-
CartItemDto responseCartItem = shoppingCartService.addCartItem(customerId, productId, quantity);
30+
public ResponseEntity<CartItemResponse> addCartItem(@PathVariable Long customerId,
31+
@PathVariable Long productId,
32+
@PathVariable Integer quantity){
33+
CartItemResponse responseCartItem = shoppingCartService.addCartItem(customerId, productId, quantity);
3334

3435
return new ResponseEntity<>(responseCartItem, HttpStatus.CREATED);
3536
}
3637

3738
//update item quantity api
3839
@PutMapping("/updateItemQuantity/{customerId}/{productId}/{quantity}")
39-
public ResponseEntity<CartItemDto> updateItemQuantity(@PathVariable Long customerId,
40-
@PathVariable Long productId,
41-
@PathVariable Integer quantity){
40+
public ResponseEntity<CartItemResponse> updateItemQuantity(@PathVariable Long customerId,
41+
@PathVariable Long productId,
42+
@PathVariable Integer quantity){
4243

43-
CartItemDto responseCartItem = shoppingCartService.updateItemQuantity(customerId, productId, quantity);
44+
CartItemResponse responseCartItem = shoppingCartService.updateItemQuantity(customerId, productId, quantity);
4445

4546
return new ResponseEntity<>(responseCartItem, HttpStatus.OK);
4647
}
@@ -49,6 +50,6 @@ public ResponseEntity<CartItemDto> updateItemQuantity(@PathVariable Long custome
4950
@DeleteMapping("/deleteItemProduct/{customerId}/{productId}")
5051
public ResponseEntity<String> deleteItemProduct(@PathVariable Long customerId, @PathVariable Long productId){
5152
shoppingCartService.deleteItemProduct(customerId, productId);
52-
return ResponseEntity.ok("Product deleted successfully from cart item");
53+
return ResponseEntity.ok("Product with id = " + productId +" is deleted successfully from your shopping cart");
5354
}
5455
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.manir.springbootecommercerestapi.dto;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import com.manir.springbootecommercerestapi.model.Product;
35
import lombok.Data;
46

57
@Data
68
public class CartItemDto {
79
private Long id;
810
private Integer quantity;
911
private String status;
12+
@JsonIgnore
13+
private Product product;
1014
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.manir.springbootecommercerestapi.service;
22

3+
import com.manir.springbootecommercerestapi.dto.CartItemDto;
4+
import com.manir.springbootecommercerestapi.response.CartItemResponse;
35
import com.manir.springbootecommercerestapi.response.CommonResponse;
46
import org.springframework.data.domain.Page;
57

@@ -9,4 +11,7 @@ public interface CommonService<T> {
911

1012
//CommonResponse getAllCategoryOrProduct(int pageNo, int pageSize, String sortBy, String sortDir);
1113
CommonResponse getResponseContent(Page<T> page, List<T> dtoList);
14+
15+
//cart iem response handler
16+
CartItemResponse getResponse(CartItemDto cartItemDto);
1217
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.manir.springbootecommercerestapi.service.Impl;
22

3+
import com.manir.springbootecommercerestapi.dto.CartItemDto;
4+
import com.manir.springbootecommercerestapi.response.CartItemResponse;
35
import com.manir.springbootecommercerestapi.response.CommonResponse;
46
import com.manir.springbootecommercerestapi.service.CommonService;
57
import lombok.extern.slf4j.Slf4j;
@@ -8,6 +10,7 @@
810
import org.springframework.data.domain.Page;
911
import org.springframework.stereotype.Service;
1012

13+
import java.util.ArrayList;
1114
import java.util.List;
1215

1316
@Service
@@ -29,4 +32,17 @@ public CommonResponse getResponseContent(Page page, List dtoList) {
2932

3033
return commonResponse;
3134
}
35+
36+
@Override
37+
public CartItemResponse getResponse(CartItemDto cartItemDto) {
38+
39+
CartItemResponse cartItemResponse = new CartItemResponse();
40+
41+
double totalPrice = cartItemDto.getProduct().getPrice() * cartItemDto.getQuantity();
42+
List<CartItemDto> cartItemDtoList = new ArrayList<>();
43+
cartItemDtoList.add(cartItemDto);
44+
cartItemResponse.setContent(cartItemDtoList);
45+
cartItemResponse.setTotalCost(totalPrice);
46+
return cartItemResponse;
47+
}
3248
}

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

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.manir.springbootecommercerestapi.repository.CartItemRepository;
1010
import com.manir.springbootecommercerestapi.repository.CustomerRepository;
1111
import com.manir.springbootecommercerestapi.repository.ProductRepository;
12+
import com.manir.springbootecommercerestapi.response.CartItemResponse;
13+
import com.manir.springbootecommercerestapi.service.CommonService;
1214
import com.manir.springbootecommercerestapi.service.ShoppingCartService;
1315
import lombok.extern.slf4j.Slf4j;
1416
import org.modelmapper.ModelMapper;
@@ -17,12 +19,13 @@
1719

1820
import javax.annotation.Resource;
1921
import javax.transaction.Transactional;
22+
import java.util.ArrayList;
2023
import java.util.List;
2124
import java.util.stream.Collectors;
25+
import java.util.stream.DoubleStream;
2226

2327
@Service
2428
@Slf4j
25-
2629
public class ShoppingCartServiceImpl implements ShoppingCartService {
2730

2831
@Resource
@@ -33,9 +36,10 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
3336
private ProductRepository productRepository;
3437
@Resource
3538
private CustomerRepository customerRepository;
36-
39+
@Resource
40+
private CommonService commonService;
3741
@Override
38-
public List<CartItemDto> findByCustomerId(Long customerId) {
42+
public CartItemResponse findByCustomerId(Long customerId) {
3943

4044
List<CartItem> cartItems = cartItemRepository.findByCustomerId(customerId);
4145

@@ -46,21 +50,19 @@ public List<CartItemDto> findByCustomerId(Long customerId) {
4650
List<CartItemDto> cartItemDtoList = cartItems.stream()
4751
.map(cartItem -> mapToDto(cartItem))
4852
.collect(Collectors.toList());
49-
50-
return cartItemDtoList;
53+
DoubleStream totalPrice = cartItemDtoList.stream()
54+
.mapToDouble(cartItemDto -> cartItemDto.getProduct().getPrice() * cartItemDto.getQuantity());
55+
CartItemResponse cartItemResponse = new CartItemResponse();
56+
cartItemResponse.setContent(cartItemDtoList);
57+
cartItemResponse.setTotalCost(totalPrice.sum());
58+
return cartItemResponse;
5159
}
5260

5361
@Override
54-
public CartItemDto addCartItem(Long customerId, Long productId, Integer quantity) {
62+
public CartItemResponse addCartItem(Long customerId, Long productId, Integer quantity) {
5563
Integer addedQuantity = quantity;
56-
Customer customer = customerRepository.findById(customerId)
57-
.orElseThrow(
58-
() -> new ResourceNotFoundException("Customer", customerId)
59-
);
60-
Product product = productRepository.findById(productId)
61-
.orElseThrow(
62-
() -> new ResourceNotFoundException("Product", productId)
63-
);
64+
Customer customer = findCustomerById(customerId);
65+
Product product = findProductById(productId);
6466

6567
CartItem cartItem = cartItemRepository.findByCustomerIdAndProductId(customerId, productId);
6668
if(cartItem != null){
@@ -74,49 +76,36 @@ public CartItemDto addCartItem(Long customerId, Long productId, Integer quantity
7476
}
7577

7678
CartItem addedCartItem = cartItemRepository.save(cartItem);
79+
CartItemDto cartItemDto = mapToDto(addedCartItem);
7780

78-
return mapToDto(addedCartItem);
81+
CartItemResponse cartItemResponse = commonService.getResponse(cartItemDto);
82+
return cartItemResponse;
7983
}
8084

8185
@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-
);
86+
public CartItemResponse updateItemQuantity(Long customerId, Long productId, Integer quantity) {
9287

9388
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);
89+
if (cartItem == null){
90+
throw new EcommerceApiException("Product is not in the cart item", HttpStatus.BAD_REQUEST);
9691
}
9792
cartItem.setQuantity(quantity);
9893
CartItem updatedItemQuantity = cartItemRepository.save(cartItem);
99-
return mapToDto(updatedItemQuantity);
94+
CartItemDto cartItemDto = mapToDto(updatedItemQuantity);
95+
96+
CartItemResponse cartItemResponse = commonService.getResponse(cartItemDto);
97+
return cartItemResponse;
10098
}
10199

102100
@Override
103101
@Transactional
104102
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-
);
103+
113104
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);
105+
if (cartItem == null){
106+
throw new EcommerceApiException("Product is not in the cart item", HttpStatus.BAD_REQUEST);
119107
}
108+
cartItemRepository.deleteByCustomerIdAndProductId(customerId, productId);
120109
}
121110

122111
//map to dto
@@ -130,4 +119,20 @@ private CartItem mapToEntity(CartItemDto cartItemDto){
130119
CartItem cartItem = modelMapper.map(cartItemDto, CartItem.class);
131120
return cartItem;
132121
}
122+
123+
private Customer findCustomerById(Long customerId){
124+
Customer customer = customerRepository.findById(customerId)
125+
.orElseThrow(
126+
() -> new ResourceNotFoundException("Customer", customerId)
127+
);
128+
return customer;
129+
}
130+
131+
private Product findProductById(Long productId){
132+
Product product = productRepository.findById(productId)
133+
.orElseThrow(
134+
() -> new ResourceNotFoundException("Product", productId)
135+
);
136+
return product;
137+
}
133138
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.manir.springbootecommercerestapi.service;
22

3-
import com.manir.springbootecommercerestapi.dto.CartItemDto;
4-
5-
import java.util.List;
3+
import com.manir.springbootecommercerestapi.response.CartItemResponse;
64

75
public interface ShoppingCartService {
86

9-
List<CartItemDto> findByCustomerId(Long customerId);
7+
CartItemResponse findByCustomerId(Long customerId);
108

11-
CartItemDto addCartItem(Long customerId, Long productId, Integer quantity);
9+
CartItemResponse addCartItem(Long customerId, Long productId, Integer quantity);
1210

13-
CartItemDto updateItemQuantity(Long customerId, Long productId, Integer quantity);
11+
CartItemResponse updateItemQuantity(Long customerId, Long productId, Integer quantity);
1412

1513
void deleteItemProduct(Long customerId, Long productId);
1614
}

0 commit comments

Comments
 (0)