fix(guild): final-review remediation — replay_detail flat, FK on leader, transaction wraps, _db extraction
C1: replay_detail — flatten stored payload to data level. ChatReplayDetailTask.Parse() calls new ReplayDetailInfo(data) which unguardedly accesses data[battleId], data[seed], data[vid1], etc. Wrapping under replay_info key crashes the client. Controller now returns Ok(JsonElement) directly so stored battle fields are at data root. Wire-shape test added. C2: Guild.LeaderViewerId long to long?; add HasOne<Viewer> FK with OnDelete=SetNull; migration AddGuildLeaderViewerIdFk; all consumers null-guarded with ?? 0L. C3: BreakupAsync — wrap 6 destructive ops in IDbContextTransaction with InMemory fallback. C4: CommitJoinAsync — wrap member-add + viewer-guildId-set + invite/request cleanup in IDbContextTransaction with InMemory fallback. Chat event emitted after commit. I1: GuildController — remove SVSimDbContext field; inject IViewerRepository + IGuildMemberRepository. Add IGuildMemberRepository.GetViewerIdsInAGuildAsync (batch guild-membership check). All _db.Viewers / _db.GuildMembers queries replaced with repo calls. I2: GuildService — extract 3 _db.Viewers queries: GetEquippedEmblemIdAsync (CreateAsync), LoadGuildProfileBatchAsync (ListOutgoingInvitesAsync + ListPendingJoinRequestsForMyGuildAsync). Add GuildMemberProfile record with IsOfficialMarkDisplayed. GetEmblemListAsync for EmblemList. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -70,4 +70,15 @@ public sealed class GuildMemberRepository : IGuildMemberRepository
|
||||
result[row.GuildId] = row.Count;
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<HashSet<long>> GetViewerIdsInAGuildAsync(
|
||||
IReadOnlyList<long> viewerIds, CancellationToken ct = default)
|
||||
{
|
||||
if (viewerIds.Count == 0) return new HashSet<long>();
|
||||
var ids = await _db.GuildMembers
|
||||
.Where(m => viewerIds.Contains(m.ViewerId))
|
||||
.Select(m => m.ViewerId)
|
||||
.ToListAsync(ct);
|
||||
return new HashSet<long>(ids);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,4 +15,11 @@ public interface IGuildMemberRepository
|
||||
|
||||
/// <summary>Returns a count of members per guild for the given guild ids. Used by Task 9 search.</summary>
|
||||
Task<Dictionary<int, int>> CountBatchByGuildIdsAsync(IReadOnlyList<int> guildIds, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Batch-checks whether each viewer id in <paramref name="viewerIds"/> is currently in any guild.
|
||||
/// Returns a set of viewer ids that ARE in a guild. Used by <c>/guild/friend_list</c> to set
|
||||
/// <c>is_join_guild</c>.
|
||||
/// </summary>
|
||||
Task<HashSet<long>> GetViewerIdsInAGuildAsync(IReadOnlyList<long> viewerIds, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,18 @@ public record ChatUserProfile(
|
||||
int Rank,
|
||||
int DegreeId);
|
||||
|
||||
/// <summary>
|
||||
/// Richer profile shape used by guild management surfaces (invite list, join-request list).
|
||||
/// Extends <see cref="ChatUserProfile"/> with <c>IsOfficialMarkDisplayed</c>.
|
||||
/// </summary>
|
||||
public record GuildMemberProfile(
|
||||
string Name,
|
||||
long EmblemId,
|
||||
string CountryCode,
|
||||
int Rank,
|
||||
int DegreeId,
|
||||
bool IsOfficialMarkDisplayed);
|
||||
|
||||
public interface IViewerRepository
|
||||
{
|
||||
Task<Models.Viewer?> GetViewerBySocialConnection(SocialAccountType accountType, ulong socialId);
|
||||
@@ -57,4 +69,23 @@ public interface IViewerRepository
|
||||
/// in the controller. Ids with no matching row are absent from the result. Read-only (AsNoTracking).
|
||||
/// </summary>
|
||||
Task<IReadOnlyDictionary<long, ChatUserProfile>> LoadChatProfilesAsync(IReadOnlyCollection<long> viewerIds, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Batch-loads the <see cref="GuildMemberProfile"/> fields (emblem, degree,
|
||||
/// <c>IsOfficialMarkDisplayed</c>) for a set of viewer ids. Used by guild management surfaces
|
||||
/// (invite_user_list, join_request_list). Ids with no matching row are absent. Read-only.
|
||||
/// </summary>
|
||||
Task<IReadOnlyDictionary<long, GuildMemberProfile>> LoadGuildProfileBatchAsync(IReadOnlyCollection<long> viewerIds, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Loads a viewer's currently-equipped emblem id, or <c>100_000_000</c> (default) if none.
|
||||
/// Used by <see cref="SVSim.Database.Services.Guild.GuildService"/> to seed the guild's initial emblem.
|
||||
/// Returns <c>100_000_000</c> if the viewer doesn't exist.
|
||||
/// </summary>
|
||||
Task<long> GetEquippedEmblemIdAsync(long viewerId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Batch-loads owned guild-emblem ids for a viewer. Used by <c>/guild/emblem_list</c>.
|
||||
/// </summary>
|
||||
Task<List<long>> GetEmblemListAsync(long viewerId, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
@@ -323,6 +323,56 @@ public class ViewerRepository : IViewerRepository
|
||||
DegreeId: r.DegreeId));
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyDictionary<long, GuildMemberProfile>> LoadGuildProfileBatchAsync(IReadOnlyCollection<long> viewerIds, CancellationToken ct = default)
|
||||
{
|
||||
if (viewerIds.Count == 0) return new Dictionary<long, GuildMemberProfile>();
|
||||
var rows = await _dbContext.Set<Models.Viewer>()
|
||||
.AsNoTracking()
|
||||
.Include(v => v.Info.SelectedEmblem)
|
||||
.Include(v => v.Info.SelectedDegree)
|
||||
.Where(v => viewerIds.Contains(v.Id))
|
||||
.Select(v => new
|
||||
{
|
||||
v.Id,
|
||||
v.DisplayName,
|
||||
EmblemId = v.Info.SelectedEmblem != null ? v.Info.SelectedEmblem.Id : 100_000_000L,
|
||||
CountryCode = v.Info.CountryCode ?? "",
|
||||
DegreeId = v.Info.SelectedDegree != null ? (int)v.Info.SelectedDegree.Id : 0,
|
||||
IsOfficialMarkDisplayed = v.Info.IsOfficialMarkDisplayed,
|
||||
})
|
||||
.ToListAsync(ct);
|
||||
return rows.ToDictionary(
|
||||
r => r.Id,
|
||||
r => new GuildMemberProfile(
|
||||
Name: r.DisplayName,
|
||||
EmblemId: r.EmblemId,
|
||||
CountryCode: r.CountryCode,
|
||||
Rank: 1, // TODO: real rank when rank tracking lands
|
||||
DegreeId: r.DegreeId,
|
||||
IsOfficialMarkDisplayed: r.IsOfficialMarkDisplayed));
|
||||
}
|
||||
|
||||
public async Task<long> GetEquippedEmblemIdAsync(long viewerId, CancellationToken ct = default)
|
||||
{
|
||||
var row = await _dbContext.Set<Models.Viewer>()
|
||||
.AsNoTracking()
|
||||
.Include(v => v.Info.SelectedEmblem)
|
||||
.Where(v => v.Id == viewerId)
|
||||
.Select(v => new { EmblemId = v.Info.SelectedEmblem != null ? v.Info.SelectedEmblem.Id : 100_000_000L })
|
||||
.FirstOrDefaultAsync(ct);
|
||||
return row?.EmblemId ?? 100_000_000L;
|
||||
}
|
||||
|
||||
public async Task<List<long>> GetEmblemListAsync(long viewerId, CancellationToken ct = default)
|
||||
{
|
||||
var viewer = await _dbContext.Set<Models.Viewer>()
|
||||
.AsNoTracking()
|
||||
.Include(v => v.Emblems)
|
||||
.Where(v => v.Id == viewerId)
|
||||
.FirstOrDefaultAsync(ct);
|
||||
return viewer?.Emblems.Select(e => (long)e.Id).ToList() ?? new List<long>();
|
||||
}
|
||||
|
||||
private async Task<Models.Viewer> BuildDefaultViewer(string displayName, int initialTutorialState = 1)
|
||||
{
|
||||
Models.Viewer viewer = new Models.Viewer
|
||||
|
||||
Reference in New Issue
Block a user