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>
85 lines
3.3 KiB
C#
85 lines
3.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using SVSim.Database.Entities.Guild;
|
|
|
|
namespace SVSim.Database.Repositories.Guild;
|
|
|
|
public sealed class GuildMemberRepository : IGuildMemberRepository
|
|
{
|
|
private readonly SVSimDbContext _db;
|
|
|
|
public GuildMemberRepository(SVSimDbContext db) { _db = db; }
|
|
|
|
/// <summary>
|
|
/// The unique index on ViewerId guarantees at most one membership row per viewer.
|
|
/// </summary>
|
|
public Task<GuildMember?> GetMembershipAsync(long viewerId, CancellationToken ct = default)
|
|
=> _db.GuildMembers.FirstOrDefaultAsync(m => m.ViewerId == viewerId, ct);
|
|
|
|
public async Task<IReadOnlyList<GuildMember>> ListByGuildAsync(int guildId, CancellationToken ct = default)
|
|
=> await _db.GuildMembers.Where(m => m.GuildId == guildId).ToListAsync(ct);
|
|
|
|
public Task<int> CountByGuildAsync(int guildId, CancellationToken ct = default)
|
|
=> _db.GuildMembers.CountAsync(m => m.GuildId == guildId, ct);
|
|
|
|
public Task<int> CountByGuildAndRoleAsync(int guildId, GuildRole role, CancellationToken ct = default)
|
|
=> _db.GuildMembers.CountAsync(m => m.GuildId == guildId && m.Role == role, ct);
|
|
|
|
public async Task AddAsync(GuildMember m, CancellationToken ct = default)
|
|
{
|
|
_db.GuildMembers.Add(m);
|
|
await _db.SaveChangesAsync(ct);
|
|
}
|
|
|
|
public async Task UpdateRoleAsync(int guildId, long viewerId, GuildRole role, CancellationToken ct = default)
|
|
{
|
|
var member = await _db.GuildMembers
|
|
.FirstOrDefaultAsync(m => m.GuildId == guildId && m.ViewerId == viewerId, ct);
|
|
if (member is null) return;
|
|
member.Role = role;
|
|
await _db.SaveChangesAsync(ct);
|
|
}
|
|
|
|
public async Task RemoveAsync(int guildId, long viewerId, CancellationToken ct = default)
|
|
{
|
|
var member = await _db.GuildMembers
|
|
.FirstOrDefaultAsync(m => m.GuildId == guildId && m.ViewerId == viewerId, ct);
|
|
if (member is null) return;
|
|
_db.GuildMembers.Remove(member);
|
|
await _db.SaveChangesAsync(ct);
|
|
}
|
|
|
|
public async Task DeleteAllForGuildAsync(int guildId, CancellationToken ct = default)
|
|
{
|
|
var members = await _db.GuildMembers.Where(m => m.GuildId == guildId).ToListAsync(ct);
|
|
_db.GuildMembers.RemoveRange(members);
|
|
await _db.SaveChangesAsync(ct);
|
|
}
|
|
|
|
public async Task<Dictionary<int, int>> CountBatchByGuildIdsAsync(
|
|
IReadOnlyList<int> guildIds, CancellationToken ct = default)
|
|
{
|
|
var counts = await _db.GuildMembers
|
|
.Where(m => guildIds.Contains(m.GuildId))
|
|
.GroupBy(m => m.GuildId)
|
|
.Select(g => new { GuildId = g.Key, Count = g.Count() })
|
|
.ToListAsync(ct);
|
|
|
|
// Seed all requested guild ids so callers don't need a null-check for guilds with 0 members.
|
|
var result = guildIds.ToDictionary(id => id, _ => 0);
|
|
foreach (var row in counts)
|
|
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);
|
|
}
|
|
}
|