diff --git a/SVSim.EmulatedEntrypoint/Controllers/AccountController.cs b/SVSim.EmulatedEntrypoint/Controllers/AccountController.cs new file mode 100644 index 0000000..fd50d8a --- /dev/null +++ b/SVSim.EmulatedEntrypoint/Controllers/AccountController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using SVSim.Database; +using SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Account; + +namespace SVSim.EmulatedEntrypoint.Controllers; + +/// +/// /account/* — viewer profile mutations that aren't tied to a specific subsystem. +/// +public class AccountController : SVSimController +{ + private readonly SVSimDbContext _db; + + public AccountController(SVSimDbContext db) + { + _db = db; + } + + [HttpPost("update_name")] + public async Task UpdateName([FromBody] AccountUpdateNameRequest request) + { + if (!TryGetViewerId(out long viewerId)) return Unauthorized(); + + var viewer = await _db.Viewers.FirstAsync(v => v.Id == viewerId); + viewer.DisplayName = request.Name; + await _db.SaveChangesAsync(); + + // Prod returns `data: []` — empty array, not empty object. Use an empty array literal + // so the translation middleware emits the right msgpack shape. + return Ok(Array.Empty()); + } +} diff --git a/SVSim.EmulatedEntrypoint/Models/Dtos/Requests/Account/AccountUpdateNameRequest.cs b/SVSim.EmulatedEntrypoint/Models/Dtos/Requests/Account/AccountUpdateNameRequest.cs new file mode 100644 index 0000000..9895d11 --- /dev/null +++ b/SVSim.EmulatedEntrypoint/Models/Dtos/Requests/Account/AccountUpdateNameRequest.cs @@ -0,0 +1,13 @@ +using System.Text.Json.Serialization; +using MessagePack; +using SVSim.EmulatedEntrypoint.Models.Dtos.Requests; + +namespace SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Account; + +[MessagePackObject] +public class AccountUpdateNameRequest : BaseRequest +{ + [JsonPropertyName("name")] + [Key("name")] + public string Name { get; set; } = string.Empty; +} diff --git a/SVSim.UnitTests/Controllers/AccountControllerTests.cs b/SVSim.UnitTests/Controllers/AccountControllerTests.cs new file mode 100644 index 0000000..6473a06 --- /dev/null +++ b/SVSim.UnitTests/Controllers/AccountControllerTests.cs @@ -0,0 +1,34 @@ +using System.Net; +using System.Text; +using System.Text.Json; +using NUnit.Framework; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using SVSim.Database; +using SVSim.UnitTests.Infrastructure; + +namespace SVSim.UnitTests.Controllers; + +public class AccountControllerTests +{ + [Test] + public async Task UpdateName_writes_display_name() + { + using var factory = new SVSimTestFactory(); + long viewerId = await factory.SeedViewerAsync(tutorialState: 0); + using var client = factory.CreateAuthenticatedClient(viewerId); + + var requestJson = """{"name":"littlefootse","viewer_id":"0","steam_id":0,"steam_session_ticket":""}"""; + + var response = await client.PostAsync("/account/update_name", + new StringContent(requestJson, Encoding.UTF8, "application/json")); + + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + + // Verify persisted name. + using var scope = factory.Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + var viewer = await db.Viewers.FirstAsync(v => v.Id == viewerId); + Assert.That(viewer.DisplayName, Is.EqualTo("littlefootse")); + } +}