Skip to content

Commit e1a718c

Browse files
committed
CartItem implementation started.
1 parent 76735f3 commit e1a718c

File tree

10 files changed

+249
-0
lines changed

10 files changed

+249
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.manir.springbootecommercerestapi.controller;
2+
3+
import com.manir.springbootecommercerestapi.dto.CartItemDto;
4+
import com.manir.springbootecommercerestapi.service.ShoppingCartService;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import javax.annotation.Resource;
10+
import java.util.List;
11+
12+
@RestController
13+
@RequestMapping("api/v1/cart")
14+
public class ShoppingCartController {
15+
16+
@Resource
17+
private ShoppingCartService shoppingCartService;
18+
19+
//find by customer api
20+
@GetMapping("/findByCustomer/{customerId}")
21+
public List<CartItemDto> findByCustomerId(@PathVariable Long customerId){
22+
List<CartItemDto> responseCartItems = shoppingCartService.findByCustomerId(customerId);
23+
24+
return responseCartItems;
25+
}
26+
27+
//add item to the cart api
28+
@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);
33+
34+
return new ResponseEntity<>(responseCartItem, HttpStatus.CREATED);
35+
}
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.manir.springbootecommercerestapi.dto;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class CartItemDto {
7+
private Long id;
8+
private Integer quantity;
9+
private String status;
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.manir.springbootecommercerestapi.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import javax.persistence.*;
9+
import java.util.Set;
10+
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
@Data
14+
@Entity
15+
@Table(name = "cart_item")
16+
public class CartItem {
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.IDENTITY)
19+
private Long id;
20+
@Column(name = "quantity")
21+
private Integer quantity;
22+
@Column(name = "status")
23+
private String status; //pending, ordered
24+
25+
//relation with product
26+
@ManyToOne()
27+
@JoinColumn(name = "product_id")
28+
private Product product;
29+
30+
//relation with user
31+
@ManyToOne()
32+
@JoinColumn(name = "customer_id")
33+
private Customer customer;
34+
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.manir.springbootecommercerestapi.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import lombok.Data;
5+
6+
import javax.persistence.*;
7+
import java.util.Set;
8+
9+
@Data
10+
@Entity
11+
@Table(name = "customers", uniqueConstraints = {@UniqueConstraint(columnNames = {"userName"}),
12+
@UniqueConstraint(columnNames = {"email"})
13+
})
14+
public class Customer {
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.IDENTITY)
17+
private Long id;
18+
private String name;
19+
private String userName;
20+
private String email;
21+
private String password;
22+
23+
//relation with cart item
24+
@OneToMany(cascade = CascadeType.ALL,
25+
fetch = FetchType.LAZY, orphanRemoval = true,
26+
mappedBy = "customer")
27+
private Set<CartItem> cartItems;
28+
}

src/main/java/com/manir/springbootecommercerestapi/model/Product.java

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

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import lombok.*;
45

56
import javax.persistence.*;
@@ -48,4 +49,9 @@ public class Product {
4849
//relation to product comment
4950
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true)
5051
private Set<Comment> comments;
52+
53+
54+
//relation to cart item
55+
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true)
56+
private Set<CartItem> cartItems;
5157
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.manir.springbootecommercerestapi.repository;
2+
3+
import com.manir.springbootecommercerestapi.model.CartItem;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
import java.util.List;
7+
8+
public interface CartItemRepository extends JpaRepository<CartItem, Long> {
9+
10+
List<CartItem> findByCustomerId(Long customerId);
11+
12+
//CartItem findByCustomerAndProduct(Customer customer, Product product);
13+
CartItem findByCustomerIdAndProductId(Long customerId, Long productId);
14+
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.manir.springbootecommercerestapi.repository;
2+
3+
import com.manir.springbootecommercerestapi.model.Customer;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface CustomerRepository extends JpaRepository<Customer, Long>{
7+
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.manir.springbootecommercerestapi.response;
2+
3+
4+
import com.manir.springbootecommercerestapi.dto.CartItemDto;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.util.List;
10+
11+
@Data
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
public class CartItemResponse {
15+
private List<CartItemDto> content;
16+
private double totalCost;
17+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.manir.springbootecommercerestapi.service.Impl;
2+
3+
import com.manir.springbootecommercerestapi.dto.CartItemDto;
4+
import com.manir.springbootecommercerestapi.exception.ResourceNotFoundException;
5+
import com.manir.springbootecommercerestapi.model.CartItem;
6+
import com.manir.springbootecommercerestapi.model.Customer;
7+
import com.manir.springbootecommercerestapi.model.Product;
8+
import com.manir.springbootecommercerestapi.repository.CartItemRepository;
9+
import com.manir.springbootecommercerestapi.repository.CustomerRepository;
10+
import com.manir.springbootecommercerestapi.repository.ProductRepository;
11+
import com.manir.springbootecommercerestapi.service.ShoppingCartService;
12+
import lombok.extern.slf4j.Slf4j;
13+
import org.modelmapper.ModelMapper;
14+
import org.springframework.stereotype.Service;
15+
16+
import javax.annotation.Resource;
17+
import java.util.List;
18+
import java.util.stream.Collectors;
19+
20+
@Service
21+
@Slf4j
22+
public class ShoppingCartServiceImpl implements ShoppingCartService {
23+
24+
@Resource
25+
private CartItemRepository cartItemRepository;
26+
@Resource
27+
private ModelMapper modelMapper;
28+
@Resource
29+
private ProductRepository productRepository;
30+
@Resource
31+
private CustomerRepository customerRepository;
32+
33+
@Override
34+
public List<CartItemDto> findByCustomerId(Long customerId) {
35+
List<CartItem> cartItems = cartItemRepository.findByCustomerId(customerId);
36+
List<CartItemDto> cartItemDtoList = cartItems.stream()
37+
.map(cartItem -> mapToDto(cartItem))
38+
.collect(Collectors.toList());
39+
40+
return cartItemDtoList;
41+
}
42+
43+
@Override
44+
public CartItemDto addCartItem(Long customerId, Long productId, Integer quantity) {
45+
Integer addedQuantity = quantity;
46+
Customer customer = customerRepository.findById(customerId)
47+
.orElseThrow(
48+
() -> new ResourceNotFoundException("Customer", customerId)
49+
);
50+
Product product = productRepository.findById(productId)
51+
.orElseThrow(
52+
() -> new ResourceNotFoundException("Customer", customerId)
53+
);
54+
55+
CartItem cartItem = cartItemRepository.findByCustomerIdAndProductId(customerId, productId);
56+
if(cartItem != null){
57+
addedQuantity = cartItem.getQuantity() + quantity;
58+
cartItem.setQuantity(addedQuantity);
59+
}else {
60+
cartItem = new CartItem();
61+
cartItem.setCustomer(customer);
62+
cartItem.setProduct(product);
63+
cartItem.setQuantity(quantity);
64+
}
65+
66+
CartItem addedCartItem = cartItemRepository.save(cartItem);
67+
68+
return mapToDto(addedCartItem);
69+
}
70+
71+
//map to dto
72+
private CartItemDto mapToDto(CartItem cartItem){
73+
CartItemDto cartItemDto = modelMapper.map(cartItem, CartItemDto.class);
74+
return cartItemDto;
75+
}
76+
77+
//map to entity
78+
private CartItem mapToEntity(CartItemDto cartItemDto){
79+
CartItem cartItem = modelMapper.map(cartItemDto, CartItem.class);
80+
return cartItem;
81+
}
82+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.manir.springbootecommercerestapi.service;
2+
3+
import com.manir.springbootecommercerestapi.dto.CartItemDto;
4+
5+
import java.util.List;
6+
7+
public interface ShoppingCartService {
8+
9+
List<CartItemDto> findByCustomerId(Long customerId);
10+
11+
CartItemDto addCartItem(Long customerId, Long productId, Integer quantity);
12+
}

0 commit comments

Comments
 (0)