| | | 1 | | using Chronicis.Api.Infrastructure; |
| | | 2 | | using Chronicis.Api.Models; |
| | | 3 | | using Chronicis.Api.Services; |
| | | 4 | | using Chronicis.Shared.DTOs.Quests; |
| | | 5 | | using Microsoft.AspNetCore.Authorization; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | |
| | | 8 | | namespace Chronicis.Api.Controllers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// API endpoints for QuestUpdate management. |
| | | 12 | | /// </summary> |
| | | 13 | | [ApiController] |
| | | 14 | | [Authorize] |
| | | 15 | | public class QuestUpdatesController : ControllerBase |
| | | 16 | | { |
| | | 17 | | private readonly IQuestUpdateService _questUpdateService; |
| | | 18 | | private readonly ICurrentUserService _currentUserService; |
| | | 19 | | private readonly ILogger<QuestUpdatesController> _logger; |
| | | 20 | | |
| | 0 | 21 | | public QuestUpdatesController( |
| | 0 | 22 | | IQuestUpdateService questUpdateService, |
| | 0 | 23 | | ICurrentUserService currentUserService, |
| | 0 | 24 | | ILogger<QuestUpdatesController> logger) |
| | | 25 | | { |
| | 0 | 26 | | _questUpdateService = questUpdateService; |
| | 0 | 27 | | _currentUserService = currentUserService; |
| | 0 | 28 | | _logger = logger; |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// GET /quests/{questId}/updates - Get paginated quest updates. |
| | | 33 | | /// </summary> |
| | | 34 | | [HttpGet("quests/{questId:guid}/updates")] |
| | | 35 | | public async Task<ActionResult<PagedResult<QuestUpdateEntryDto>>> GetQuestUpdates( |
| | | 36 | | Guid questId, |
| | | 37 | | [FromQuery] int skip = 0, |
| | | 38 | | [FromQuery] int take = 20) |
| | | 39 | | { |
| | 0 | 40 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | 0 | 41 | | _logger.LogDebug("Getting quest updates for quest {QuestId} (skip: {Skip}, take: {Take}) for user {UserId}", |
| | 0 | 42 | | questId, skip, take, user.Id); |
| | | 43 | | |
| | 0 | 44 | | var result = await _questUpdateService.GetQuestUpdatesAsync(questId, user.Id, skip, take); |
| | | 45 | | |
| | 0 | 46 | | return result.Status switch |
| | 0 | 47 | | { |
| | 0 | 48 | | ServiceStatus.Success => Ok(result.Value), |
| | 0 | 49 | | ServiceStatus.NotFound => NotFound(new { error = result.ErrorMessage }), |
| | 0 | 50 | | ServiceStatus.Forbidden => StatusCode(403, new { error = result.ErrorMessage }), |
| | 0 | 51 | | ServiceStatus.ValidationError => BadRequest(new { error = result.ErrorMessage }), |
| | 0 | 52 | | _ => StatusCode(500, new { error = "An unexpected error occurred" }) |
| | 0 | 53 | | }; |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// POST /quests/{questId}/updates - Create a new quest update (GM or Player). |
| | | 58 | | /// </summary> |
| | | 59 | | [HttpPost("quests/{questId:guid}/updates")] |
| | | 60 | | public async Task<ActionResult<QuestUpdateEntryDto>> CreateQuestUpdate( |
| | | 61 | | Guid questId, |
| | | 62 | | [FromBody] QuestUpdateCreateDto dto) |
| | | 63 | | { |
| | 0 | 64 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | | 65 | | |
| | 0 | 66 | | if (dto == null) |
| | | 67 | | { |
| | 0 | 68 | | return BadRequest(new { error = "Request body is required" }); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | _logger.LogDebug("Creating quest update for quest {QuestId} by user {UserId}", questId, user.Id); |
| | | 72 | | |
| | 0 | 73 | | var result = await _questUpdateService.CreateQuestUpdateAsync(questId, dto, user.Id); |
| | | 74 | | |
| | 0 | 75 | | return result.Status switch |
| | 0 | 76 | | { |
| | 0 | 77 | | ServiceStatus.Success => CreatedAtAction( |
| | 0 | 78 | | nameof(GetQuestUpdates), |
| | 0 | 79 | | new { questId = questId, skip = 0, take = 20 }, |
| | 0 | 80 | | result.Value), |
| | 0 | 81 | | ServiceStatus.NotFound => NotFound(new { error = result.ErrorMessage }), |
| | 0 | 82 | | ServiceStatus.Forbidden => StatusCode(403, new { error = result.ErrorMessage }), |
| | 0 | 83 | | ServiceStatus.ValidationError => BadRequest(new { error = result.ErrorMessage }), |
| | 0 | 84 | | _ => StatusCode(500, new { error = "An unexpected error occurred" }) |
| | 0 | 85 | | }; |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// DELETE /quests/{questId}/updates/{updateId} - Delete a quest update. |
| | | 90 | | /// GM can delete any, Player can delete own only. |
| | | 91 | | /// </summary> |
| | | 92 | | [HttpDelete("quests/{questId:guid}/updates/{updateId:guid}")] |
| | | 93 | | public async Task<IActionResult> DeleteQuestUpdate(Guid questId, Guid updateId) |
| | | 94 | | { |
| | 0 | 95 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | 0 | 96 | | _logger.LogDebug("Deleting quest update {UpdateId} from quest {QuestId} for user {UserId}", |
| | 0 | 97 | | updateId, questId, user.Id); |
| | | 98 | | |
| | 0 | 99 | | var result = await _questUpdateService.DeleteQuestUpdateAsync(questId, updateId, user.Id); |
| | | 100 | | |
| | 0 | 101 | | return result.Status switch |
| | 0 | 102 | | { |
| | 0 | 103 | | ServiceStatus.Success => NoContent(), |
| | 0 | 104 | | ServiceStatus.NotFound => NotFound(new { error = result.ErrorMessage }), |
| | 0 | 105 | | ServiceStatus.Forbidden => StatusCode(403, new { error = result.ErrorMessage }), |
| | 0 | 106 | | _ => StatusCode(500, new { error = "An unexpected error occurred" }) |
| | 0 | 107 | | }; |
| | 0 | 108 | | } |
| | | 109 | | } |