diff --git a/Govor.API/Controllers/AdminStuff/InviteUserController.cs b/Govor.API/Controllers/AdminStuff/InviteUserController.cs index 0fbfcfa..f26bcdd 100644 --- a/Govor.API/Controllers/AdminStuff/InviteUserController.cs +++ b/Govor.API/Controllers/AdminStuff/InviteUserController.cs @@ -14,13 +14,15 @@ public class InviteUserController : Controller private readonly IInvitationGetter _invitationGetter; private readonly IInvitationGenerator _invitationGenerator; private readonly ILogger _logger; - + private readonly IConfiguration _configuration; public InviteUserController(IInvitationGenerator invitationGenerator, IInvitationGetter invitationGetter, + IConfiguration configuration, ILogger logger) { _invitationGenerator = invitationGenerator; _logger = logger; + _configuration = configuration; _invitationGetter = invitationGetter; } @@ -43,6 +45,42 @@ public class InviteUserController : Controller } } + [HttpPost("InvitationWithKey")] + [AllowAnonymous] + public async Task InvitationWithKey([FromBody] CreateInvitationWithKeyRequest request) + { + try + { + var expectedKey = _configuration["InvitationAdmin:Key"]; + + if (string.IsNullOrWhiteSpace(expectedKey)) + { + _logger.LogWarning("InvitationAdmin:Key is not configured"); + return StatusCode(StatusCodes.Status503ServiceUnavailable, "Admin key is not configured"); + } + + if (string.IsNullOrWhiteSpace(request.AdminKey) || + !string.Equals(request.AdminKey, expectedKey, StringComparison.Ordinal)) + { + _logger.LogWarning("Invalid admin key attempt for invitation creation"); + return Unauthorized("Invalid admin key"); + } + + var result = await _invitationGenerator.GenerateInvitationCode( + request.EndDate, + request.MaxParticipants, + request.IsAdmin, + request.Description); + + return Ok(result); + } + catch (Exception e) + { + _logger.LogError(e, e.Message); + return BadRequest($"An error occured: {e.Message}"); + } + } + [HttpGet("[action]")] public async Task GetAllActiveInvitations() { diff --git a/Govor.API/Program.cs b/Govor.API/Program.cs index cff9cb5..95df209 100644 --- a/Govor.API/Program.cs +++ b/Govor.API/Program.cs @@ -164,11 +164,14 @@ if (migrateOnly) return; } -if (app.Environment.IsDevelopment()) +/*if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); -} +}*/ + +app.UseSwagger(); +app.UseSwaggerUI(); //app.UseHttpsRedirection(); diff --git a/Govor.Contracts/Requests/CreateInvitationWithKeyRequest.cs b/Govor.Contracts/Requests/CreateInvitationWithKeyRequest.cs new file mode 100644 index 0000000..c023c06 --- /dev/null +++ b/Govor.Contracts/Requests/CreateInvitationWithKeyRequest.cs @@ -0,0 +1,10 @@ +namespace Govor.Contracts.Requests; + +public class CreateInvitationWithKeyRequest +{ + public DateTime EndDate { get; set; } + public int MaxParticipants { get; set; } + public bool IsAdmin { get; set; } + public string? Description { get; set; } + public string AdminKey { get; set; } = string.Empty; +} \ No newline at end of file