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:
2026-02-26 17:45:33 -03:00
parent f29c695a2c
commit 5a7f447d96
3 changed files with 28 additions and 4 deletions

View File

@@ -6,10 +6,9 @@ import com.vegamarket.orderservice.services.OrdersService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.List;
import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/orders") @RequestMapping("/orders")
@@ -21,4 +20,14 @@ public class OrdersController {
public ResponseEntity<Orders> create(@RequestBody NewOrderDTO createDTO) { public ResponseEntity<Orders> create(@RequestBody NewOrderDTO createDTO) {
return new ResponseEntity<>(ordersService.createOrder(createDTO), HttpStatus.CREATED); 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));
}
} }

View File

@@ -2,5 +2,8 @@ package com.vegamarket.orderservice.model;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface OrdersRepository extends JpaRepository<Orders, Long> { public interface OrdersRepository extends JpaRepository<Orders, Long> {
List<Orders> findALlByCustomerId(Long customerId);
} }

View File

@@ -31,6 +31,18 @@ public class OrdersService {
return order; 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) { private BigDecimal getTotalAmount(List<OrderItems> items) {
BigDecimal totalAmount = BigDecimal.ZERO; BigDecimal totalAmount = BigDecimal.ZERO;
for (OrderItems item : items) { for (OrderItems item : items) {