feat: Add order retrieval functionality
- Implement endpoints to retrieve a single order by ID. - Implement endpoints to retrieve all orders for a specific customer. - Enhance OrdersRepository to support querying orders by customer ID. - Add corresponding methods in OrdersService for order retrieval.
This commit is contained in:
@@ -6,10 +6,9 @@ import com.vegamarket.orderservice.services.OrdersService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/orders")
|
||||
@@ -21,4 +20,14 @@ public class OrdersController {
|
||||
public ResponseEntity<Orders> create(@RequestBody NewOrderDTO createDTO) {
|
||||
return new ResponseEntity<>(ordersService.createOrder(createDTO), HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@GetMapping("{orderId}")
|
||||
public ResponseEntity<Orders> get(@PathVariable Long orderId) {
|
||||
return ResponseEntity.ok().body(ordersService.getById(orderId));
|
||||
}
|
||||
|
||||
@GetMapping("/customer/{customerId}")
|
||||
public ResponseEntity<List<Orders>> getAllByCustomer(@PathVariable Long customerId) {
|
||||
return ResponseEntity.ok().body(ordersService.getAllByCustomerId(customerId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,5 +2,8 @@ package com.vegamarket.orderservice.model;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface OrdersRepository extends JpaRepository<Orders, Long> {
|
||||
List<Orders> findALlByCustomerId(Long customerId);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,18 @@ public class OrdersService {
|
||||
return order;
|
||||
}
|
||||
|
||||
public Orders getById(Long id) {
|
||||
Orders order = ordersRepository.findById(id).orElseThrow(() -> {
|
||||
throw new RuntimeException("Order not found");
|
||||
});
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
public List<Orders> getAllByCustomerId(Long customerId) {
|
||||
return ordersRepository.findALlByCustomerId(customerId);
|
||||
}
|
||||
|
||||
private BigDecimal getTotalAmount(List<OrderItems> items) {
|
||||
BigDecimal totalAmount = BigDecimal.ZERO;
|
||||
for (OrderItems item : items) {
|
||||
|
||||
Reference in New Issue
Block a user