From c103ab6a87727e7de8d3063535f60c427cc3cbdd Mon Sep 17 00:00:00 2001 From: gamer147 Date: Sat, 27 Jun 2026 13:08:11 -0400 Subject: [PATCH] fix(guild): populate leader_name in search results (matches prod capture) Add IViewerRepository.LoadDisplayNamesAsync (batch AsNoTracking select), extend GuildSearchEntry with LeaderName, enrich GuildService.SearchAsync with a leader-id batch lookup, and thread the name through the controller's ToDetailDto call. Adds a leader_name assertion to GuildSearchFlowTests. Co-Authored-By: Claude Sonnet 4.6 --- .../Repositories/Viewer/IViewerRepository.cs | 6 ++++++ SVSim.Database/Repositories/Viewer/ViewerRepository.cs | 10 ++++++++++ SVSim.Database/Services/Guild/GuildService.cs | 7 +++++-- SVSim.Database/Services/Guild/GuildServiceTypes.cs | 2 +- .../Controllers/GuildController.cs | 2 +- .../Integration/Guild/GuildSearchFlowTests.cs | 3 +++ 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/SVSim.Database/Repositories/Viewer/IViewerRepository.cs b/SVSim.Database/Repositories/Viewer/IViewerRepository.cs index 76a4f546..492939ef 100644 --- a/SVSim.Database/Repositories/Viewer/IViewerRepository.cs +++ b/SVSim.Database/Repositories/Viewer/IViewerRepository.cs @@ -33,4 +33,10 @@ public interface IViewerRepository /// Clears Viewer.GuildId to null. No-op if the viewer does not exist. Task ClearGuildIdAsync(long viewerId, CancellationToken ct = default); + + /// + /// Batch-loads DisplayName for a set of viewer ids. Returns a dictionary keyed by viewer id; + /// ids with no matching row are absent from the result. Read-only (AsNoTracking). + /// + Task> LoadDisplayNamesAsync(IReadOnlyCollection viewerIds, CancellationToken ct = default); } diff --git a/SVSim.Database/Repositories/Viewer/ViewerRepository.cs b/SVSim.Database/Repositories/Viewer/ViewerRepository.cs index 0a0adcc0..fb0feb55 100644 --- a/SVSim.Database/Repositories/Viewer/ViewerRepository.cs +++ b/SVSim.Database/Repositories/Viewer/ViewerRepository.cs @@ -286,6 +286,16 @@ public class ViewerRepository : IViewerRepository await _dbContext.SaveChangesAsync(ct); } + public async Task> LoadDisplayNamesAsync(IReadOnlyCollection viewerIds, CancellationToken ct = default) + { + if (viewerIds.Count == 0) return new Dictionary(); + return await _dbContext.Set() + .AsNoTracking() + .Where(v => viewerIds.Contains(v.Id)) + .Select(v => new { v.Id, v.DisplayName }) + .ToDictionaryAsync(v => v.Id, v => v.DisplayName, ct); + } + private async Task BuildDefaultViewer(string displayName, int initialTutorialState = 1) { Models.Viewer viewer = new Models.Viewer diff --git a/SVSim.Database/Services/Guild/GuildService.cs b/SVSim.Database/Services/Guild/GuildService.cs index 9e8cf542..b0a55191 100644 --- a/SVSim.Database/Services/Guild/GuildService.cs +++ b/SVSim.Database/Services/Guild/GuildService.cs @@ -65,8 +65,11 @@ public sealed class GuildService : IGuildService { var cfg = _config.Get(); var rows = await _guilds.SearchAsync(name ?? "", activity, joinCondition, memberBucket, cfg.MaxMemberNum, cfg.SearchResultCap, ct); - var memberCounts = await _members.CountBatchByGuildIdsAsync(rows.Select(r => r.GuildId).ToList(), ct); - return rows.Select(r => new GuildSearchEntry(r, memberCounts.GetValueOrDefault(r.GuildId, 0))).ToList(); + var guildIds = rows.Select(r => r.GuildId).ToList(); + var memberCounts = await _members.CountBatchByGuildIdsAsync(guildIds, ct); + var leaderIds = rows.Select(r => r.LeaderViewerId).Distinct().ToList(); + var leaderNames = await _viewers.LoadDisplayNamesAsync(leaderIds, ct); + return rows.Select(r => new GuildSearchEntry(r, memberCounts.GetValueOrDefault(r.GuildId, 0), leaderNames.GetValueOrDefault(r.LeaderViewerId, ""))).ToList(); } public async Task CreateAsync(long viewerId, CreateGuildRequest req, CancellationToken ct = default) diff --git a/SVSim.Database/Services/Guild/GuildServiceTypes.cs b/SVSim.Database/Services/Guild/GuildServiceTypes.cs index df0ab382..95957e01 100644 --- a/SVSim.Database/Services/Guild/GuildServiceTypes.cs +++ b/SVSim.Database/Services/Guild/GuildServiceTypes.cs @@ -34,7 +34,7 @@ public sealed record GuildFullView( int JoinRequestCount, int InviteCount); -public sealed record GuildSearchEntry(Entities.Guild.Guild Guild, int MemberNum); +public sealed record GuildSearchEntry(Entities.Guild.Guild Guild, int MemberNum, string LeaderName); public sealed record CreateGuildRequest(string Name, int Activity, int JoinCondition); public sealed record UpdateGuildRequest(int? Activity, int? JoinCondition, string? Name = null); diff --git a/SVSim.EmulatedEntrypoint/Controllers/GuildController.cs b/SVSim.EmulatedEntrypoint/Controllers/GuildController.cs index 0dcb1454..4a4039df 100644 --- a/SVSim.EmulatedEntrypoint/Controllers/GuildController.cs +++ b/SVSim.EmulatedEntrypoint/Controllers/GuildController.cs @@ -135,7 +135,7 @@ public sealed class GuildController : SVSimController { if (!TryGetViewerId(out var v)) return Unauthorized(); var entries = await _guild.SearchAsync(req.GuildName ?? "", req.Activity, req.JoinCondition, req.MemberConditionRange, ct); - return new GuildSearchGuildResponse { List = entries.Select(e => ToDetailDto(e.Guild, e.MemberNum)).ToList() }; + return new GuildSearchGuildResponse { List = entries.Select(e => ToDetailDto(e.Guild, e.MemberNum, e.LeaderName)).ToList() }; } [HttpPost("emblem_list")] diff --git a/SVSim.UnitTests/Integration/Guild/GuildSearchFlowTests.cs b/SVSim.UnitTests/Integration/Guild/GuildSearchFlowTests.cs index 0abf0d01..b6a1fe26 100644 --- a/SVSim.UnitTests/Integration/Guild/GuildSearchFlowTests.cs +++ b/SVSim.UnitTests/Integration/Guild/GuildSearchFlowTests.cs @@ -231,5 +231,8 @@ public class GuildSearchFlowTests Assert.That(entry.TryGetProperty("detail", out _), Is.False, "no 'detail' wrapper on search list entries"); Assert.That(entry.TryGetProperty("guild_name", out var gn), Is.True); Assert.That(gn.GetString(), Is.EqualTo("SmallFreeGuild")); + // leader_name must be populated from the seeded viewer's DisplayName, not empty string + Assert.That(entry.TryGetProperty("leader_name", out var ln), Is.True, "leader_name field must be present"); + Assert.That(ln.GetString(), Is.EqualTo("SearchLeader7"), "leader_name must match the seeded leader's DisplayName"); } }