Search box for the Customers page
C# · ASP.NET Core · beginner · modification
Adds the search endpoint behind the new search box on the Customers page. `CustomersController` gets `GET /api/customers/search`: it trims the text, answers 400 when nothing is left, and finds the account's customers whose name or email contains it with a case-insensitive `ILIKE` on both columns, written as SQL through `FromSqlRaw` and then sorted by name and capped at 25 with LINQ. `%` and `_` are escaped first, so they match themselves rather than acting as wildcards. `CustomerSearchTests.cs` adds endpoint tests on the existing `CrmApiFactory`.
ASP.NET Core 10 Web API on .NET 10 (C# 14, nullable reference types enabled), controllers with `[ApiController]`. EF Core 10 with Npgsql on PostgreSQL 16 (default settings, `standard_conforming_strings` on). `CrmDbContext` is unchanged: it maps `Customer` to the `customers` table with columns `id`, `account_id`, `name`, `email`, `phone` (nullable) and `created_at`; names and emails are stored as typed, and case is ignored as PostgreSQL `ILIKE` folds it. `CustomerDto(Id, Name, Email, Phone)` exists in `CustomerDtos.cs`, unchanged. Authentication is unchanged in `Program.cs`: JWT bearer tokens from the identity provider, each carrying an `account_id` claim read by `User.GetAccountId()` (`Crm.Api.Auth.ClaimsPrincipalExtensions`, existing). Every user of an account may see all of that account's customers; there are no finer roles. Unhandled exceptions go to `UseExceptionHandler` with ProblemDetails, which answers 500 without the exception text. Tests use xUnit and the existing `CrmApiFactory` (`WebApplicationFactory<Program>` over a fresh PostgreSQL 16 database per test class): `CreateClientFor(accountId)` signs requests in as a user of that account, and `SeedCustomersAsync(accountId, (name, email)...)` inserts customers. The search box itself ships separately in the web app.
Requirements
- `GET /api/customers/search?q=...` (new) powers the search box on the Customers page: it returns the caller's account's customers whose name or email contains the search text, ignoring case, at most 25 of them, ordered by name, in the same `CustomerDto` shape as the list.
- The search text is matched as typed, after trimming whitespace at both ends, so `%` and `_` match only themselves. A missing or blank `q` answers 400.
- A user only ever sees customers of their own account, as on the list.
- Unchanged: the controller requires a signed-in user (`[Authorize]`, 401 otherwise), and `GET /api/customers` keeps its behaviour (the account's 50 newest customers).
Files touched
- src/Crm.Api/Customers/CustomersController.cs
- tests/Crm.Api.Tests/CustomerSearchTests.cs