Skip to content

Commit 4f28a19

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

File tree

13 files changed

+254
-8
lines changed

13 files changed

+254
-8
lines changed
Lines changed: 63 additions & 0 deletions
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+
}

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

Lines changed: 7 additions & 2 deletions
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
}

src/main/java/com/manir/springbootecommercerestapi/dto/OrderDto.java

Lines changed: 3 additions & 0 deletions
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
}
Lines changed: 13 additions & 0 deletions
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+
}

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

Lines changed: 3 additions & 4 deletions
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 {

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

Lines changed: 5 additions & 2 deletions
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"})

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

Lines changed: 4 additions & 0 deletions
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
}
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.OrderProducts;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface OrderProductsRepository extends JpaRepository<OrderProducts, Long> {
7+
8+
}
Lines changed: 12 additions & 0 deletions
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+
}
Lines changed: 22 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)