Step 04 — OHS (Sales)
- OrderController OHS versionné sous /api/sales/v1/
- OrderViewAssembler : assemble le modèle interne → OrderView (Published Language)
- Endpoints : POST /orders, GET /orders/{id}, POST /orders/{id}/confirm, GET /customers/{id}/orders
- Tests vérifiant que les modèles internes ne sont jamais exposés
This commit is contained in:
84
src/Sales/Interfaces/Http/Api/V1/OrderController.php
Normal file
84
src/Sales/Interfaces/Http/Api/V1/OrderController.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MiniShop\Sales\Interfaces\Http\Api\V1;
|
||||
|
||||
use MiniShop\Sales\Application\Command\ConfirmOrder;
|
||||
use MiniShop\Sales\Application\Command\ConfirmOrderHandler;
|
||||
use MiniShop\Sales\Application\Command\PlaceOrder;
|
||||
use MiniShop\Sales\Application\Command\PlaceOrderHandler;
|
||||
use MiniShop\Sales\Application\Query\GetOrderById;
|
||||
use MiniShop\Sales\Application\Query\GetOrderByIdHandler;
|
||||
use MiniShop\Sales\Application\Query\ListOrdersByCustomer;
|
||||
use MiniShop\Sales\Application\Query\ListOrdersByCustomerHandler;
|
||||
use MiniShop\Shared\Technical\UuidGenerator;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
/**
|
||||
* Open Host Service — sales.v1
|
||||
* API stable et versionnee. Les modeles internes ne sont jamais exposes.
|
||||
* Tous les DTOs de reponse passent par OrderViewAssembler → OrderView (Published Language).
|
||||
*/
|
||||
#[Route('/api/sales/v1')]
|
||||
final readonly class OrderController
|
||||
{
|
||||
public function __construct(
|
||||
private PlaceOrderHandler $placeOrderHandler,
|
||||
private ConfirmOrderHandler $confirmOrderHandler,
|
||||
private GetOrderByIdHandler $getOrderByIdHandler,
|
||||
private ListOrdersByCustomerHandler $listOrdersByCustomerHandler,
|
||||
private OrderViewAssembler $assembler,
|
||||
) {}
|
||||
|
||||
#[Route('/orders', name: 'ohs_sales_place_order', methods: ['POST'])]
|
||||
public function placeOrder(Request $request): JsonResponse
|
||||
{
|
||||
/** @var array{customerId: string, lines: list<array{productName: string, quantity: int, unitPriceInCents: int, currency: string}>} $data */
|
||||
$data = json_decode($request->getContent(), true, flags: JSON_THROW_ON_ERROR);
|
||||
|
||||
$orderId = UuidGenerator::generate();
|
||||
|
||||
($this->placeOrderHandler)(new PlaceOrder(
|
||||
orderId: $orderId,
|
||||
customerId: $data['customerId'],
|
||||
lines: $data['lines'],
|
||||
));
|
||||
|
||||
$order = ($this->getOrderByIdHandler)(new GetOrderById($orderId));
|
||||
|
||||
return new JsonResponse($this->assembler->toView($order), Response::HTTP_CREATED);
|
||||
}
|
||||
|
||||
#[Route('/orders/{orderId}', name: 'ohs_sales_get_order', methods: ['GET'])]
|
||||
public function getOrder(string $orderId): JsonResponse
|
||||
{
|
||||
$order = ($this->getOrderByIdHandler)(new GetOrderById($orderId));
|
||||
|
||||
return new JsonResponse($this->assembler->toView($order));
|
||||
}
|
||||
|
||||
#[Route('/orders/{orderId}/confirm', name: 'ohs_sales_confirm_order', methods: ['POST'])]
|
||||
public function confirmOrder(string $orderId): JsonResponse
|
||||
{
|
||||
($this->confirmOrderHandler)(new ConfirmOrder(orderId: $orderId));
|
||||
|
||||
$order = ($this->getOrderByIdHandler)(new GetOrderById($orderId));
|
||||
|
||||
return new JsonResponse($this->assembler->toView($order));
|
||||
}
|
||||
|
||||
#[Route('/customers/{customerId}/orders', name: 'ohs_sales_list_orders', methods: ['GET'])]
|
||||
public function listOrders(string $customerId): JsonResponse
|
||||
{
|
||||
$orders = ($this->listOrdersByCustomerHandler)(new ListOrdersByCustomer($customerId));
|
||||
|
||||
return new JsonResponse(array_map(
|
||||
fn ($order) => $this->assembler->toView($order),
|
||||
$orders,
|
||||
));
|
||||
}
|
||||
}
|
||||
37
src/Sales/Interfaces/Http/Api/V1/OrderViewAssembler.php
Normal file
37
src/Sales/Interfaces/Http/Api/V1/OrderViewAssembler.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MiniShop\Sales\Interfaces\Http\Api\V1;
|
||||
|
||||
use MiniShop\Contracts\Sales\V1\Api\OrderView;
|
||||
use MiniShop\Sales\Domain\Model\Order;
|
||||
use MiniShop\Sales\Domain\Model\OrderLine;
|
||||
|
||||
/**
|
||||
* Assemble le modele interne Order vers le DTO public OrderView (sales.v1).
|
||||
* Les modeles internes de Sales ne sont jamais exposes directement.
|
||||
*/
|
||||
final class OrderViewAssembler
|
||||
{
|
||||
public function toView(Order $order): OrderView
|
||||
{
|
||||
return new OrderView(
|
||||
orderId: $order->id->toString(),
|
||||
customerId: $order->customerId->toString(),
|
||||
status: $order->status()->value,
|
||||
totalInCents: $order->total()->amount,
|
||||
currency: $order->total()->currency,
|
||||
lines: array_map(
|
||||
static fn (OrderLine $line): array => [
|
||||
'productName' => $line->productName,
|
||||
'quantity' => $line->quantity,
|
||||
'unitPriceInCents' => $line->unitPrice->amount,
|
||||
'currency' => $line->unitPrice->currency,
|
||||
'lineTotalInCents' => $line->lineTotal()->amount,
|
||||
],
|
||||
$order->lines(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user