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 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-27 13:08:11 -04:00
parent 3f8f04eb1a
commit c103ab6a87
6 changed files with 26 additions and 4 deletions

View File

@@ -33,4 +33,10 @@ public interface IViewerRepository
/// <summary>Clears Viewer.GuildId to null. No-op if the viewer does not exist.</summary>
Task ClearGuildIdAsync(long viewerId, CancellationToken ct = default);
/// <summary>
/// Batch-loads <c>DisplayName</c> 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).
/// </summary>
Task<Dictionary<long, string>> LoadDisplayNamesAsync(IReadOnlyCollection<long> viewerIds, CancellationToken ct = default);
}

View File

@@ -286,6 +286,16 @@ public class ViewerRepository : IViewerRepository
await _dbContext.SaveChangesAsync(ct);
}
public async Task<Dictionary<long, string>> LoadDisplayNamesAsync(IReadOnlyCollection<long> viewerIds, CancellationToken ct = default)
{
if (viewerIds.Count == 0) return new Dictionary<long, string>();
return await _dbContext.Set<Models.Viewer>()
.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<Models.Viewer> BuildDefaultViewer(string displayName, int initialTutorialState = 1)
{
Models.Viewer viewer = new Models.Viewer

View File

@@ -65,8 +65,11 @@ public sealed class GuildService : IGuildService
{
var cfg = _config.Get<GuildConfig>();
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<GuildOpResult> CreateAsync(long viewerId, CreateGuildRequest req, CancellationToken ct = default)

View File

@@ -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);