-
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 votes and user activity to immediate apis
- Added `es-toolkit` package instead of janky extension helper methods - Fixed RSS for chapters - Fixed notification button
- Loading branch information
Showing
23 changed files
with
247 additions
and
297 deletions.
There are no files selected for viewing
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
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,45 @@ | ||
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.UserActivity; | ||
|
||
using ReturnType = Results<Ok, NotFound>; | ||
|
||
[Handler] | ||
[MapMethod("api/useractivity", "HEAD")] | ||
[Authorize] | ||
public static partial class UpdateLastActive | ||
{ | ||
internal static void CustomizeEndpoint(IEndpointConventionBuilder endpoint) => endpoint | ||
.DisableAntiforgery() | ||
.WithName(nameof(UpdateLastActive)); | ||
|
||
[UsedImplicitly] | ||
public sealed record Command; | ||
|
||
private static async ValueTask<ReturnType> HandleAsync( | ||
Command _, | ||
ApplicationDbContext context, | ||
IUserService userService, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
if (userService.User?.GetNumericId() is not {} uid) return TypedResults.NotFound(); | ||
|
||
var rows = await context.Users | ||
.Where(u => u.Id == uid) | ||
.ExecuteUpdateAsync( | ||
setters => setters.SetProperty(u => u.LastActive, DateTime.Now.ToUniversalTime()), | ||
cancellationToken | ||
); | ||
|
||
return rows > 0 ? TypedResults.Ok() : TypedResults.NotFound(); | ||
} | ||
} |
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,51 @@ | ||
using Immediate.Apis.Shared; | ||
using Immediate.Handlers.Shared; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using Microsoft.EntityFrameworkCore; | ||
using Ogma3.Data; | ||
using Ogma3.Data.Votes; | ||
using Ogma3.Infrastructure.Extensions; | ||
using Ogma3.Services.UserService; | ||
|
||
namespace Ogma3.Api.V1.Votes; | ||
|
||
using ReturnType = Results<UnauthorizedHttpResult, Ok<VoteResult>>; | ||
|
||
[Handler] | ||
[MapPost("api/votes")] | ||
[Authorize] | ||
public static partial class CreateVote | ||
{ | ||
public sealed record Command(long StoryId); | ||
|
||
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 didUserVote = await context.Votes | ||
.Where(v => v.StoryId == request.StoryId) | ||
.Where(v => v.UserId == uid) | ||
.AnyAsync(cancellationToken); | ||
|
||
if (didUserVote) return TypedResults.Ok(new VoteResult(true)); | ||
|
||
context.Votes.Add(new Vote | ||
{ | ||
UserId = uid, | ||
StoryId = request.StoryId, | ||
}); | ||
await context.SaveChangesAsync(cancellationToken); | ||
|
||
var count = await context.Votes | ||
.Where(v => v.StoryId == request.StoryId) | ||
.CountAsync(cancellationToken); | ||
|
||
return TypedResults.Ok(new VoteResult(true, count)); | ||
} | ||
} |
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,46 @@ | ||
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.Infrastructure.Extensions; | ||
using Ogma3.Services.UserService; | ||
|
||
namespace Ogma3.Api.V1.Votes; | ||
|
||
using ReturnType = Results<UnauthorizedHttpResult, Ok<VoteResult>, NotFound>; | ||
|
||
[Handler] | ||
[MapDelete("api/votes")] | ||
[Authorize] | ||
public static partial class DeleteVote | ||
{ | ||
[UsedImplicitly] | ||
public sealed record Command(long StoryId); | ||
|
||
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 res = await context.Votes | ||
.Where(v => v.StoryId == request.StoryId) | ||
.Where(v => v.UserId == uid) | ||
.ExecuteDeleteAsync(cancellationToken); | ||
|
||
if (res <= 0) return TypedResults.NotFound(); | ||
|
||
var count = await context.Votes | ||
.Where(v => v.StoryId == request.StoryId) | ||
.CountAsync(cancellationToken); | ||
|
||
return TypedResults.Ok(new VoteResult(false, count)); | ||
} | ||
} |
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,40 @@ | ||
using Immediate.Apis.Shared; | ||
using Immediate.Handlers.Shared; | ||
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.Votes; | ||
|
||
using ReturnType = Results<UnauthorizedHttpResult, Ok<VoteResult>>; | ||
|
||
[Handler] | ||
[MapGet("api/votes/{storyId:long}")] | ||
[Authorize] | ||
public static partial class GetVotes | ||
{ | ||
public sealed record Query(long StoryId); | ||
|
||
private static async ValueTask<ReturnType> HandleAsync( | ||
Query request, | ||
ApplicationDbContext context, | ||
IUserService userService, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
if (userService.User?.GetNumericId() is not {} uid) return TypedResults.Unauthorized(); | ||
|
||
var count = await context.Votes | ||
.Where(v => v.StoryId == request.StoryId) | ||
.CountAsync(cancellationToken); | ||
var didUserVote = await context.Votes | ||
.Where(v => v.StoryId == request.StoryId) | ||
.Where(v => v.UserId == uid) | ||
.AnyAsync(cancellationToken); | ||
|
||
return TypedResults.Ok(new VoteResult(didUserVote, count)); | ||
} | ||
} |
Oops, something went wrong.