| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Api.Infrastructure; |
| | | 3 | | using Chronicis.Shared.DTOs; |
| | | 4 | | using Chronicis.Shared.Enums; |
| | | 5 | | using Microsoft.AspNetCore.Authorization; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | using Microsoft.EntityFrameworkCore; |
| | | 8 | | |
| | | 9 | | namespace Chronicis.Api.Controllers; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// API endpoints for Character claiming operations. |
| | | 13 | | /// </summary> |
| | | 14 | | [ApiController] |
| | | 15 | | [Route("characters")] |
| | | 16 | | [Authorize] |
| | | 17 | | public class CharactersController : ControllerBase |
| | | 18 | | { |
| | | 19 | | private readonly ChronicisDbContext _context; |
| | | 20 | | private readonly ICurrentUserService _currentUserService; |
| | | 21 | | private readonly ILogger<CharactersController> _logger; |
| | | 22 | | |
| | 0 | 23 | | public CharactersController( |
| | 0 | 24 | | ChronicisDbContext context, |
| | 0 | 25 | | ICurrentUserService currentUserService, |
| | 0 | 26 | | ILogger<CharactersController> logger) |
| | | 27 | | { |
| | 0 | 28 | | _context = context; |
| | 0 | 29 | | _currentUserService = currentUserService; |
| | 0 | 30 | | _logger = logger; |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// GET /api/characters/claimed - Get all characters claimed by the current user. |
| | | 35 | | /// </summary> |
| | | 36 | | [HttpGet("claimed")] |
| | | 37 | | public async Task<ActionResult<List<ClaimedCharacterDto>>> GetClaimedCharacters() |
| | | 38 | | { |
| | 0 | 39 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | 0 | 40 | | _logger.LogDebug("Getting claimed characters for user {UserId}", user.Id); |
| | | 41 | | |
| | 0 | 42 | | var claimedCharacters = await _context.Articles |
| | 0 | 43 | | .Where(a => a.PlayerId == user.Id && a.Type == ArticleType.Character) |
| | 0 | 44 | | .Where(a => a.WorldId.HasValue) |
| | 0 | 45 | | .Select(a => new ClaimedCharacterDto |
| | 0 | 46 | | { |
| | 0 | 47 | | Id = a.Id, |
| | 0 | 48 | | Title = a.Title ?? "Unnamed Character", |
| | 0 | 49 | | IconEmoji = a.IconEmoji, |
| | 0 | 50 | | WorldId = a.WorldId!.Value, |
| | 0 | 51 | | WorldName = a.World != null ? a.World.Name : "Unknown World", |
| | 0 | 52 | | ModifiedAt = a.ModifiedAt, |
| | 0 | 53 | | CreatedAt = a.CreatedAt |
| | 0 | 54 | | }) |
| | 0 | 55 | | .ToListAsync(); |
| | | 56 | | |
| | 0 | 57 | | return Ok(claimedCharacters); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// GET /api/characters/{id}/claim - Get the claim status of a character. |
| | | 62 | | /// </summary> |
| | | 63 | | [HttpGet("{id:guid}/claim")] |
| | | 64 | | public async Task<ActionResult<CharacterClaimStatusDto>> GetClaimStatus(Guid id) |
| | | 65 | | { |
| | 0 | 66 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | 0 | 67 | | _logger.LogDebug("Getting claim status for character {CharacterId}", id); |
| | | 68 | | |
| | 0 | 69 | | var article = await _context.Articles |
| | 0 | 70 | | .Where(a => a.Id == id && a.Type == ArticleType.Character) |
| | 0 | 71 | | .Select(a => new |
| | 0 | 72 | | { |
| | 0 | 73 | | a.Id, |
| | 0 | 74 | | a.PlayerId, |
| | 0 | 75 | | PlayerName = a.Player != null ? a.Player.DisplayName : null |
| | 0 | 76 | | }) |
| | 0 | 77 | | .FirstOrDefaultAsync(); |
| | | 78 | | |
| | 0 | 79 | | if (article == null) |
| | | 80 | | { |
| | 0 | 81 | | return NotFound(new { error = "Character not found" }); |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | var status = new CharacterClaimStatusDto |
| | 0 | 85 | | { |
| | 0 | 86 | | CharacterId = article.Id, |
| | 0 | 87 | | IsClaimed = article.PlayerId.HasValue, |
| | 0 | 88 | | IsClaimedByMe = article.PlayerId == user.Id, |
| | 0 | 89 | | ClaimedByName = article.PlayerName |
| | 0 | 90 | | }; |
| | | 91 | | |
| | 0 | 92 | | return Ok(status); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// POST /api/characters/{id}/claim - Claim a character for the current user. |
| | | 97 | | /// </summary> |
| | | 98 | | [HttpPost("{id:guid}/claim")] |
| | | 99 | | public async Task<IActionResult> ClaimCharacter(Guid id) |
| | | 100 | | { |
| | 0 | 101 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | 0 | 102 | | _logger.LogDebug("User {UserId} claiming character {CharacterId}", user.Id, id); |
| | | 103 | | |
| | | 104 | | // Get the character article and verify user has access to its world |
| | 0 | 105 | | var article = await _context.Articles |
| | 0 | 106 | | .Where(a => a.Id == id && a.Type == ArticleType.Character) |
| | 0 | 107 | | .Where(a => a.WorldId.HasValue && a.World!.Members.Any(m => m.UserId == user.Id)) |
| | 0 | 108 | | .FirstOrDefaultAsync(); |
| | | 109 | | |
| | 0 | 110 | | if (article == null) |
| | | 111 | | { |
| | 0 | 112 | | return NotFound(new { error = "Character not found or access denied" }); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | // Check if already claimed by someone else |
| | 0 | 116 | | if (article.PlayerId.HasValue && article.PlayerId != user.Id) |
| | | 117 | | { |
| | 0 | 118 | | return Conflict(new { error = "Character is already claimed by another player" }); |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | // Claim the character |
| | 0 | 122 | | article.PlayerId = user.Id; |
| | 0 | 123 | | article.ModifiedAt = DateTime.UtcNow; |
| | 0 | 124 | | await _context.SaveChangesAsync(); |
| | | 125 | | |
| | 0 | 126 | | _logger.LogDebug("Character {CharacterId} claimed by user {UserId}", id, user.Id); |
| | 0 | 127 | | return NoContent(); |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// DELETE /api/characters/{id}/claim - Unclaim a character (remove current user's claim). |
| | | 132 | | /// </summary> |
| | | 133 | | [HttpDelete("{id:guid}/claim")] |
| | | 134 | | public async Task<IActionResult> UnclaimCharacter(Guid id) |
| | | 135 | | { |
| | 0 | 136 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | 0 | 137 | | _logger.LogDebug("User {UserId} unclaiming character {CharacterId}", user.Id, id); |
| | | 138 | | |
| | | 139 | | // Get the character article |
| | 0 | 140 | | var article = await _context.Articles |
| | 0 | 141 | | .Where(a => a.Id == id && a.Type == ArticleType.Character) |
| | 0 | 142 | | .Where(a => a.PlayerId == user.Id) // Only allow unclaiming own characters |
| | 0 | 143 | | .FirstOrDefaultAsync(); |
| | | 144 | | |
| | 0 | 145 | | if (article == null) |
| | | 146 | | { |
| | 0 | 147 | | return NotFound(new { error = "Character not found or not claimed by you" }); |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | // Unclaim the character |
| | 0 | 151 | | article.PlayerId = null; |
| | 0 | 152 | | article.ModifiedAt = DateTime.UtcNow; |
| | 0 | 153 | | await _context.SaveChangesAsync(); |
| | | 154 | | |
| | 0 | 155 | | _logger.LogDebug("Character {CharacterId} unclaimed by user {UserId}", id, user.Id); |
| | 0 | 156 | | return NoContent(); |
| | 0 | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Status of a character's claim. |
| | | 162 | | /// </summary> |
| | | 163 | | public class CharacterClaimStatusDto |
| | | 164 | | { |
| | | 165 | | public Guid CharacterId { get; set; } |
| | | 166 | | public bool IsClaimed { get; set; } |
| | | 167 | | public bool IsClaimedByMe { get; set; } |
| | | 168 | | public string? ClaimedByName { get; set; } |
| | | 169 | | } |