Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .agents/skills/frontend-feature/SKILL.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A user with the `manage_workshops` permission adds a venue and a workshop in it,
- `workshop-edit` saves a new title with the loaded version. The detail page shows the title.
- `workshop-stale` refuses a save after another tab saved: the alert `This workshop changed after you opened it.` shows, and `Load the latest` loads the saved workshop into the form on the same URL.
- `workshop-actions` shows one button for each entry of `allowedActions`. `Schedule` moves the workshop to `Scheduled`. `Cancel workshop` opens the dialog `Cancel this workshop?` first.
- `workshop-search` puts the search in the URL (`?search=`) and lists the workshops whose title contains it.
- `workshop-search` puts the search in the URL (`?search=`), sends it as the filter `title @=* "<text>"`, and lists the workshops whose title contains it.

## How to get to it (user POV)

Expand All @@ -38,7 +38,7 @@ Preconditions:
- **Edit.** Click `Edit` in the group `Actions for <title>`. Fill `Title`, click `Save`. `PUT /api/v1/workshops/{id}` gives 200 with a `version` that is larger than the loaded one. The detail page shows the new title.
- **Stale save.** Open the edit page in this tab and in a second tab. Save a title in the second tab. Save another title in the first tab. `PUT` gives 409 with the type `urn:fullstacktemplate:problem:stale-version`. The alert shows, and `Title` keeps the text of the first tab. Click `Load the latest`. The URL stays `/workshops/<id>/edit`, and `Title` has the title of the second tab.
- **Actions.** On the detail page, the buttons equal `allowedActions` of the last `GET` (`Update` is the `Edit` link). Click `Schedule`. `POST /api/v1/workshops/{id}/schedule` sends the loaded `version` and gives 200 with the status `Scheduled`. The page shows `Scheduled`, `Cancel workshop`, and `Mark completed`. `Schedule` and `Edit` are gone. Click `Cancel workshop`. The dialog `Cancel this workshop?` shows. Click `Keep workshop`. The dialog closes, no `POST .../cancel` goes out, and `Cancel workshop` has the focus.
- **Search.** Open `/workshops`, type a word of the title into the searchbox `Search workshops`. The URL gets `?search=<word>`, and `GET /api/v1/workshops` has the same `search`. The row of the workshop shows, with the start in the zone of the venue.
- **Search.** Open `/workshops`, type a word of the title into the searchbox `Search workshops`. The URL gets `?search=<word>`, and `GET /api/v1/workshops` has `filters=title @=* "<word>"`. Text with a `"` goes in a padded raw string, as `docs/backend/features-and-cqrs.md`, "Search text", says. The row of the workshop shows, with the start in the zone of the venue.
- **Proof.** Run `node $S/scripts/browse.mjs $S/recipes/examples/workshops.mjs`. The captures `no-tenant-list`, `empty-list`, `venue-picked`, `detail-draft`, `stale-alert`, `latest-loaded`, `scheduled`, `cancel-dialog`, and `search` show the states.

## Gotchas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ export default async ({ page, context, step, capture, settle, expect, login }) =
expect(sent).toBe(0)
})

await step('search: the URL and the request have the search, and the row shows', async () => {
await step('search: the URL has the search, the request has its title filter, and the row shows', async () => {
await page.goto('/workshops')
await settle()
const searched = page.waitForResponse(
(r) => new URL(r.url()).pathname === '/api/v1/workshops' && new URL(r.url()).searchParams.get('search') === stamp,
(r) => new URL(r.url()).pathname === '/api/v1/workshops' && new URL(r.url()).searchParams.get('filters') === `title @=* "${stamp}"`,
)
await page.getByRole('searchbox', { name: 'Search workshops' }).fill(stamp)
expect((await searched).status()).toBe(200)
Expand Down
2 changes: 1 addition & 1 deletion .agents/skills/writing-tests/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Examples:
| A new query hook or an error toast | `pnpm test` and `pnpm test:e2e` |
| A new auth provider, or a change to the cookie or the refresh of the BFF | `pnpm test:e2e:stack` against that provider |
| A change to the `/api` proxy of the Vite dev server | Both Playwright suites |
| A new search on a list | An integration test with `%`, `_`, and `\` in the search. See `docs/backend.md`, section "Tests" |
| A new text field that a list filters with `@=*` | An integration test with `%`, `_`, `\`, and a quote at an end in the filter value, built with `QueryKitText.Quote`. See `docs/backend.md`, section "Tests" |

## Write a functional test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@ namespace FullstackTemplate.Server.Examples.Venues.Dtos;

public sealed class VenueParametersDto : BasePaginationParameters
{
public const int SearchMaxLength = Venue.NameMaxLength;

public string? Filters { get; set; }

public string? SortOrder { get; set; }

public string? Search { get; set; }

public static VenueParametersDto From(
int? pageNumber, int? pageSize, string? filters, string? sortOrder, string? search)
int? pageNumber, int? pageSize, string? filters, string? sortOrder)
{
var parameters = new VenueParametersDto
{
Filters = string.IsNullOrEmpty(filters) ? null : filters,
SortOrder = string.IsNullOrEmpty(sortOrder) ? null : sortOrder,
Search = string.IsNullOrWhiteSpace(search) ? null : search.Trim(),
};
parameters.SetPage(pageNumber, pageSize);
return parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ public void Map(IEndpointRouteBuilder versionedGroup) => versionedGroup
.ProducesValidationProblem();

/// <summary>
/// Gets a paginated list of Venues. The search finds the venues whose name contains the text.
/// Gets a paginated list of Venues.
/// </summary>
public static async Task<Ok<PagedList<VenueDto>>> Handle(
[MaxLength(CustomQueryKitConfiguration.FilterMaxLength)] string? filters,
string? sortOrder,
[MaxLength(VenueParametersDto.SearchMaxLength)] string? search,
[Range(1, BasePaginationParameters.MaxPageNumber)] int? pageNumber,
[Range(1, int.MaxValue)] int? pageSize,
IMediator mediator,
CancellationToken cancellationToken)
{
var parameters = VenueParametersDto.From(pageNumber, pageSize, filters, sortOrder, search);
var parameters = VenueParametersDto.From(pageNumber, pageSize, filters, sortOrder);

var result = await mediator.Send(new GetVenueList.Query(parameters), cancellationToken);
return TypedResults.Ok(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace FullstackTemplate.Server.Examples.Venues.Features;

using Databases;
using Dtos;
using Exceptions;
using Mappings;
using MediatR;
using Microsoft.EntityFrameworkCore;
Expand All @@ -14,29 +13,14 @@ public sealed record Query(VenueParametersDto Parameters) : IRequest<PagedList<V

public sealed class Handler(AppDbContext dbContext) : IRequestHandler<Query, PagedList<VenueDto>>
{
private const string SearchParameter = "search";

public async Task<PagedList<VenueDto>> Handle(Query request, CancellationToken cancellationToken)
{
var queryKitConfig = new CustomQueryKitConfiguration();

var venues = dbContext.Venues
var query = dbContext.Venues
.TagWithOperationCallSite("venues.list")
.AsNoTracking();

if (request.Parameters.Search is { } search)
{
if (search.Length > VenueParametersDto.SearchMaxLength)
throw new QueryInputException(SearchParameter,
$"A search can have no more than {VenueParametersDto.SearchMaxLength} characters.");
if (!PostgresText.CanStore(search))
throw new QueryInputException(SearchParameter, "The search has a character that is not allowed.");

var pattern = LikePattern.Containing(search);
venues = venues.Where(v => EF.Functions.ILike(v.Name, pattern, LikePattern.EscapeCharacter));
}

var query = venues.ToVenueDtoQueryable();
.AsNoTracking()
.ToVenueDtoQueryable();

if (!string.IsNullOrWhiteSpace(request.Parameters.Filters))
query = query.ApplyFilters(request.Parameters.Filters, queryKitConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@ namespace FullstackTemplate.Server.Examples.Workshops.Dtos;

public sealed class WorkshopParametersDto : BasePaginationParameters
{
public const int SearchMaxLength = Workshop.TitleMaxLength;

public string? Filters { get; set; }

public string? SortOrder { get; set; }

public string? Search { get; set; }

public static WorkshopParametersDto From(
int? pageNumber, int? pageSize, string? filters, string? sortOrder, string? search)
int? pageNumber, int? pageSize, string? filters, string? sortOrder)
{
var parameters = new WorkshopParametersDto
{
Filters = string.IsNullOrEmpty(filters) ? null : filters,
SortOrder = string.IsNullOrEmpty(sortOrder) ? null : sortOrder,
Search = string.IsNullOrWhiteSpace(search) ? null : search.Trim(),
};
parameters.SetPage(pageNumber, pageSize);
return parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ public void Map(IEndpointRouteBuilder versionedGroup) => versionedGroup
.ProducesValidationProblem();

/// <summary>
/// Gets a paginated list of Workshops. The search finds the workshops whose title contains the text.
/// Gets a paginated list of Workshops.
/// </summary>
public static async Task<Ok<PagedList<WorkshopDto>>> Handle(
[MaxLength(CustomQueryKitConfiguration.FilterMaxLength)] string? filters,
string? sortOrder,
[MaxLength(WorkshopParametersDto.SearchMaxLength)] string? search,
[Range(1, BasePaginationParameters.MaxPageNumber)] int? pageNumber,
[Range(1, int.MaxValue)] int? pageSize,
IMediator mediator,
CancellationToken cancellationToken)
{
var parameters = WorkshopParametersDto.From(pageNumber, pageSize, filters, sortOrder, search);
var parameters = WorkshopParametersDto.From(pageNumber, pageSize, filters, sortOrder);

var result = await mediator.Send(new GetWorkshopList.Query(parameters), cancellationToken);
return TypedResults.Ok(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ public static class GetWorkshopListTool
public static async Task<McpPage<WorkshopDto>> Handle(
IMediator mediator,
CancellationToken cancellationToken,
[Description("A text that the title contains, with no regard to case.")] string? search = null,
[Description("The page number. The first page is 1. Leave it out for the first page.")] int? pageNumber = null,
[Description("The number of workshops on a page, at least 1. Leave it out for the default size. A value more than the maximum gives the maximum. The result gives the size that was used.")] int? pageSize = null,
[Description("A QueryKit filter on the fields of a workshop, for example: status == \"Scheduled\"")] string? filters = null,
[Description("A QueryKit filter on the fields of a workshop, for example: title @=* \"text\". Put text with a \" in a raw string with one space on each side, for example: title @=* \"\"\" say \"hi\" \"\"\". Its delimiter is one quote more than the longest run of quotes in the text, and at least three quotes.")] string? filters = null,
[Description("A QueryKit sort order, for example: -startsAt")] string? sortOrder = null)
{
var parameters = WorkshopParametersDto.From(pageNumber, pageSize, filters, sortOrder, search);
var parameters = WorkshopParametersDto.From(pageNumber, pageSize, filters, sortOrder);
var result = await mediator.Send(new GetWorkshopList.Query(parameters), cancellationToken);
return McpPage.From(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace FullstackTemplate.Server.Examples.Workshops.Features;
using Databases;
using Domain.Users;
using Dtos;
using Exceptions;
using Mappings;
using MediatR;
using Microsoft.EntityFrameworkCore;
Expand All @@ -16,31 +15,16 @@ public sealed record Query(WorkshopParametersDto Parameters) : IRequest<PagedLis
public sealed class Handler(AppDbContext dbContext, IPermissionGuard permissionGuard)
: IRequestHandler<Query, PagedList<WorkshopDto>>
{
private const string SearchParameter = "search";

public async Task<PagedList<WorkshopDto>> Handle(Query request, CancellationToken cancellationToken)
{
var caller = await permissionGuard.GetCallerAsync(cancellationToken);

var queryKitConfig = new CustomQueryKitConfiguration();

var workshops = dbContext.Workshops
var query = dbContext.Workshops
.TagWithOperationCallSite("workshops.list")
.AsNoTracking();

if (request.Parameters.Search is { } search)
{
if (search.Length > WorkshopParametersDto.SearchMaxLength)
throw new QueryInputException(SearchParameter,
$"A search can have no more than {WorkshopParametersDto.SearchMaxLength} characters.");
if (!PostgresText.CanStore(search))
throw new QueryInputException(SearchParameter, "The search has a character that is not allowed.");

var pattern = LikePattern.Containing(search);
workshops = workshops.Where(w => EF.Functions.ILike(w.Title, pattern, LikePattern.EscapeCharacter));
}

var query = workshops.ToWorkshopDtoQueryable(dbContext.Venues);
.AsNoTracking()
.ToWorkshopDtoQueryable(dbContext.Venues);

if (!string.IsNullOrWhiteSpace(request.Parameters.Filters))
query = query.ApplyFilters(request.Parameters.Filters, queryKitConfig);
Expand Down
20 changes: 2 additions & 18 deletions FullstackTemplate.Server/openapi/FullstackTemplate.Server.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ A test never takes today from the UTC date. Take it from the zone of the place i

TUnit runs all tests in parallel, also the tests of one class. The integration and functional tests share one fixture for each assembly, which `AssemblyHooks.cs` starts and stops. Each test must create its own data and must not depend on rows from other tests. If a test reads state of the whole process, such as all exported spans, put `[NotInParallel]` on its class.

A list with a search has an integration test that searches for `50%`, `a_b`, and `c\d`, and finds only the row with that text. See `tests/FullstackTemplate.IntegrationTests/Databases/LikePatternTests.cs`.
A list has an integration test that filters its text field with `@=*` for `50%`, `a_b`, `c\d`, and text with a quote at an end, and finds only the row with that text. Build each filter value with `QueryKitText.Quote`. A raw `ILike` with `LikePattern` has the same test. See `tests/FullstackTemplate.IntegrationTests/Databases/LikePatternTests.cs`.

To run the tests in a web UI, open the **Labcoat UI** link of the `tests` resource in the Aspire dashboard. [Labcoat](https://github.com/eriklieben/labcoat) builds and runs the test projects, and shows each failure with its source.

Expand Down
12 changes: 6 additions & 6 deletions docs/backend/features-and-cqrs.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,17 @@ public static class GetUserList

### Search text

A free-text search of a list uses `EF.Functions.ILike(column, LikePattern.Containing(search), LikePattern.EscapeCharacter)`. A name check that ignores case uses `LikePattern.Exactly`, and a prefix match uses `LikePattern.StartingWith`. `LikePattern` escapes `%`, `_`, and `\`, so a search for `50%` or `a_b` finds only the rows with that text. A name check gives a clear error, but only a unique index on the name stops two requests that save the same name at one time.
A list has no `search` parameter. A caller finds text through `filters`, with the QueryKit operator `@=*` (contains, ignore case), for example `title @=* "text"`. EF Core escapes the pattern of the QueryKit contains, starts-with, and ends-with operators, so a filter for `50%` or `a_b` finds only the rows with that text. `ApplyFilters` refuses a filter that fails `PostgresText.CanStore`, with the `filters` parameter. See [Validation](#validation).

`BannedSymbols.txt` bans the two-argument `ILike` and `Like` (RS0030). They send `ESCAPE ''`, so the backslash of an escaped pattern escapes nothing.

Trim the search, and give it a max length on the parameters DTO. If the search is empty after the trim, do not apply it. If the search fails `PostgresText.CanStore`, throw a `QueryInputException` with the name of the parameter. See [Validation](#validation). Apply the search before `ApplyFilters`, on the entity or on the DTO projection. The query keeps its fixed sort.
QueryKit has no escape in a quoted value. Text with a `"` goes in a raw string with one space of padding on each side, for example `title @=* """ say "hi" """`. The delimiter is one quote longer than the longest run of quotes in the text, and at least three quotes long. QueryKit removes the padding, so a quote at the start or the end of the text stays in the text. Give the MCP description of `filters` this rule, as the lists that `scripts/new-feature.cs` writes do. `QueryKitText.Quote` in the shared test helpers and `quoteText` in `frontend/src/lib/querykit-text.ts` write it.

<!-- example:start -->
The workshop list searches the title: [`GetWorkshopList.Handler.Handle`](../../FullstackTemplate.Server/Examples/Workshops/Features/GetWorkshopList/GetWorkshopList.cs).
The workshop list test finds titles with `%`, `_`, `\`, and quotes through a filter: [`WorkshopListQueryTests`](../../tests/FullstackTemplate.IntegrationTests/Examples/Workshops/WorkshopListQueryTests.cs).
<!-- example:end -->

A QueryKit filter needs no `LikePattern`. EF Core escapes the pattern of its contains, starts-with, and ends-with operators.
A raw `EF.Functions.ILike` outside QueryKit, such as a name check that ignores case or a prefix match, uses `LikePattern`: `LikePattern.Exactly` for a name check, `LikePattern.StartingWith` for a prefix, and `LikePattern.Containing` for a contains, each with `LikePattern.EscapeCharacter` as the third argument. `LikePattern` escapes `%`, `_`, and `\`. A name check gives a clear error, but only a unique index on the name stops two requests that save the same name at one time.

`BannedSymbols.txt` bans the two-argument `ILike` and `Like` (RS0030). They send `ESCAPE ''`, so the backslash of an escaped pattern escapes nothing.

## Permission checks

Expand Down
Loading
Loading