is there a way to loop through mysql database table id column

Viewed 41

I am working on a spring boot application with mysql database,it is a kind of ecommerce app, the issue is that I don't want to pass the id of the user_order (that is my table name)as a path variable during runtime. I don't know if there is a way that I can loop through the id column by the app and pass the ids depending on the number of items in the database. When I tried something like using getid, it returns null. Your help will be appreciated.

Here is my code.

My entity class

    @Entity
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class UserOrder {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
        private @NotNull int totalProduct;
        private @NotNull int userID;
    
    }

My repository

    @Repository
    public interface OrderRepository extends JpaRepository<UserOrder,Integer> {
    }
    

My service class

    @Service
    public class OrderService {
    
        @Autowired
        private CartService cartService;
    
        @Autowired
        private CartRepository cartRepository;
    
        @Autowired
        UserRepository userRepository;
    
        @Autowired
        private OrderRepository orderRepository;
    
    public UserOrderDto finalOrderList(User user) throws DataNotFoundException {
            UserOrder userOrder = new UserOrder();
    
            Optional<UserOrder> orderOptional = orderRepository.findById(userOrder.getId());
            if (!orderOptional.isPresent()) {
                throw new DataNotFoundException("Order Id not found");
            }
            
            userOrder.setUserID(orderOptional.get().getUserID());
            userOrder.setId(orderOptional.get().getId());
            userOrder.setTotalProduct(orderOptional.get().getTotalProduct());
            UserOrderDto userOrderDto = new UserOrderDto(userOrder);
            user.setId(userOrder.getUserID());
            if (userOrder.getUserID() != user.getId()) {
                throw new DataNotFoundException("You are not the owner of this Order");
            }
    
    
            if (Objects.isNull(user)) {
                throw new DataNotFoundException("This User has not place final order yet");
            }
            userRepository.findById(user.getId());
            List<Cart> cartList = cartRepository.findAllByUserOrderByCreatedDateDesc(user);
            List<CartItemDto> cartItemDtos = new ArrayList<>();
            double totalCost = 0;
            for (Cart cart : cartList) {
                CartItemDto cartItemDto = new CartItemDto(cart);
                totalCost += cartItemDto.getQuantity() * cart.getProduct().getPrice();
                cartItemDtos.add(cartItemDto);
            }
            CartDto cartDto = new CartDto();
            cartDto.setCartItemDtoList(cartItemDtos);
            cartDto.setTotalCost(totalCost);
    
            userOrderDto.setCartDtos(cartDto);
            return userOrderDto;
    
        }
    }
    

Here is my controller class

    @RestController
    @RequestMapping("/order")
    public class UserOrderController {
    
        @Autowired
        private AuthenticationTokenService authenticationTokenService;
    
        @Autowired
        private OrderService orderService;
    
        @Autowired
        private CartController cartController;
    
    @GetMapping("/list")
        public ResponseEntity<UserOrderDto> getFinalOrderList(@RequestParam("token") String token) throws DataNotFoundException {
            authenticationTokenService.authenticateToken(token);
            User user = authenticationTokenService.getUserByToken(token);
            UserOrderDto userOrderDto = orderService.finalOrderList(user);
            if (userOrderDto.getCartDtos().getCartItemDtoList().size() < 1) {
                throw new DataNotFoundException("This User has not place a final order yet");
            }
            return new ResponseEntity<>(userOrderDto, HttpStatus.OK);
        }
    }
    
    

Normally in my method named finalOrderList in the Orderservice class I supposed to have something like this

    public UserOrderDto finalOrderList(User user, Integer itemId) throws DataNotFoundException {
    
            Optional<UserOrder> orderOptional = orderRepository.findById(itemId);
            if (!orderOptional.isPresent()) {
                throw new DataNotFoundException("Order Id not found");
            }

But I don't want to pass the id manually during the time that the api is called, I want it to take the id on it own and return the data corresponding to it if only the id exist, and I want it to be a sort of loop which will check if the id correspond to the user id that's in that row.

0 Answers
Related