From 71787d4c886cb649f823acf176305ce7d569563f Mon Sep 17 00:00:00 2001 From: evanlihou Date: Wed, 24 Jul 2024 21:37:35 -0400 Subject: [PATCH] Allow note creation --- FiMAdminApi.Data/DataContext.cs | 1 + FiMAdminApi.Data/Models/EventNote.cs | 13 +++++ FiMAdminApi/Endpoints/EventNotesEndpoints.cs | 6 --- FiMAdminApi/Endpoints/EventsEndpoints.cs | 52 +++++++++++++++++-- .../Infrastructure/SerializerContext.cs | 2 + 5 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 FiMAdminApi.Data/Models/EventNote.cs delete mode 100644 FiMAdminApi/Endpoints/EventNotesEndpoints.cs diff --git a/FiMAdminApi.Data/DataContext.cs b/FiMAdminApi.Data/DataContext.cs index bf4fbbb..eb2e145 100644 --- a/FiMAdminApi.Data/DataContext.cs +++ b/FiMAdminApi.Data/DataContext.cs @@ -9,6 +9,7 @@ public class DataContext(DbContextOptions options) : DbContext(opti public DbSet Levels { get; init; } public DbSet Seasons { get; init; } public DbSet Events { get; init; } + public DbSet EventNotes { get; init; } public DbSet EventStaffs { get; init; } public DbSet TruckRoutes { get; init; } diff --git a/FiMAdminApi.Data/Models/EventNote.cs b/FiMAdminApi.Data/Models/EventNote.cs new file mode 100644 index 0000000..eb04a86 --- /dev/null +++ b/FiMAdminApi.Data/Models/EventNote.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace FiMAdminApi.Data.Models; + +public class EventNote +{ + [Key] + public int Id { get; set; } + public required Guid EventId { get; set; } + public required string Content { get; set; } + public DateTime CreatedAt { get; set; } + public required Guid CreatedBy { get; set; } +} \ No newline at end of file diff --git a/FiMAdminApi/Endpoints/EventNotesEndpoints.cs b/FiMAdminApi/Endpoints/EventNotesEndpoints.cs deleted file mode 100644 index 787c744..0000000 --- a/FiMAdminApi/Endpoints/EventNotesEndpoints.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace FiMAdminApi.Endpoints; - -public class EventNotesEndpoints -{ - -} \ No newline at end of file diff --git a/FiMAdminApi/Endpoints/EventsEndpoints.cs b/FiMAdminApi/Endpoints/EventsEndpoints.cs index f2bba3f..cad5c60 100644 --- a/FiMAdminApi/Endpoints/EventsEndpoints.cs +++ b/FiMAdminApi/Endpoints/EventsEndpoints.cs @@ -19,16 +19,18 @@ public static class EventsEndpoints { public static WebApplication RegisterEventsEndpoints(this WebApplication app, ApiVersionSet vs) { - var eventsCreateGroup = app.MapGroup("/api/v{apiVersion:apiVersion}/events") + var eventsGroup = app.MapGroup("/api/v{apiVersion:apiVersion}/events") .WithApiVersionSet(vs).HasApiVersion(1).WithTags("Events") .RequireAuthorization(); - eventsCreateGroup.MapPut("{id:guid}", UpdateBasicInfo) + eventsGroup.MapPut("{id:guid}", UpdateBasicInfo) .WithSummary("Update basic event info"); - eventsCreateGroup.MapPut("{eventId:guid}/staffs", UpsertEventStaff) + eventsGroup.MapPut("{eventId:guid}/staffs", UpsertEventStaff) .WithSummary("Create or update a staff user for an event"); - eventsCreateGroup.MapDelete("{eventId:guid}/staffs/{userId:guid}", DeleteEventStaff) + eventsGroup.MapDelete("{eventId:guid}/staffs/{userId:guid}", DeleteEventStaff) .WithSummary("Remove a staff user for an event"); + eventsGroup.MapPost("{eventId:guid}/notes", CreateEventNote) + .WithSummary("Create an event note"); return app; } @@ -140,6 +142,41 @@ private static async Task, NotFound, ForbidHttpResult, ValidationProblem>> CreateEventNote( + [FromRoute] Guid eventId, + [FromBody] CreateEventNoteRequest request, + [FromServices] DataContext dbContext, + ClaimsPrincipal user, + [FromServices] IAuthorizationService authSvc) + { + var (isValid, validationErrors) = await MiniValidator.TryValidateAsync(request); + if (!isValid) return TypedResults.ValidationProblem(validationErrors); + + if (!await dbContext.Events.AnyAsync(e => e.Id == eventId)) + return TypedResults.NotFound(); + + var isAuthorized = await authSvc.AuthorizeAsync(user, eventId, new EventAuthorizationRequirement + { + NeededEventPermission = EventPermission.Event_Note, + NeededGlobalPermission = GlobalPermission.Events_Note + }); + if (!isAuthorized.Succeeded) return TypedResults.Forbid(); + + var userId = user.Claims.FirstOrDefault(c => c.Type == "id"); + var note = new EventNote + { + EventId = eventId, + Content = request.Content, + CreatedAt = DateTime.UtcNow, + CreatedBy = Guid.TryParse(userId?.Value, out var guid) ? guid : Guid.Empty + }; + dbContext.EventNotes.Add(note); + + await dbContext.SaveChangesAsync(); + + return TypedResults.Ok(note); + } + public class UpdateBasicInfoRequest { [Required] @@ -170,4 +207,11 @@ public class UpsertEventStaffRequest [Required] public ICollection Permissions { get; set; } = new List(); } + + public class CreateEventNoteRequest + { + [Required] + [MaxLength(4000)] + public required string Content { get; set; } + } } \ No newline at end of file diff --git a/FiMAdminApi/Infrastructure/SerializerContext.cs b/FiMAdminApi/Infrastructure/SerializerContext.cs index 28f550c..3fb8fcb 100644 --- a/FiMAdminApi/Infrastructure/SerializerContext.cs +++ b/FiMAdminApi/Infrastructure/SerializerContext.cs @@ -13,8 +13,10 @@ namespace FiMAdminApi.Infrastructure; [JsonSerializable(typeof(UpsertEventsService.UpsertEventsResponse))] [JsonSerializable(typeof(EventsEndpoints.UpdateBasicInfoRequest))] [JsonSerializable(typeof(EventsEndpoints.UpsertEventStaffRequest))] +[JsonSerializable(typeof(EventsEndpoints.CreateEventNoteRequest))] [JsonSerializable(typeof(User[]))] [JsonSerializable(typeof(EventStaff))] +[JsonSerializable(typeof(EventNote))] [JsonSerializable(typeof(HealthEndpoints.ThinHealthReport))] public partial class SerializerContext : JsonSerializerContext {