-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved club join/leave functionality to Immediate.Apis
- 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
Showing
12 changed files
with
2,409 additions
and
2,417 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.