| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Api.Infrastructure; |
| | | 3 | | using Chronicis.Shared.DTOs; |
| | | 4 | | using Chronicis.Shared.Extensions; |
| | | 5 | | using Chronicis.Shared.Models; |
| | | 6 | | using Microsoft.AspNetCore.Authorization; |
| | | 7 | | using Microsoft.AspNetCore.Mvc; |
| | | 8 | | using Microsoft.EntityFrameworkCore; |
| | | 9 | | |
| | | 10 | | namespace Chronicis.Api.Controllers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// API endpoints for World Link management (external resource links). |
| | | 14 | | /// </summary> |
| | | 15 | | [ApiController] |
| | | 16 | | [Route("worlds/{worldId:guid}/links")] |
| | | 17 | | [Authorize] |
| | | 18 | | public class WorldLinksController : ControllerBase |
| | | 19 | | { |
| | | 20 | | private readonly ChronicisDbContext _db; |
| | | 21 | | private readonly ICurrentUserService _currentUserService; |
| | | 22 | | private readonly ILogger<WorldLinksController> _logger; |
| | | 23 | | |
| | 0 | 24 | | public WorldLinksController( |
| | 0 | 25 | | ChronicisDbContext db, |
| | 0 | 26 | | ICurrentUserService currentUserService, |
| | 0 | 27 | | ILogger<WorldLinksController> logger) |
| | | 28 | | { |
| | 0 | 29 | | _db = db; |
| | 0 | 30 | | _currentUserService = currentUserService; |
| | 0 | 31 | | _logger = logger; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// GET /worlds/{worldId}/links - Get all links for a world. |
| | | 36 | | /// </summary> |
| | | 37 | | [HttpGet] |
| | | 38 | | public async Task<ActionResult<IEnumerable<WorldLinkDto>>> GetWorldLinks(Guid worldId) |
| | | 39 | | { |
| | 0 | 40 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | 0 | 41 | | _logger.LogDebug("Getting links for world {WorldId} by user {UserId}", worldId, user.Id); |
| | | 42 | | |
| | | 43 | | // Verify user has access to the world |
| | 0 | 44 | | var world = await _db.Worlds |
| | 0 | 45 | | .AsNoTracking() |
| | 0 | 46 | | .FirstOrDefaultAsync(w => w.Id == worldId && w.OwnerId == user.Id); |
| | | 47 | | |
| | 0 | 48 | | if (world == null) |
| | | 49 | | { |
| | 0 | 50 | | return NotFound(new { error = "World not found or access denied" }); |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | var links = await _db.WorldLinks |
| | 0 | 54 | | .AsNoTracking() |
| | 0 | 55 | | .Where(wl => wl.WorldId == worldId) |
| | 0 | 56 | | .OrderBy(wl => wl.Title) |
| | 0 | 57 | | .Select(wl => new WorldLinkDto |
| | 0 | 58 | | { |
| | 0 | 59 | | Id = wl.Id, |
| | 0 | 60 | | WorldId = wl.WorldId, |
| | 0 | 61 | | Url = wl.Url, |
| | 0 | 62 | | Title = wl.Title, |
| | 0 | 63 | | Description = wl.Description, |
| | 0 | 64 | | CreatedAt = wl.CreatedAt |
| | 0 | 65 | | }) |
| | 0 | 66 | | .ToListAsync(); |
| | | 67 | | |
| | 0 | 68 | | return Ok(links); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// POST /worlds/{worldId}/links - Create a new link for a world. |
| | | 73 | | /// </summary> |
| | | 74 | | [HttpPost] |
| | | 75 | | public async Task<ActionResult<WorldLinkDto>> CreateWorldLink(Guid worldId, [FromBody] WorldLinkCreateDto dto) |
| | | 76 | | { |
| | 0 | 77 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | | 78 | | |
| | | 79 | | // Verify user owns the world |
| | 0 | 80 | | var world = await _db.Worlds |
| | 0 | 81 | | .FirstOrDefaultAsync(w => w.Id == worldId && w.OwnerId == user.Id); |
| | | 82 | | |
| | 0 | 83 | | if (world == null) |
| | | 84 | | { |
| | 0 | 85 | | return NotFound(new { error = "World not found or access denied" }); |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | if (dto == null || string.IsNullOrWhiteSpace(dto.Url) || string.IsNullOrWhiteSpace(dto.Title)) |
| | | 89 | | { |
| | 0 | 90 | | return BadRequest(new { error = "URL and Title are required" }); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | // Validate URL format |
| | 0 | 94 | | if (!Uri.TryCreate(dto.Url, UriKind.Absolute, out var uri) || |
| | 0 | 95 | | (uri.Scheme != "http" && uri.Scheme != "https")) |
| | | 96 | | { |
| | 0 | 97 | | return BadRequest(new { error = "Invalid URL format. Must be a valid http or https URL." }); |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | _logger.LogDebugSanitized("Creating link '{Title}' for world {WorldId} by user {UserId}", |
| | 0 | 101 | | dto.Title, worldId, user.Id); |
| | | 102 | | |
| | 0 | 103 | | var link = new WorldLink |
| | 0 | 104 | | { |
| | 0 | 105 | | Id = Guid.NewGuid(), |
| | 0 | 106 | | WorldId = worldId, |
| | 0 | 107 | | Url = dto.Url.Trim(), |
| | 0 | 108 | | Title = dto.Title.Trim(), |
| | 0 | 109 | | Description = string.IsNullOrWhiteSpace(dto.Description) ? null : dto.Description.Trim(), |
| | 0 | 110 | | CreatedAt = DateTime.UtcNow |
| | 0 | 111 | | }; |
| | | 112 | | |
| | 0 | 113 | | _db.WorldLinks.Add(link); |
| | 0 | 114 | | await _db.SaveChangesAsync(); |
| | | 115 | | |
| | 0 | 116 | | var result = new WorldLinkDto |
| | 0 | 117 | | { |
| | 0 | 118 | | Id = link.Id, |
| | 0 | 119 | | WorldId = link.WorldId, |
| | 0 | 120 | | Url = link.Url, |
| | 0 | 121 | | Title = link.Title, |
| | 0 | 122 | | Description = link.Description, |
| | 0 | 123 | | CreatedAt = link.CreatedAt |
| | 0 | 124 | | }; |
| | | 125 | | |
| | 0 | 126 | | return CreatedAtAction(nameof(GetWorldLinks), new { worldId }, result); |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// PUT /worlds/{worldId}/links/{linkId} - Update an existing world link. |
| | | 131 | | /// </summary> |
| | | 132 | | [HttpPut("{linkId:guid}")] |
| | | 133 | | public async Task<ActionResult<WorldLinkDto>> UpdateWorldLink( |
| | | 134 | | Guid worldId, |
| | | 135 | | Guid linkId, |
| | | 136 | | [FromBody] WorldLinkUpdateDto dto) |
| | | 137 | | { |
| | 0 | 138 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | | 139 | | |
| | | 140 | | // Verify user owns the world |
| | 0 | 141 | | var world = await _db.Worlds |
| | 0 | 142 | | .AsNoTracking() |
| | 0 | 143 | | .FirstOrDefaultAsync(w => w.Id == worldId && w.OwnerId == user.Id); |
| | | 144 | | |
| | 0 | 145 | | if (world == null) |
| | | 146 | | { |
| | 0 | 147 | | return NotFound(new { error = "World not found or access denied" }); |
| | | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | var link = await _db.WorldLinks |
| | 0 | 151 | | .FirstOrDefaultAsync(wl => wl.Id == linkId && wl.WorldId == worldId); |
| | | 152 | | |
| | 0 | 153 | | if (link == null) |
| | | 154 | | { |
| | 0 | 155 | | return NotFound(new { error = "Link not found" }); |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | if (dto == null || string.IsNullOrWhiteSpace(dto.Url) || string.IsNullOrWhiteSpace(dto.Title)) |
| | | 159 | | { |
| | 0 | 160 | | return BadRequest(new { error = "URL and Title are required" }); |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | // Validate URL format |
| | 0 | 164 | | if (!Uri.TryCreate(dto.Url, UriKind.Absolute, out var uri) || |
| | 0 | 165 | | (uri.Scheme != "http" && uri.Scheme != "https")) |
| | | 166 | | { |
| | 0 | 167 | | return BadRequest(new { error = "Invalid URL format. Must be a valid http or https URL." }); |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | _logger.LogDebug("Updating link {LinkId} for world {WorldId} by user {UserId}", |
| | 0 | 171 | | linkId, worldId, user.Id); |
| | | 172 | | |
| | 0 | 173 | | link.Url = dto.Url.Trim(); |
| | 0 | 174 | | link.Title = dto.Title.Trim(); |
| | 0 | 175 | | link.Description = string.IsNullOrWhiteSpace(dto.Description) ? null : dto.Description.Trim(); |
| | | 176 | | |
| | 0 | 177 | | await _db.SaveChangesAsync(); |
| | | 178 | | |
| | 0 | 179 | | var result = new WorldLinkDto |
| | 0 | 180 | | { |
| | 0 | 181 | | Id = link.Id, |
| | 0 | 182 | | WorldId = link.WorldId, |
| | 0 | 183 | | Url = link.Url, |
| | 0 | 184 | | Title = link.Title, |
| | 0 | 185 | | Description = link.Description, |
| | 0 | 186 | | CreatedAt = link.CreatedAt |
| | 0 | 187 | | }; |
| | | 188 | | |
| | 0 | 189 | | return Ok(result); |
| | 0 | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// DELETE /worlds/{worldId}/links/{linkId} - Delete a world link. |
| | | 194 | | /// </summary> |
| | | 195 | | [HttpDelete("{linkId:guid}")] |
| | | 196 | | public async Task<IActionResult> DeleteWorldLink(Guid worldId, Guid linkId) |
| | | 197 | | { |
| | 0 | 198 | | var user = await _currentUserService.GetRequiredUserAsync(); |
| | | 199 | | |
| | | 200 | | // Verify user owns the world |
| | 0 | 201 | | var world = await _db.Worlds |
| | 0 | 202 | | .AsNoTracking() |
| | 0 | 203 | | .FirstOrDefaultAsync(w => w.Id == worldId && w.OwnerId == user.Id); |
| | | 204 | | |
| | 0 | 205 | | if (world == null) |
| | | 206 | | { |
| | 0 | 207 | | return NotFound(new { error = "World not found or access denied" }); |
| | | 208 | | } |
| | | 209 | | |
| | 0 | 210 | | var link = await _db.WorldLinks |
| | 0 | 211 | | .FirstOrDefaultAsync(wl => wl.Id == linkId && wl.WorldId == worldId); |
| | | 212 | | |
| | 0 | 213 | | if (link == null) |
| | | 214 | | { |
| | 0 | 215 | | return NotFound(new { error = "Link not found" }); |
| | | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | _logger.LogDebug("Deleting link {LinkId} for world {WorldId} by user {UserId}", |
| | 0 | 219 | | linkId, worldId, user.Id); |
| | | 220 | | |
| | 0 | 221 | | _db.WorldLinks.Remove(link); |
| | 0 | 222 | | await _db.SaveChangesAsync(); |
| | | 223 | | |
| | 0 | 224 | | return NoContent(); |
| | 0 | 225 | | } |
| | | 226 | | } |