Edit your profile from the settings page

C# · ASP.NET Core · intermediate · modification

Lets people edit their profile on the settings page. `ProfileController` gets `PATCH /api/me`, which takes a JSON Patch document (the settings page sends a `replace` operation for each field the user changed), rejects a null operation or one that lacks its `op`, `path` or (for `move` and `copy`) `from`, applies it to the signed-in user, checks the display name and the time zone and saves, or answers 400 with a validation problem. `Teamspace.Api.csproj` adds `Microsoft.AspNetCore.JsonPatch.SystemTextJson`, the System.Text.Json JSON Patch package of .NET 10. `ProfilePatchTests.cs` adds endpoint tests on the existing `TeamspaceApiFactory`.

Team workspace API on ASP.NET Core 10 and .NET 10 (C# 14, nullable reference types enabled, implicit usings), controllers with `[ApiController]`, EF Core 10 with Npgsql, xUnit for tests. Existing and unchanged: `Program.cs` registers the controllers, `AppDbContext`, JWT bearer authentication and the `WorkspaceAdmin` authorization policy. `AppDbContext.Users` is the `AppUser` entity: `int Id`, `int WorkspaceId` (a foreign key to the workspace), `string Email`, `string DisplayName`, `string TimeZone` and `bool IsAdmin`. Every token's `sub` claim is the `AppUser.Id` of an existing user, read by `User.GetUserId()` (`Teamspace.Api.Auth.ClaimsPrincipalExtensions`). The `WorkspaceAdmin` policy (admin pages and the members page) reads `IsAdmin` from the user's row on every request, and every workspace-scoped endpoint reads the caller's workspace from `AppUser.WorkspaceId` on every request. `ProfileDto(Id, Email, DisplayName, TimeZone, IsAdmin)` and `ProfileDto.From(AppUser)` exist in `ProfileDto.cs`. With the new package, the default System.Text.Json input formatter reads `JsonPatchDocument<T>` bodies; `ApplyTo` matches a path to a property of the target object and reports an operation whose path or value it cannot apply to the callback. Tests use the existing `TeamspaceApiFactory` (`WebApplicationFactory<Program>` over an in-memory SQLite database): `CreateClientFor(userId)` signs requests in as that user, `SeedUserAsync(workspaceId, email, isAdmin = false)` inserts a user whose display name is the part of the email before `@` and whose time zone is `UTC`, and `LoadUserAsync(id)` reads the saved row. The settings page ships separately in the web app.

Requirements

Files touched

Review this PR

C# practice