|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using Nornis.Api.Contracts.Requests; |
| 3 | +using Nornis.Api.Contracts.Responses; |
| 4 | +using Nornis.Api.Extensions; |
| 5 | +using Nornis.Api.Filters; |
| 6 | +using Nornis.Application.Errors; |
| 7 | +using Nornis.Application.Models; |
| 8 | +using Nornis.Application.Services; |
| 9 | +using Nornis.Domain.Entities; |
| 10 | +using Nornis.Domain.Enums; |
| 11 | + |
| 12 | +namespace Nornis.Api.Controllers; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// GM management of a world's reusable invite links. World-scoped: the |
| 16 | +/// <see cref="WorldMemberActionFilter"/> resolves membership, and every action is GM-only. |
| 17 | +/// Redemption of an invite lives on the separate, non-world-scoped <see cref="InvitesController"/>, |
| 18 | +/// because an invitee is by definition not yet a member. |
| 19 | +/// </summary> |
| 20 | +[ApiController] |
| 21 | +[Route("api/worlds/{worldId:guid}/invites")] |
| 22 | +[ServiceFilter(typeof(WorldMemberActionFilter))] |
| 23 | +public class WorldInvitesController : ControllerBase |
| 24 | +{ |
| 25 | + private readonly IWorldInviteService _inviteService; |
| 26 | + |
| 27 | + public WorldInvitesController(IWorldInviteService inviteService) |
| 28 | + { |
| 29 | + _inviteService = inviteService; |
| 30 | + } |
| 31 | + |
| 32 | + [HttpGet] |
| 33 | + public async Task<IActionResult> List(Guid worldId, CancellationToken ct) |
| 34 | + { |
| 35 | + if (HttpContext.GetWorldMember().Role != WorldRole.GM) |
| 36 | + { |
| 37 | + return StatusCode(403, new ErrorResponse("insufficient_role", "Only GMs can view invites.")); |
| 38 | + } |
| 39 | + |
| 40 | + var user = HttpContext.GetNornisUser(); |
| 41 | + var result = await _inviteService.ListAsync(worldId, user.Id, ct); |
| 42 | + |
| 43 | + if (!result.IsSuccess) |
| 44 | + { |
| 45 | + return MapError(result.Error!); |
| 46 | + } |
| 47 | + |
| 48 | + var response = result.Value!.Select(ToResponse).ToList(); |
| 49 | + return Ok(response); |
| 50 | + } |
| 51 | + |
| 52 | + [HttpPost] |
| 53 | + public async Task<IActionResult> Create( |
| 54 | + Guid worldId, |
| 55 | + [FromBody] CreateInviteRequest request, |
| 56 | + CancellationToken ct) |
| 57 | + { |
| 58 | + if (HttpContext.GetWorldMember().Role != WorldRole.GM) |
| 59 | + { |
| 60 | + return StatusCode(403, new ErrorResponse("insufficient_role", "Only GMs can create invites.")); |
| 61 | + } |
| 62 | + |
| 63 | + if (!Enum.TryParse<WorldRole>(request.Role, ignoreCase: true, out var role)) |
| 64 | + { |
| 65 | + return BadRequest(new ErrorResponse("invalid_role", $"'{request.Role}' is not a valid world role.")); |
| 66 | + } |
| 67 | + |
| 68 | + var user = HttpContext.GetNornisUser(); |
| 69 | + var command = new CreateInviteCommand(worldId, user.Id, role, request.ExpiresAt, request.MaxUses); |
| 70 | + |
| 71 | + var result = await _inviteService.CreateAsync(command, ct); |
| 72 | + if (!result.IsSuccess) |
| 73 | + { |
| 74 | + return MapError(result.Error!); |
| 75 | + } |
| 76 | + |
| 77 | + var response = ToResponse(result.Value!); |
| 78 | + return CreatedAtAction(nameof(List), new { worldId }, response); |
| 79 | + } |
| 80 | + |
| 81 | + [HttpDelete("{inviteId:guid}")] |
| 82 | + public async Task<IActionResult> Revoke(Guid worldId, Guid inviteId, CancellationToken ct) |
| 83 | + { |
| 84 | + if (HttpContext.GetWorldMember().Role != WorldRole.GM) |
| 85 | + { |
| 86 | + return StatusCode(403, new ErrorResponse("insufficient_role", "Only GMs can revoke invites.")); |
| 87 | + } |
| 88 | + |
| 89 | + var user = HttpContext.GetNornisUser(); |
| 90 | + var result = await _inviteService.RevokeAsync(worldId, inviteId, user.Id, ct); |
| 91 | + |
| 92 | + if (!result.IsSuccess) |
| 93 | + { |
| 94 | + return MapError(result.Error!); |
| 95 | + } |
| 96 | + |
| 97 | + return NoContent(); |
| 98 | + } |
| 99 | + |
| 100 | + private static WorldInviteResponse ToResponse(WorldInvite invite) |
| 101 | + { |
| 102 | + return new WorldInviteResponse( |
| 103 | + Id: invite.Id, |
| 104 | + WorldId: invite.WorldId, |
| 105 | + Code: invite.Code, |
| 106 | + Role: invite.Role.ToString(), |
| 107 | + Status: invite.StatusAt(DateTimeOffset.UtcNow).ToString(), |
| 108 | + UseCount: invite.UseCount, |
| 109 | + MaxUses: invite.MaxUses, |
| 110 | + ExpiresAt: invite.ExpiresAt, |
| 111 | + CreatedAt: invite.CreatedAt, |
| 112 | + RevokedAt: invite.RevokedAt); |
| 113 | + } |
| 114 | + |
| 115 | + private IActionResult MapError(AppError error) |
| 116 | + { |
| 117 | + return error.StatusCode switch |
| 118 | + { |
| 119 | + 400 => BadRequest(new ErrorResponse(error.Code, error.Message)), |
| 120 | + 403 => StatusCode(403, new ErrorResponse(error.Code, error.Message)), |
| 121 | + 404 => NotFound(new ErrorResponse(error.Code, error.Message)), |
| 122 | + 409 => Conflict(new ErrorResponse(error.Code, error.Message)), |
| 123 | + _ => StatusCode(error.StatusCode, new ErrorResponse(error.Code, error.Message)) |
| 124 | + }; |
| 125 | + } |
| 126 | +} |
0 commit comments