Invoice details endpoint for the invoice page
C# · ASP.NET Core · beginner · modification
Adds the invoice details endpoint the new invoice page calls. `InvoicesController` gets `GET /api/invoices/{id}`, which loads the invoice and projects it straight into the new `InvoiceDetailsDto`, with its lines as `InvoiceLineDto` records in position order, so only the columns the page needs are read; an id with no invoice answers 404. Both records live next to the existing summary record in `InvoiceDtos.cs`. `InvoiceDetailsTests.cs` adds endpoint tests on the existing `BillingApiFactory`.
ASP.NET Core 10 Web API on .NET 10 (C# 14, nullable reference types enabled), controllers with `[ApiController]`. EF Core 10 with Npgsql in production; `BillingDbContext` is unchanged: `Invoice` has `Id` (int identity, sequential), `CompanyId`, `Number`, `IssuedOn` and `DueOn` (`DateOnly`), `Status` (`draft`, `sent`, `paid` or `void`), `TotalCents` (long) and `Lines`; `InvoiceLine` has `InvoiceId`, `Position` (int), `Description`, `Quantity` (int), `UnitPriceCents` and `AmountCents` (long), at most 200 lines per invoice. Amounts are stored when the invoice is issued and are never recomputed by this API. Authentication is unchanged in `Program.cs`: JWT bearer tokens from the company's identity provider, and every token carries a `company_id` claim, read by `User.GetCompanyId()` (`Billing.Api.Auth.ClaimsPrincipalExtensions`, existing). Each company's users may see all of that company's invoices; there are no finer roles. Tests use xUnit and the existing `BillingApiFactory` (`WebApplicationFactory<Program>` over an in-memory SQLite database): `CreateClientFor(companyId)` signs requests in as a user of that company, and `SeedInvoiceAsync(companyId, number, lines...)` inserts a `sent` invoice dated 2026-10-01, due 2026-10-31. The invoice page itself ships separately in the web app.
Requirements
- `GET /api/invoices/{id}` (new) returns one invoice for the new invoice page: its number, issue and due dates, status, total and its lines (description, quantity, unit price and amount), every amount in integer cents as stored. The lines come in their `Position` order.
- A user sees only their own company's invoices, as on the existing list: an invoice that belongs to another company answers 404, exactly like an id that does not exist, so the API does not reveal which invoice ids exist.
- Unchanged: the controller requires a signed-in user (`[Authorize]`, 401 otherwise), and `GET /api/invoices` keeps its behaviour (the caller company's 100 newest invoices).
Files touched
- src/Billing.Api/Invoices/InvoicesController.cs
- src/Billing.Api/Invoices/InvoiceDtos.cs
- tests/Billing.Api.Tests/InvoiceDetailsTests.cs