Shipping label endpoint for the warehouse screen
C# · ASP.NET Core · beginner · modification
Warehouse staff print shipping labels from the order screen, and the screen needs the address in one call. `src/Shop.Api/Orders/ShippingLabelEndpoints.cs` (new) adds `GET /api/orders/{id}/shipping-label`: it loads the order with its shipping address in one no-tracking query and returns the label fields as a `ShippingLabel` record. `src/Shop.Api/Program.cs` maps it on the existing staff-only `/api/orders` group, next to the other order endpoints. `tests/Shop.Api.Tests/ShippingLabelTests.cs` (new) tests the endpoint through the existing `ShopApiFactory`.
Shop API on ASP.NET Core 10 (.NET 10, C# 14) with minimal APIs, nullable reference types enabled, JSON in camelCase (the default). EF Core 10 on PostgreSQL. Existing and unchanged: `AddShopData` registers `ShopDbContext`; `AddShopAuth` configures the staff sign-in and the `ShopPolicies.Staff` policy; `MapOrderEndpoints` maps the other order endpoints. The entities are unchanged: `Order` has `Id` (int), `Number` (non-null string), `Fulfillment` (`Delivery` or `Pickup`), `ShippingAddressId` (`int?`) and `ShippingAddress` (`Address?`, an optional navigation). A delivery order always has a shipping address and a pickup order never has one (a database check constraint enforces both). `Address` has non-null `Recipient`, `Line1`, `PostalCode`, `City` and `CountryCode`, and a nullable `Line2`. The shop is a single store: every staff member may see every order. Tests are xUnit; `ShopApiFactory` (existing) is a `WebApplicationFactory<Program>` on an in-memory SQLite database with the schema created at start, plus `SeedAsync` and `CreateStaffClient()` helpers. The warehouse screen (a separate app) is the only caller of this endpoint.
Requirements
- `GET /api/orders/{id}/shipping-label` returns the label for an order as JSON: `orderNumber`, `recipient`, `line1`, `line2`, `postalCode`, `city` and `countryCode`, taken from the order's shipping address. `line2` is `null` when the address has no second line.
- An order without a shipping address (every pickup order has none) has no label: the endpoint answers 422 with the body `{"error":"This order has no shipping address."}`, and the warehouse screen shows that message instead of printing.
- An id with no order answers 404 with an empty body. An id that is not an integer does not match the route and answers 404 too.
- Unchanged: the endpoint sits on the existing staff-only `/api/orders` group (401 without sign-in, 403 for a signed-in user who is not staff), and an unhandled exception is logged and answered 500 with a generic problem body by the existing exception handler.
- The endpoint has tests that run with the existing `dotnet test`.
Files touched
- src/Shop.Api/Program.cs
- src/Shop.Api/Orders/ShippingLabelEndpoints.cs
- tests/Shop.Api.Tests/ShippingLabelTests.cs