Skip to content

Commit b02e91b

Browse files
committed
Find Customer order and ordered items are done.
1 parent 4f28a19 commit b02e91b

File tree

7 files changed

+63
-9
lines changed

7 files changed

+63
-9
lines changed

src/main/java/com/manir/springbootecommercerestapi/config/SecurityConfig.java

-6
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@
1111
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1212
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1313
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
14-
import org.springframework.security.core.userdetails.User;
15-
import org.springframework.security.core.userdetails.UserDetails;
16-
import org.springframework.security.core.userdetails.UserDetailsService;
1714
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1815
import org.springframework.security.crypto.password.PasswordEncoder;
19-
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
20-
21-
import javax.annotation.Resource;
2216

2317
@Configuration
2418
@EnableWebSecurity

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.manir.springbootecommercerestapi.controller;
22

33
import com.manir.springbootecommercerestapi.dto.OrderDto;
4+
import com.manir.springbootecommercerestapi.dto.OrderProductsDto;
45
import com.manir.springbootecommercerestapi.exception.EcommerceApiException;
5-
import com.manir.springbootecommercerestapi.model.Order;
66
import com.manir.springbootecommercerestapi.model.User;
77
import com.manir.springbootecommercerestapi.repository.UserRepository;
8+
import com.manir.springbootecommercerestapi.service.OrderProductsService;
89
import com.manir.springbootecommercerestapi.service.OrderService;
910
import com.manir.springbootecommercerestapi.utils.isAuthenticatedAsAdminOrUser;
1011
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,6 +31,8 @@ public class OrderController {
3031
private UserRepository userRepository;
3132
@Autowired
3233
private OrderService orderService;
34+
@Autowired
35+
private OrderProductsService orderProductsService;
3336

3437
//place order complete order api
3538
@isAuthenticatedAsAdminOrUser
@@ -60,4 +63,19 @@ public List<OrderDto> listOrdersByCustomer(@AuthenticationPrincipal Authenticati
6063
throw new EcommerceApiException("User not authenticated", HttpStatus.BAD_REQUEST);
6164
}
6265
}
66+
67+
//find ordered items by Customer
68+
@isAuthenticatedAsAdminOrUser
69+
@GetMapping("/findOrderedItemsByCustomer")
70+
public List<OrderProductsDto> findOrderedItemsByCustomer(@AuthenticationPrincipal Authentication authentication){
71+
authentication = SecurityContextHolder.getContext().getAuthentication();
72+
if (!(authentication instanceof AnonymousAuthenticationToken)){
73+
String currentUserEmail = authentication.getName();
74+
User customer = userRepository.findByEmail(currentUserEmail).orElseThrow(() -> new UsernameNotFoundException("Customer Not found"));
75+
List<OrderProductsDto> customerOrderedItems = orderProductsService.findOrderItemsByCustomer(customer);
76+
return customerOrderedItems;
77+
}else{
78+
throw new EcommerceApiException("User not authenticated", HttpStatus.BAD_REQUEST);
79+
}
80+
}
6381
}

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

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

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import com.manir.springbootecommercerestapi.model.User;
45
import lombok.Data;
56

@@ -14,5 +15,6 @@ public class OrderDto {
1415
private String note;
1516
private String status;
1617

18+
@JsonIgnore
1719
private User customer;
1820
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.manir.springbootecommercerestapi.repository;
22

33
import com.manir.springbootecommercerestapi.model.OrderProducts;
4+
import com.manir.springbootecommercerestapi.model.User;
45
import org.springframework.data.jpa.repository.JpaRepository;
56

6-
public interface OrderProductsRepository extends JpaRepository<OrderProducts, Long> {
7+
import java.util.List;
78

9+
public interface OrderProductsRepository extends JpaRepository<OrderProducts, Long> {
10+
List<OrderProducts> findByCustomer(User customer);
811
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,53 @@
11
package com.manir.springbootecommercerestapi.service.Impl;
22

3+
import com.manir.springbootecommercerestapi.dto.OrderProductsDto;
4+
import com.manir.springbootecommercerestapi.exception.EcommerceApiException;
35
import com.manir.springbootecommercerestapi.model.OrderProducts;
6+
import com.manir.springbootecommercerestapi.model.User;
47
import com.manir.springbootecommercerestapi.repository.OrderProductsRepository;
58
import com.manir.springbootecommercerestapi.service.OrderProductsService;
69
import lombok.AllArgsConstructor;
10+
import org.modelmapper.ModelMapper;
11+
import org.modelmapper.convention.MatchingStrategies;
12+
import org.springframework.http.HttpStatus;
713
import org.springframework.stereotype.Service;
814

915
import javax.annotation.Resource;
16+
import java.util.List;
17+
import java.util.stream.Collectors;
1018

1119
@Service
1220
@AllArgsConstructor
1321
public class OrderProductsServiceImpl implements OrderProductsService {
1422

1523
@Resource(name = "orderProductsRepository")
1624
private final OrderProductsRepository orderProductsRepository;
25+
@Resource(name = "modelMapper")
26+
private final ModelMapper modelMapper;
1727

1828
@Override
1929
public void addOrderProducts(OrderProducts orderProducts) {
2030
orderProductsRepository.save(orderProducts);
2131
}
32+
33+
@Override
34+
public List<OrderProductsDto> findOrderItemsByCustomer(User customer) {
35+
36+
List<OrderProducts> orderProducts = orderProductsRepository.findByCustomer(customer);
37+
if (orderProducts.size() == 0){
38+
throw new EcommerceApiException("User has no ordered products", HttpStatus.BAD_REQUEST);
39+
}
40+
List<OrderProductsDto> orderProductsDtoList = orderProducts.stream()
41+
.map(orderProduct -> mapToDto(orderProduct))
42+
.collect(Collectors.toList());
43+
return orderProductsDtoList;
44+
}
45+
46+
//map to dto
47+
private OrderProductsDto mapToDto(OrderProducts orderProducts){
48+
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
49+
OrderProductsDto orderProductsDto = modelMapper.map(orderProducts, OrderProductsDto.class);
50+
51+
return orderProductsDto;
52+
}
2253
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public void placeOrder(User customer) {
4545
orderDto.setTotalPrice(cartItemDto.getTotalCost());
4646
orderDto.setEmail(customer.getEmail());
4747
orderDto.setName(customer.getName());
48+
orderDto.setCustomer(customer);
4849
OrderDto savedOrder = saveOrder(orderDto, customer);
4950
List<CartItemDto> cartItemDtoList = cartItemDto.getContent();
5051
for(CartItemDto cartItem : cartItemDtoList){
@@ -54,7 +55,7 @@ public void placeOrder(User customer) {
5455
orderProducts.setOrder(mapToEntity(savedOrder));
5556
orderProducts.setProductPrice(cartItem.getProduct().getPrice());
5657
orderProducts.setProductQuantity(cartItem.getQuantity());
57-
orderProducts.setTotalPrice(cartItemDto.getTotalCost());
58+
orderProducts.setTotalPrice(cartItem.getProduct().getPrice()*cartItem.getQuantity());
5859
orderProductsService.addOrderProducts(orderProducts);
5960
}
6061
cartItemRepository.deleteByCustomerId(customer.getId());
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.manir.springbootecommercerestapi.service;
22

3+
import com.manir.springbootecommercerestapi.dto.OrderProductsDto;
34
import com.manir.springbootecommercerestapi.model.OrderProducts;
5+
import com.manir.springbootecommercerestapi.model.User;
6+
7+
import java.util.List;
48

59
public interface OrderProductsService {
610
void addOrderProducts(OrderProducts orderProducts);
11+
List<OrderProductsDto> findOrderItemsByCustomer(User customer);
712
}

0 commit comments

Comments
 (0)