| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Api.Models; |
| | | 3 | | using Chronicis.Shared.DTOs.Quests; |
| | | 4 | | using Chronicis.Shared.Enums; |
| | | 5 | | using Chronicis.Shared.Models; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | |
| | | 8 | | namespace Chronicis.Api.Services; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Service for QuestUpdate operations with world membership authorization. |
| | | 12 | | /// </summary> |
| | | 13 | | public class QuestUpdateService : IQuestUpdateService |
| | | 14 | | { |
| | | 15 | | private readonly ChronicisDbContext _context; |
| | | 16 | | private readonly ILogger<QuestUpdateService> _logger; |
| | | 17 | | |
| | 17 | 18 | | public QuestUpdateService(ChronicisDbContext context, ILogger<QuestUpdateService> logger) |
| | | 19 | | { |
| | 17 | 20 | | _context = context; |
| | 17 | 21 | | _logger = logger; |
| | 17 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<ServiceResult<PagedResult<QuestUpdateEntryDto>>> GetQuestUpdatesAsync( |
| | | 25 | | Guid questId, |
| | | 26 | | Guid userId, |
| | | 27 | | int skip = 0, |
| | | 28 | | int take = 20) |
| | | 29 | | { |
| | | 30 | | // Validate pagination parameters |
| | 6 | 31 | | if (skip < 0) |
| | | 32 | | { |
| | 1 | 33 | | return ServiceResult<PagedResult<QuestUpdateEntryDto>>.ValidationError("Skip must be non-negative"); |
| | | 34 | | } |
| | | 35 | | |
| | 5 | 36 | | if (take < 1 || take > 100) |
| | | 37 | | { |
| | 1 | 38 | | return ServiceResult<PagedResult<QuestUpdateEntryDto>>.ValidationError("Take must be between 1 and 100"); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | // Find quest and check access |
| | 4 | 42 | | var quest = await _context.Quests |
| | 4 | 43 | | .AsNoTracking() |
| | 4 | 44 | | .Include(q => q.Arc) |
| | 4 | 45 | | .ThenInclude(a => a.Campaign) |
| | 4 | 46 | | .FirstOrDefaultAsync(q => q.Id == questId); |
| | | 47 | | |
| | 4 | 48 | | if (quest == null) |
| | | 49 | | { |
| | 1 | 50 | | return ServiceResult<PagedResult<QuestUpdateEntryDto>>.NotFound("Quest not found"); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // Check world membership |
| | 3 | 54 | | var member = await _context.WorldMembers |
| | 3 | 55 | | .AsNoTracking() |
| | 3 | 56 | | .FirstOrDefaultAsync(m => m.WorldId == quest.Arc.Campaign.WorldId && m.UserId == userId); |
| | | 57 | | |
| | 3 | 58 | | if (member == null) |
| | | 59 | | { |
| | 0 | 60 | | return ServiceResult<PagedResult<QuestUpdateEntryDto>>.NotFound("Quest not found"); |
| | | 61 | | } |
| | | 62 | | |
| | 3 | 63 | | var isGM = member.Role == WorldRole.GM; |
| | | 64 | | |
| | | 65 | | // Non-GM users cannot see GM-only quests |
| | 3 | 66 | | if (quest.IsGmOnly && !isGM) |
| | | 67 | | { |
| | 0 | 68 | | return ServiceResult<PagedResult<QuestUpdateEntryDto>>.NotFound("Quest not found"); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // Get total count |
| | 3 | 72 | | var totalCount = await _context.QuestUpdates |
| | 3 | 73 | | .CountAsync(qu => qu.QuestId == questId); |
| | | 74 | | |
| | | 75 | | // Get paginated updates |
| | 3 | 76 | | var updates = await _context.QuestUpdates |
| | 3 | 77 | | .AsNoTracking() |
| | 3 | 78 | | .Where(qu => qu.QuestId == questId) |
| | 3 | 79 | | .OrderByDescending(qu => qu.CreatedAt) |
| | 3 | 80 | | .Skip(skip) |
| | 3 | 81 | | .Take(take) |
| | 3 | 82 | | .Select(qu => new QuestUpdateEntryDto |
| | 3 | 83 | | { |
| | 3 | 84 | | Id = qu.Id, |
| | 3 | 85 | | QuestId = qu.QuestId, |
| | 3 | 86 | | Body = qu.Body, |
| | 3 | 87 | | SessionId = qu.SessionId, |
| | 3 | 88 | | SessionTitle = qu.Session != null ? qu.Session.Title : null, |
| | 3 | 89 | | CreatedBy = qu.CreatedBy, |
| | 3 | 90 | | CreatedByName = qu.Creator.DisplayName, |
| | 3 | 91 | | CreatedByAvatarUrl = qu.Creator.AvatarUrl, |
| | 3 | 92 | | CreatedAt = qu.CreatedAt |
| | 3 | 93 | | }) |
| | 3 | 94 | | .ToListAsync(); |
| | | 95 | | |
| | 3 | 96 | | var result = new PagedResult<QuestUpdateEntryDto> |
| | 3 | 97 | | { |
| | 3 | 98 | | Items = updates, |
| | 3 | 99 | | TotalCount = totalCount, |
| | 3 | 100 | | Skip = skip, |
| | 3 | 101 | | Take = take |
| | 3 | 102 | | }; |
| | | 103 | | |
| | 3 | 104 | | return ServiceResult<PagedResult<QuestUpdateEntryDto>>.Success(result); |
| | 6 | 105 | | } |
| | | 106 | | |
| | | 107 | | public async Task<ServiceResult<QuestUpdateEntryDto>> CreateQuestUpdateAsync( |
| | | 108 | | Guid questId, |
| | | 109 | | QuestUpdateCreateDto dto, |
| | | 110 | | Guid userId) |
| | | 111 | | { |
| | | 112 | | // Validate input |
| | 7 | 113 | | if (string.IsNullOrWhiteSpace(dto.Body)) |
| | | 114 | | { |
| | 1 | 115 | | return ServiceResult<QuestUpdateEntryDto>.ValidationError("Body is required and cannot be empty"); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | // Find quest and check access |
| | 6 | 119 | | var quest = await _context.Quests |
| | 6 | 120 | | .Include(q => q.Arc) |
| | 6 | 121 | | .ThenInclude(a => a.Campaign) |
| | 6 | 122 | | .FirstOrDefaultAsync(q => q.Id == questId); |
| | | 123 | | |
| | 6 | 124 | | if (quest == null) |
| | | 125 | | { |
| | 0 | 126 | | return ServiceResult<QuestUpdateEntryDto>.NotFound("Quest not found"); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | // Check world membership and role |
| | 6 | 130 | | var member = await _context.WorldMembers |
| | 6 | 131 | | .AsNoTracking() |
| | 6 | 132 | | .FirstOrDefaultAsync(m => m.WorldId == quest.Arc.Campaign.WorldId && m.UserId == userId); |
| | | 133 | | |
| | 6 | 134 | | if (member == null) |
| | | 135 | | { |
| | 0 | 136 | | return ServiceResult<QuestUpdateEntryDto>.NotFound("Quest not found"); |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | // Observer cannot create updates |
| | 6 | 140 | | if (member.Role == WorldRole.Observer) |
| | | 141 | | { |
| | 1 | 142 | | return ServiceResult<QuestUpdateEntryDto>.Forbidden("Observers cannot create quest updates"); |
| | | 143 | | } |
| | | 144 | | |
| | 5 | 145 | | var isGM = member.Role == WorldRole.GM; |
| | | 146 | | |
| | | 147 | | // Non-GM users cannot update GM-only quests |
| | 5 | 148 | | if (quest.IsGmOnly && !isGM) |
| | | 149 | | { |
| | 0 | 150 | | return ServiceResult<QuestUpdateEntryDto>.NotFound("Quest not found"); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | // Validate SessionId if provided |
| | 5 | 154 | | Article? session = null; |
| | 5 | 155 | | if (dto.SessionId.HasValue) |
| | | 156 | | { |
| | 2 | 157 | | session = await _context.Articles |
| | 2 | 158 | | .AsNoTracking() |
| | 2 | 159 | | .FirstOrDefaultAsync(a => a.Id == dto.SessionId.Value); |
| | | 160 | | |
| | 2 | 161 | | if (session == null) |
| | | 162 | | { |
| | 0 | 163 | | return ServiceResult<QuestUpdateEntryDto>.ValidationError("Session not found"); |
| | | 164 | | } |
| | | 165 | | |
| | 2 | 166 | | if (session.Type != ArticleType.Session) |
| | | 167 | | { |
| | 1 | 168 | | return ServiceResult<QuestUpdateEntryDto>.ValidationError("Referenced article is not a Session"); |
| | | 169 | | } |
| | | 170 | | |
| | 1 | 171 | | if (session.ArcId != quest.ArcId) |
| | | 172 | | { |
| | 0 | 173 | | return ServiceResult<QuestUpdateEntryDto>.ValidationError("Session must belong to the same Arc as the qu |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | |
| | 4 | 177 | | var now = DateTime.UtcNow; |
| | | 178 | | |
| | 4 | 179 | | var questUpdate = new QuestUpdate |
| | 4 | 180 | | { |
| | 4 | 181 | | Id = Guid.NewGuid(), |
| | 4 | 182 | | QuestId = questId, |
| | 4 | 183 | | SessionId = dto.SessionId, |
| | 4 | 184 | | Body = dto.Body.Trim(), |
| | 4 | 185 | | CreatedBy = userId, |
| | 4 | 186 | | CreatedAt = now |
| | 4 | 187 | | }; |
| | | 188 | | |
| | 4 | 189 | | _context.QuestUpdates.Add(questUpdate); |
| | | 190 | | |
| | | 191 | | // Update parent Quest's UpdatedAt timestamp |
| | 4 | 192 | | quest.UpdatedAt = now; |
| | | 193 | | |
| | 4 | 194 | | await _context.SaveChangesAsync(); |
| | | 195 | | |
| | 4 | 196 | | _logger.LogDebug("Created quest update for quest {QuestId} by user {UserId}", questId, userId); |
| | | 197 | | |
| | | 198 | | // Fetch creator details for DTO |
| | 4 | 199 | | var creator = await _context.Users.FindAsync(userId); |
| | | 200 | | |
| | 4 | 201 | | var resultDto = new QuestUpdateEntryDto |
| | 4 | 202 | | { |
| | 4 | 203 | | Id = questUpdate.Id, |
| | 4 | 204 | | QuestId = questUpdate.QuestId, |
| | 4 | 205 | | Body = questUpdate.Body, |
| | 4 | 206 | | SessionId = questUpdate.SessionId, |
| | 4 | 207 | | SessionTitle = session?.Title, |
| | 4 | 208 | | CreatedBy = questUpdate.CreatedBy, |
| | 4 | 209 | | CreatedByName = creator?.DisplayName ?? "Unknown", |
| | 4 | 210 | | CreatedByAvatarUrl = creator?.AvatarUrl, |
| | 4 | 211 | | CreatedAt = questUpdate.CreatedAt |
| | 4 | 212 | | }; |
| | | 213 | | |
| | 4 | 214 | | return ServiceResult<QuestUpdateEntryDto>.Success(resultDto); |
| | 7 | 215 | | } |
| | | 216 | | |
| | | 217 | | public async Task<ServiceResult<bool>> DeleteQuestUpdateAsync( |
| | | 218 | | Guid questId, |
| | | 219 | | Guid updateId, |
| | | 220 | | Guid userId) |
| | | 221 | | { |
| | | 222 | | // Find the quest update |
| | 4 | 223 | | var questUpdate = await _context.QuestUpdates |
| | 4 | 224 | | .Include(qu => qu.Quest) |
| | 4 | 225 | | .ThenInclude(q => q.Arc) |
| | 4 | 226 | | .ThenInclude(a => a.Campaign) |
| | 4 | 227 | | .FirstOrDefaultAsync(qu => qu.Id == updateId && qu.QuestId == questId); |
| | | 228 | | |
| | 4 | 229 | | if (questUpdate == null) |
| | | 230 | | { |
| | 1 | 231 | | return ServiceResult<bool>.NotFound("Quest update not found"); |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | // Check world membership and role |
| | 3 | 235 | | var member = await _context.WorldMembers |
| | 3 | 236 | | .AsNoTracking() |
| | 3 | 237 | | .FirstOrDefaultAsync(m => m.WorldId == questUpdate.Quest.Arc.Campaign.WorldId && m.UserId == userId); |
| | | 238 | | |
| | 3 | 239 | | if (member == null) |
| | | 240 | | { |
| | 0 | 241 | | return ServiceResult<bool>.NotFound("Quest update not found"); |
| | | 242 | | } |
| | | 243 | | |
| | 3 | 244 | | var isGM = member.Role == WorldRole.GM; |
| | | 245 | | |
| | | 246 | | // Determine if user can delete this update |
| | 3 | 247 | | bool canDelete = isGM || questUpdate.CreatedBy == userId; |
| | | 248 | | |
| | 3 | 249 | | if (!canDelete) |
| | | 250 | | { |
| | 1 | 251 | | return ServiceResult<bool>.Forbidden("You can only delete your own quest updates"); |
| | | 252 | | } |
| | | 253 | | |
| | 2 | 254 | | _context.QuestUpdates.Remove(questUpdate); |
| | 2 | 255 | | await _context.SaveChangesAsync(); |
| | | 256 | | |
| | 2 | 257 | | _logger.LogDebug("Deleted quest update {UpdateId} from quest {QuestId} by user {UserId}", |
| | 2 | 258 | | updateId, questId, userId); |
| | | 259 | | |
| | 2 | 260 | | return ServiceResult<bool>.Success(true); |
| | 4 | 261 | | } |
| | | 262 | | } |