From 48184cd8b661d56263ca1284c9805c346541453e Mon Sep 17 00:00:00 2001 From: AleksDw Date: Sat, 31 May 2025 02:24:54 +0200 Subject: [PATCH] Add remove endpoint --- .../Endpoints/EventRegistrationEndpoints.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/WebApp/Endpoints/EventRegistrationEndpoints.cs b/WebApp/Endpoints/EventRegistrationEndpoints.cs index cec14f5..bb45476 100644 --- a/WebApp/Endpoints/EventRegistrationEndpoints.cs +++ b/WebApp/Endpoints/EventRegistrationEndpoints.cs @@ -104,6 +104,31 @@ namespace WebApp.Endpoints }); }); + // POST /api/events/remove/{id}/{userId} + group.MapPost("/remove/{id}/{userId}", + async (int id, int userId, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) => + { + Event? Eve = await dbContext.Events.FindAsync(id); + if (Eve is null) + return Results.Json(new { success = false, error_msg = "Event not found." }); + + Token? token = await guhf.GetTokenFromHTTPContext(httpContext); + Organisation? org = await guhf.GetOrganisationFromToken(token); + if (org is null || org.OrganisationId != Eve.OrganisationId) + return Results.Json(new { success = false, error_msg = "Unauthorized." }); + + EventRegistration? registration = await dbContext.EventRegistrations + .FirstOrDefaultAsync(er => er.UserId == userId && er.EventId == id); + + if (registration is null) + return Results.Json(new { success = false, error_msg = "Registration not found." }); + + dbContext.EventRegistrations.Remove(registration); + await dbContext.SaveChangesAsync(); + + return Results.Json(new { success = true }); + }); + return group; } }