Skip to content

Commit

Permalink
Moved club join/leave functionality to Immediate.Apis
Browse files Browse the repository at this point in the history
- Updated `UserBanMiddleware` to use `ILogger` instead of `Serilog`.
- Updated `join-club-button.ts` to use new API endpoints for joining and leaving clubs.
  • Loading branch information
Atulin committed Aug 29, 2024
1 parent 0a0ca79 commit 10e4328
Show file tree
Hide file tree
Showing 12 changed files with 2,409 additions and 2,417 deletions.
34 changes: 0 additions & 34 deletions Ogma3/Api/V1/ClubJoin/ClubJoinController.cs

This file was deleted.

59 changes: 0 additions & 59 deletions Ogma3/Api/V1/ClubJoin/Commands/JoinClub.cs

This file was deleted.

52 changes: 0 additions & 52 deletions Ogma3/Api/V1/ClubJoin/Commands/LeaveClub.cs

This file was deleted.

39 changes: 39 additions & 0 deletions Ogma3/Api/V1/ClubJoin/GetClubMembershipStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Ogma3.Data;
using Ogma3.Infrastructure.Extensions;
using Ogma3.Services.UserService;

namespace Ogma3.Api.V1.ClubJoin;

using ReturnType = Ok<bool>;

[Handler]
[MapGet("/api/clubjoin/{clubId:long}")]
[Authorize]
public static partial class GetClubMembershipStatus
{
[UsedImplicitly]
public sealed record Query(long ClubId);

private static async ValueTask<ReturnType> HandleAsync(
Query request,
ApplicationDbContext context,
IUserService userService,
CancellationToken cancellationToken
)
{
if (userService.User?.GetNumericId() is not {} uid) return TypedResults.Ok(false);

var isMember = await context.ClubMembers
.Where(cm => cm.ClubId == request.ClubId)
.Where(cm => cm.MemberId == uid)
.AnyAsync(cancellationToken);

return TypedResults.Ok(isMember);
}
}
60 changes: 60 additions & 0 deletions Ogma3/Api/V1/ClubJoin/JoinClub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Ogma3.Data;
using Ogma3.Data.Clubs;
using Ogma3.Infrastructure.Extensions;
using Ogma3.Services.UserService;

namespace Ogma3.Api.V1.ClubJoin;

using ReturnType = Results<UnauthorizedHttpResult, NotFound, Ok<bool>>;

[Handler]
[MapPost("api/clubjoin")]
[Authorize]
public static partial class JoinClub
{
[UsedImplicitly]
public sealed record Command(long ClubId);

private static async ValueTask<ReturnType> HandleAsync(
Command request,
ApplicationDbContext context,
IUserService userService,
CancellationToken cancellationToken
)
{
if (userService.User?.GetNumericId() is not {} uid) return TypedResults.Unauthorized();

var memberOrBanned = await context.Clubs
.Where(c => c.Id == request.ClubId)
.Select(c => new MemberOrBanned(
c.ClubMembers.Any(cm => cm.MemberId == uid),
c.BannedUsers.Any(u => u.Id == uid)
))
.FirstOrDefaultAsync(cancellationToken);

if (memberOrBanned is null) return TypedResults.NotFound();

if (memberOrBanned.IsMember) return TypedResults.Ok(true);

if (memberOrBanned.IsBanned) return TypedResults.Unauthorized();

context.ClubMembers.Add(new ClubMember
{
ClubId = request.ClubId,
MemberId = uid,
Role = EClubMemberRoles.User,
});

await context.SaveChangesAsync(cancellationToken);

return TypedResults.Ok(true);
}

private record MemberOrBanned(bool IsMember, bool IsBanned);
}
54 changes: 54 additions & 0 deletions Ogma3/Api/V1/ClubJoin/LeaveClub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Ogma3.Data;
using Ogma3.Data.Clubs;
using Ogma3.Infrastructure.Extensions;
using Ogma3.Services.UserService;

namespace Ogma3.Api.V1.ClubJoin;

using ReturnType = Results<UnauthorizedHttpResult, Ok<bool>, BadRequest<string>>;

[Handler]
[MapDelete("api/clubjoin")]
[Authorize]
public static partial class LeaveClub
{
[UsedImplicitly]
public sealed record Command(long ClubId);

private static async ValueTask<ReturnType> HandleAsync(
[FromBody] Command request,
ApplicationDbContext context,
IUserService userService,
CancellationToken cancellationToken
)
{
if (userService.User?.GetNumericId() is not {} uid) return TypedResults.Unauthorized();

var isFounder = await context.ClubMembers
.Where(cm => cm.MemberId == uid)
.Where(cm => cm.ClubId == request.ClubId)
.Where(cm => cm.Role == EClubMemberRoles.Founder)
.AnyAsync(cancellationToken);

if (isFounder) return TypedResults.BadRequest("Founder cannot leave their club. Delete it instead.");

var member = await context.ClubMembers
.Where(cm => cm.MemberId == uid)
.Where(cm => cm.ClubId == request.ClubId)
.FirstOrDefaultAsync(cancellationToken);

if (member is null) return TypedResults.Ok(false);

context.ClubMembers.Remove(member);
await context.SaveChangesAsync(cancellationToken);

return TypedResults.Ok(false);
}
}
38 changes: 0 additions & 38 deletions Ogma3/Api/V1/ClubJoin/Queries/GetClubMembershipStatus.cs

This file was deleted.

5 changes: 2 additions & 3 deletions Ogma3/Infrastructure/Middleware/UserBanMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
using Ogma3.Data;
using Ogma3.Data.Infractions;
using Ogma3.Infrastructure.Extensions;
using Serilog;

namespace Ogma3.Infrastructure.Middleware;

public class UserBanMiddleware(IMemoryCache cache, ApplicationDbContext dbContext) : IMiddleware
public class UserBanMiddleware(IMemoryCache cache, ApplicationDbContext dbContext, ILogger<UserBanMiddleware> logger) : IMiddleware
{
public static string CacheKey(long id) => $"u{id}_Ban";

Expand Down Expand Up @@ -41,7 +40,7 @@ public async Task InvokeAsync(HttpContext httpContext, RequestDelegate next)

if (banDate > DateTime.Now)
{
Log.Information("Banned user {UserId} tried accessing the site", uid);
logger.LogInformation("Banned user {UserId} tried accessing the site", uid);
if (httpContext.Request.Path.StartsWithSegments("/api"))
{
httpContext.Response.Clear();
Expand Down
Loading

0 comments on commit 10e4328

Please sign in to comment.