Skip to content

Commit 4f28a19

Browse files
committed
order and ordered products is implemented successfully.
1 parent 67233d5 commit 4f28a19

13 files changed

+254
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.manir.springbootecommercerestapi.controller;
2+
3+
import com.manir.springbootecommercerestapi.dto.OrderDto;
4+
import com.manir.springbootecommercerestapi.exception.EcommerceApiException;
5+
import com.manir.springbootecommercerestapi.model.Order;
6+
import com.manir.springbootecommercerestapi.model.User;
7+
import com.manir.springbootecommercerestapi.repository.UserRepository;
8+
import com.manir.springbootecommercerestapi.service.OrderService;
9+
import com.manir.springbootecommercerestapi.utils.isAuthenticatedAsAdminOrUser;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.http.HttpStatus;
12+
import org.springframework.http.ResponseEntity;
13+
import org.springframework.security.authentication.AnonymousAuthenticationToken;
14+
import org.springframework.security.core.Authentication;
15+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
16+
import org.springframework.security.core.context.SecurityContextHolder;
17+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
18+
import org.springframework.web.bind.annotation.GetMapping;
19+
import org.springframework.web.bind.annotation.PostMapping;
20+
import org.springframework.web.bind.annotation.RequestMapping;
21+
import org.springframework.web.bind.annotation.RestController;
22+
23+
import java.util.List;
24+
25+
@RestController
26+
@RequestMapping(value = "api/v1/order")
27+
public class OrderController {
28+
29+
@Autowired
30+
private UserRepository userRepository;
31+
@Autowired
32+
private OrderService orderService;
33+
34+
//place order complete order api
35+
@isAuthenticatedAsAdminOrUser
36+
@PostMapping("/placeOrder")
37+
public ResponseEntity<?> placeOrder(@AuthenticationPrincipal Authentication authentication){
38+
authentication = SecurityContextHolder.getContext().getAuthentication();
39+
if (!(authentication instanceof AnonymousAuthenticationToken)){
40+
String currentUserEmail = authentication.getName();
41+
User customer = userRepository.findByEmail(currentUserEmail).orElseThrow(() -> new UsernameNotFoundException("Customer Not found"));
42+
orderService.placeOrder(customer);
43+
return new ResponseEntity<>("Order placed successfully", HttpStatus.CREATED);
44+
}else{
45+
throw new EcommerceApiException("User not authenticated", HttpStatus.BAD_REQUEST);
46+
}
47+
}
48+
49+
//find order by customer api
50+
@isAuthenticatedAsAdminOrUser
51+
@GetMapping("/findByCustomer")
52+
public List<OrderDto> listOrdersByCustomer(@AuthenticationPrincipal Authentication authentication){
53+
authentication = SecurityContextHolder.getContext().getAuthentication();
54+
if (!(authentication instanceof AnonymousAuthenticationToken)){
55+
String currentUserEmail = authentication.getName();
56+
User customer = userRepository.findByEmail(currentUserEmail).orElseThrow(() -> new UsernameNotFoundException("Customer Not found"));
57+
List<OrderDto> customerOrders = orderService.listOrdersByCustomer(customer);
58+
return customerOrders;
59+
}else{
60+
throw new EcommerceApiException("User not authenticated", HttpStatus.BAD_REQUEST);
61+
}
62+
}
63+
}

Diff for: src/main/java/com/manir/springbootecommercerestapi/controller/ShoppingCartController.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.manir.springbootecommercerestapi.model.User;
55
import com.manir.springbootecommercerestapi.repository.UserRepository;
66
import com.manir.springbootecommercerestapi.response.CartItemResponse;
7+
import com.manir.springbootecommercerestapi.service.OrderService;
78
import com.manir.springbootecommercerestapi.service.ShoppingCartService;
89
import com.manir.springbootecommercerestapi.utils.isAuthenticatedAsAdminOrUser;
910
import org.springframework.beans.factory.annotation.Autowired;
@@ -24,10 +25,10 @@ public class ShoppingCartController {
2425

2526
@Resource
2627
private ShoppingCartService shoppingCartService;
27-
2828
@Autowired
2929
private UserRepository userRepository;
30-
30+
@Autowired
31+
private OrderService orderService;
3132

3233
//find by customer api
3334
@isAuthenticatedAsAdminOrUser
@@ -96,4 +97,8 @@ public ResponseEntity<String> deleteItemProduct(@AuthenticationPrincipal Authent
9697
throw new EcommerceApiException("User not authenticated", HttpStatus.BAD_REQUEST);
9798
}
9899
}
100+
101+
102+
103+
99104
}

Diff for: src/main/java/com/manir/springbootecommercerestapi/dto/OrderDto.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.manir.springbootecommercerestapi.dto;
22

3+
import com.manir.springbootecommercerestapi.model.User;
34
import lombok.Data;
45

56
@Data
@@ -12,4 +13,6 @@ public class OrderDto {
1213
private double totalPrice;
1314
private String note;
1415
private String status;
16+
17+
private User customer;
1518
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.manir.springbootecommercerestapi.dto;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class OrderProductsDto {
7+
private Long id;
8+
private double productPrice;
9+
private Integer productQuantity;
10+
private double totalPrice;
11+
private String note;
12+
private String status;
13+
}

Diff for: src/main/java/com/manir/springbootecommercerestapi/model/Order.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.manir.springbootecommercerestapi.model;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Data;
5-
import lombok.NoArgsConstructor;
3+
import lombok.*;
64

75
import javax.persistence.*;
86
import java.util.Set;
97

108
@AllArgsConstructor
119
@NoArgsConstructor
12-
@Data
10+
@Getter
11+
@Setter
1312
@Entity
1413
@Table(name = "orders")
1514
public class Order {

Diff for: src/main/java/com/manir/springbootecommercerestapi/model/User.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.manir.springbootecommercerestapi.model;
22

3-
import lombok.Data;
3+
import lombok.*;
44

55
import javax.persistence.*;
66
import java.util.Set;
77

8-
@Data
8+
@Getter
9+
@Setter
10+
@AllArgsConstructor
11+
@NoArgsConstructor
912
@Entity
1013
@Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = {"userName"}),
1114
@UniqueConstraint(columnNames = {"email"})

Diff for: src/main/java/com/manir/springbootecommercerestapi/repository/CartItemRepository.java

+4
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ public interface CartItemRepository extends JpaRepository<CartItem, Long> {
2323
@Modifying
2424
void deleteByCustomerAndProduct(Long customerId, Long productId);
2525

26+
@Query("DELETE FROM CartItem c WHERE c.customer.id = ?1")
27+
@Modifying
28+
void deleteByCustomerId(Long customerId);
29+
2630
}
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.OrderProducts;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface OrderProductsRepository extends JpaRepository<OrderProducts, Long> {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.manir.springbootecommercerestapi.repository;
2+
3+
import com.manir.springbootecommercerestapi.model.Order;
4+
import com.manir.springbootecommercerestapi.model.User;
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
7+
import java.util.List;
8+
9+
public interface OrderRepository extends JpaRepository<Order, Long> {
10+
11+
List<Order> findByCustomer(User customer);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.manir.springbootecommercerestapi.service.Impl;
2+
3+
import com.manir.springbootecommercerestapi.model.OrderProducts;
4+
import com.manir.springbootecommercerestapi.repository.OrderProductsRepository;
5+
import com.manir.springbootecommercerestapi.service.OrderProductsService;
6+
import lombok.AllArgsConstructor;
7+
import org.springframework.stereotype.Service;
8+
9+
import javax.annotation.Resource;
10+
11+
@Service
12+
@AllArgsConstructor
13+
public class OrderProductsServiceImpl implements OrderProductsService {
14+
15+
@Resource(name = "orderProductsRepository")
16+
private final OrderProductsRepository orderProductsRepository;
17+
18+
@Override
19+
public void addOrderProducts(OrderProducts orderProducts) {
20+
orderProductsRepository.save(orderProducts);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.manir.springbootecommercerestapi.service.Impl;
2+
3+
import com.manir.springbootecommercerestapi.dto.CartItemDto;
4+
import com.manir.springbootecommercerestapi.dto.OrderDto;
5+
import com.manir.springbootecommercerestapi.exception.EcommerceApiException;
6+
import com.manir.springbootecommercerestapi.model.Order;
7+
import com.manir.springbootecommercerestapi.model.OrderProducts;
8+
import com.manir.springbootecommercerestapi.model.User;
9+
import com.manir.springbootecommercerestapi.repository.CartItemRepository;
10+
import com.manir.springbootecommercerestapi.repository.OrderRepository;
11+
import com.manir.springbootecommercerestapi.response.CartItemResponse;
12+
import com.manir.springbootecommercerestapi.service.OrderProductsService;
13+
import com.manir.springbootecommercerestapi.service.OrderService;
14+
import com.manir.springbootecommercerestapi.service.ShoppingCartService;
15+
import lombok.AllArgsConstructor;
16+
import org.modelmapper.ModelMapper;
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.http.HttpStatus;
19+
import org.springframework.stereotype.Service;
20+
21+
import javax.transaction.Transactional;
22+
import java.util.List;
23+
import java.util.stream.Collectors;
24+
25+
@Service
26+
@AllArgsConstructor
27+
public class OrderServiceImpl implements OrderService {
28+
29+
@Autowired
30+
private final OrderRepository orderRepository;
31+
@Autowired
32+
private final OrderProductsService orderProductsService;
33+
@Autowired
34+
private final CartItemRepository cartItemRepository;
35+
@Autowired
36+
private final ShoppingCartService shoppingCartService;
37+
@Autowired
38+
private final ModelMapper modelMapper;
39+
40+
@Override
41+
@Transactional
42+
public void placeOrder(User customer) {
43+
CartItemResponse cartItemDto = shoppingCartService.findByCustomer(customer);
44+
OrderDto orderDto = new OrderDto();
45+
orderDto.setTotalPrice(cartItemDto.getTotalCost());
46+
orderDto.setEmail(customer.getEmail());
47+
orderDto.setName(customer.getName());
48+
OrderDto savedOrder = saveOrder(orderDto, customer);
49+
List<CartItemDto> cartItemDtoList = cartItemDto.getContent();
50+
for(CartItemDto cartItem : cartItemDtoList){
51+
OrderProducts orderProducts = new OrderProducts();
52+
orderProducts.setCustomer(customer);
53+
orderProducts.setProduct(cartItem.getProduct());
54+
orderProducts.setOrder(mapToEntity(savedOrder));
55+
orderProducts.setProductPrice(cartItem.getProduct().getPrice());
56+
orderProducts.setProductQuantity(cartItem.getQuantity());
57+
orderProducts.setTotalPrice(cartItemDto.getTotalCost());
58+
orderProductsService.addOrderProducts(orderProducts);
59+
}
60+
cartItemRepository.deleteByCustomerId(customer.getId());
61+
}
62+
63+
@Override
64+
public OrderDto saveOrder(OrderDto orderDto, User customer) {
65+
//convert to entity
66+
Order order = mapToEntity(orderDto);
67+
//save order to db
68+
Order placedOrder = orderRepository.save(order);
69+
return mapToDto(placedOrder);
70+
}
71+
72+
@Override
73+
public List<OrderDto> listOrdersByCustomer(User customer) {
74+
List<Order> orders = orderRepository.findByCustomer(customer);
75+
if (orders.size() == 0){
76+
throw new EcommerceApiException("User has no order", HttpStatus.BAD_REQUEST);
77+
}
78+
List<OrderDto> orderDtoList = orders.stream()
79+
.map(order -> mapToDto(order))
80+
.collect(Collectors.toList());
81+
return orderDtoList;
82+
}
83+
84+
//map to Entity
85+
private Order mapToEntity(OrderDto orderDto){
86+
Order order = modelMapper.map(orderDto, Order.class);
87+
return order;
88+
}
89+
//map to Dto
90+
private OrderDto mapToDto(Order order){
91+
OrderDto orderDto = modelMapper.map(order, OrderDto.class);
92+
return orderDto;
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.manir.springbootecommercerestapi.service;
2+
3+
import com.manir.springbootecommercerestapi.model.OrderProducts;
4+
5+
public interface OrderProductsService {
6+
void addOrderProducts(OrderProducts orderProducts);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.manir.springbootecommercerestapi.service;
2+
3+
import com.manir.springbootecommercerestapi.dto.OrderDto;
4+
import com.manir.springbootecommercerestapi.model.User;
5+
6+
import java.util.List;
7+
8+
public interface OrderService {
9+
10+
void placeOrder(User customer);
11+
OrderDto saveOrder(OrderDto orderDto, User customer);
12+
List<OrderDto> listOrdersByCustomer(User customer);
13+
}

0 commit comments

Comments
 (0)