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:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user