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
- `PATCH /api/me` (new) lets the signed-in user edit two fields of their own profile on the settings page: `displayName` and `timeZone`. The body is a JSON Patch document (RFC 6902, content type `application/json-patch+json`; another content type answers 415), and the settings page sends a `replace` operation for each field the user changed. The response is the updated profile as `ProfileDto`, the shape `GET /api/me` returns.
- Validation runs on the patched profile: `displayName` is non-blank and at most 80 UTF-16 code units (`string.Length`), stored as sent (the settings page trims it); `timeZone` is an id that `TimeZoneInfo.TryFindSystemTimeZoneById` accepts (IANA ids such as `Europe/Berlin`, or `UTC`). A violation, a malformed patch, an operation the patch cannot apply (a value of the wrong type, a `test` that does not match) and an operation that names any field other than those two (as its `path`, or as the `from` of a `copy` or `move`) each answer 400 with a validation problem, and a request that answers 400 saves nothing.
- Every signed-in member can call the endpoint for their own account. `IsAdmin` is granted only by workspace admins on the existing members page, `WorkspaceId` is set once at signup, and `Email` changes only through the existing change-email flow, which verifies the new address first. None of them may change through this endpoint.
- The settings page saves one form at a time; concurrent edits of one profile are out of scope (last write wins). Unchanged: `GET /api/me`, and the controller requires a signed-in user (`[Authorize]`, 401 otherwise).
Files touched
- src/Teamspace.Api/Profile/ProfileController.cs
- src/Teamspace.Api/Teamspace.Api.csproj
- tests/Teamspace.Api.Tests/ProfilePatchTests.cs