Skip to content

Commit

Permalink
Moved votes and user activity to immediate apis
Browse files Browse the repository at this point in the history
- Added `es-toolkit` package instead of janky extension helper methods
- Fixed RSS for chapters
- Fixed notification button
  • Loading branch information
Atulin committed Jul 30, 2024
1 parent 542f77c commit d8a7ccf
Show file tree
Hide file tree
Showing 23 changed files with 247 additions and 297 deletions.
3 changes: 2 additions & 1 deletion Ogma3/Api/Rss/Queries/GetChapters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public async ValueTask<ActionResult<RssResult>> Handle(Query request, Cancellati
.Where(s => s.Id == request.StoryId)
.Select(s => new
{
Sid = s.Id,
s.Title,
s.ChapterCount,
Chapters = s.Chapters
Expand All @@ -48,7 +49,7 @@ public async ValueTask<ActionResult<RssResult>> Handle(Query request, Cancellati
var items = storyResult.Chapters.Select(s => new SyndicationItem(
s.Title,
s.Hook,
new Uri(generator.GetUriByPage(httpContext, "/Chapter", values: new { s.Id, s.Slug }) ?? ""),
new Uri(generator.GetUriByPage(httpContext, "/Chapter", values: new { storyResult.Sid, s.Id, s.Slug }) ?? ""),
s.Slug,
s.PublicationDate ?? default
));
Expand Down
31 changes: 0 additions & 31 deletions Ogma3/Api/V1/UserActivity/Commands/UpdateLastActive.cs

This file was deleted.

45 changes: 45 additions & 0 deletions Ogma3/Api/V1/UserActivity/UpdateLastActive.cs
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();
}
}
21 changes: 0 additions & 21 deletions Ogma3/Api/V1/UserActivity/UserActivityController.cs

This file was deleted.

54 changes: 0 additions & 54 deletions Ogma3/Api/V1/Votes/Commands/CreateVote.cs

This file was deleted.

46 changes: 0 additions & 46 deletions Ogma3/Api/V1/Votes/Commands/DeleteVote.cs

This file was deleted.

51 changes: 51 additions & 0 deletions Ogma3/Api/V1/Votes/CreateVote.cs
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));
}
}
46 changes: 46 additions & 0 deletions Ogma3/Api/V1/Votes/DeleteVote.cs
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));
}
}
40 changes: 40 additions & 0 deletions Ogma3/Api/V1/Votes/GetVotes.cs
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));
}
}
Loading

0 comments on commit d8a7ccf

Please sign in to comment.