Skip to content

Commit

Permalink
Allow note creation
Browse files Browse the repository at this point in the history
  • Loading branch information
NixFey committed Jul 25, 2024
1 parent 3218669 commit 71787d4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions FiMAdminApi.Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class DataContext(DbContextOptions<DataContext> options) : DbContext(opti
public DbSet<Level> Levels { get; init; }
public DbSet<Season> Seasons { get; init; }
public DbSet<Event> Events { get; init; }
public DbSet<EventNote> EventNotes { get; init; }
public DbSet<EventStaff> EventStaffs { get; init; }
public DbSet<TruckRoute> TruckRoutes { get; init; }

Expand Down
13 changes: 13 additions & 0 deletions FiMAdminApi.Data/Models/EventNote.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
6 changes: 0 additions & 6 deletions FiMAdminApi/Endpoints/EventNotesEndpoints.cs

This file was deleted.

52 changes: 48 additions & 4 deletions FiMAdminApi/Endpoints/EventsEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -140,6 +142,41 @@ private static async Task<Results<Ok, NotFound, ForbidHttpResult, ValidationProb
return TypedResults.Ok();
}

private static async Task<Results<Ok<EventNote>, 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]
Expand Down Expand Up @@ -170,4 +207,11 @@ public class UpsertEventStaffRequest
[Required]
public ICollection<EventPermission> Permissions { get; set; } = new List<EventPermission>();
}

public class CreateEventNoteRequest
{
[Required]
[MaxLength(4000)]
public required string Content { get; set; }
}
}
2 changes: 2 additions & 0 deletions FiMAdminApi/Infrastructure/SerializerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 71787d4

Please sign in to comment.