mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40:13 +01:00
Compare commits
3 Commits
4be57c27d9
...
a81a57654c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a81a57654c | ||
|
|
48184cd8b6 | ||
|
|
f2ccde2ea6 |
11
WebApp/DTOs/EventRegistrationDto.cs
Normal file
11
WebApp/DTOs/EventRegistrationDto.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using WebApp.Entities;
|
||||||
|
|
||||||
|
namespace WebApp.DTOs;
|
||||||
|
|
||||||
|
// Output values in JSON file
|
||||||
|
public record class EventRegistrationDto(
|
||||||
|
int EventId,
|
||||||
|
int UserId,
|
||||||
|
DateTime RegisteredAt
|
||||||
|
);
|
||||||
135
WebApp/Endpoints/EventRegistrationEndpoints.cs
Normal file
135
WebApp/Endpoints/EventRegistrationEndpoints.cs
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Http.HttpResults;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using WebApp.Data;
|
||||||
|
using WebApp.DTOs;
|
||||||
|
using WebApp.Entities;
|
||||||
|
using WebApp.Mapping;
|
||||||
|
|
||||||
|
namespace WebApp.Endpoints
|
||||||
|
{
|
||||||
|
public static class EventsRegistrationEndpoints
|
||||||
|
{
|
||||||
|
const string GetEventEndpointRegistrationName = "GetEventRegistration";
|
||||||
|
|
||||||
|
public static RouteGroupBuilder MapEventsRegistrationEndpoints(this WebApplication app)
|
||||||
|
{
|
||||||
|
var group = app.MapGroup("api/events")
|
||||||
|
.WithParameterValidation();
|
||||||
|
|
||||||
|
// POST /api/events/join/{id}
|
||||||
|
group.MapPost("/join/{id}",
|
||||||
|
async (int id, 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);
|
||||||
|
User? user = await guhf.GetUserFromToken(token);
|
||||||
|
|
||||||
|
if (user is null || user.IsOrganisation)
|
||||||
|
return Results.Json(new { success = false, error_msg = "Unauthorized or organisations cannot register for events." });
|
||||||
|
|
||||||
|
if (await dbContext.EventRegistrations.AnyAsync(er => er.UserId == user.UserId && er.EventId == id))
|
||||||
|
return Results.Json(new { success = false, error_msg = "You are already registered for this event." });
|
||||||
|
|
||||||
|
if (Eve.EventDate < DateTime.UtcNow)
|
||||||
|
return Results.Json(new { success = false, error_msg = "This event has already ended." });
|
||||||
|
|
||||||
|
EventRegistration registration = new EventRegistration
|
||||||
|
{
|
||||||
|
UserId = user.UserId,
|
||||||
|
EventId = id,
|
||||||
|
RegisteredAt = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
dbContext.EventRegistrations.Add(registration);
|
||||||
|
await dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
return Results.Json(new { success = true });
|
||||||
|
});
|
||||||
|
|
||||||
|
// POST /api/events/leave/{id}
|
||||||
|
group.MapPost("/leave/{id}",
|
||||||
|
async (int id, 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);
|
||||||
|
User? user = await guhf.GetUserFromToken(token);
|
||||||
|
|
||||||
|
if (user is null)
|
||||||
|
return Results.Json(new { success = false, error_msg = "Unauthorized." });
|
||||||
|
|
||||||
|
if (!await dbContext.EventRegistrations.AnyAsync(er => er.UserId == user.UserId && er.EventId == id))
|
||||||
|
return Results.Json(new { success = false, error_msg = "You are not registered for this event." });
|
||||||
|
|
||||||
|
if (Eve.EventDate < DateTime.UtcNow)
|
||||||
|
return Results.Json(new { success = false, error_msg = "This event has already ended." });
|
||||||
|
|
||||||
|
EventRegistration? registration = await dbContext.EventRegistrations
|
||||||
|
.FirstOrDefaultAsync(er => er.UserId == user.UserId && er.EventId == id);
|
||||||
|
|
||||||
|
dbContext.EventRegistrations.Remove(registration);
|
||||||
|
await dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
return Results.Json(new { success = true });
|
||||||
|
});
|
||||||
|
|
||||||
|
// GET /api/events/registrations/{id}
|
||||||
|
group.MapGet("/registrations/{id}",
|
||||||
|
async (int id, 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." });
|
||||||
|
|
||||||
|
var registrations = await dbContext.EventRegistrations
|
||||||
|
.Where(er => er.EventId == id)
|
||||||
|
.Select(er => er.ToEventRegistrationDto())
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
return Results.Json(new
|
||||||
|
{
|
||||||
|
success = true,
|
||||||
|
registrations
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
WebApp/Mapping/EventRegistrationMapping.cs
Normal file
17
WebApp/Mapping/EventRegistrationMapping.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using WebApp.DTOs;
|
||||||
|
using WebApp.Entities;
|
||||||
|
|
||||||
|
namespace WebApp.Mapping
|
||||||
|
{
|
||||||
|
public static class EventRegistrationMapping
|
||||||
|
{
|
||||||
|
public static EventRegistrationDto ToEventRegistrationDto(this EventRegistration er)
|
||||||
|
{
|
||||||
|
return new EventRegistrationDto(
|
||||||
|
er.EventId,
|
||||||
|
er.UserId,
|
||||||
|
er.RegisteredAt
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -53,5 +53,6 @@ app.UseRouting(); // Enables routing to match incoming request to endpoints
|
|||||||
app.MapEventsEndpoints();
|
app.MapEventsEndpoints();
|
||||||
app.MapOrganizationsEndpoints();
|
app.MapOrganizationsEndpoints();
|
||||||
app.MapAuthEndpoints();
|
app.MapAuthEndpoints();
|
||||||
|
app.MapEventsRegistrationEndpoints();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
Reference in New Issue
Block a user