Features + filter on Followed content

This commit is contained in:
Dominic Villemure
2024-08-29 19:37:12 -04:00
parent 4fb31e876b
commit 14b43a3951
2 changed files with 86 additions and 10 deletions

View File

@@ -5,39 +5,38 @@ using Hutopy.Web.Features.Contents.Handlers.Models;
namespace Hutopy.Web.Features.Contents.Handlers;
[PublicAPI]
public sealed class GetContentsRequest
public sealed class GetFeaturedContentsRequest
{
[BindFrom("page_size")] public int PageSize { get; set; } = 10;
[BindFrom("last_id")] public Guid? LastId { get; set; }
}
[PublicAPI]
public class GetContentsHandler(
public class GetFeaturedContentsHandler(
ContentDbContext context)
: Endpoint<GetContentsRequest, List<ContentModel>>
: Endpoint<GetFeaturedContentsRequest, List<ContentModel>>
{
public override void Configure()
{
Get("/api/contents/all");
Get("/api/contents/featured");
Options(o => o.WithTags("Contents"));
AllowAnonymous();
}
public override async Task HandleAsync(
GetContentsRequest req,
GetFeaturedContentsRequest req,
CancellationToken ct)
{
var query = context.Contents
.Where(c => c.DeletedAt == null)
.OrderByDescending(c => c.CreatedAt);
.Where(c => c.DeletedAt == null);
if (req.LastId.HasValue)
{
query = query.Where(c => c.Id > req.LastId.Value)
.OrderByDescending(c => c.CreatedAt);
query = query.Where(c => c.Id > req.LastId.Value);
}
query = query.OrderByDescending(x => x.Reactions.Count);
var content = await query
.Select(c => new ContentModel
{

View File

@@ -0,0 +1,77 @@
using Hutopy.Web.Common;
using Hutopy.Web.Extensions;
using Hutopy.Web.Features.Contents.Data;
using Hutopy.Web.Features.Contents.Handlers.Models;
namespace Hutopy.Web.Features.Contents.Handlers;
[PublicAPI]
public sealed class GetFollowedContentsRequest
{
[BindFrom("page_size")] public int PageSize { get; set; } = 10;
[BindFrom("last_id")] public Guid? LastId { get; set; }
}
[PublicAPI]
public class GetFollowedContentsHandler(
ContentDbContext context)
: Endpoint<GetFollowedContentsRequest, List<ContentModel>>
{
public override void Configure()
{
Get("/api/contents/followed");
Options(o => o.WithTags("Contents"));
AllowAnonymous();
}
public override async Task HandleAsync(
GetFollowedContentsRequest req,
CancellationToken ct)
{
var userId = HttpContext.User.GetUserId();
var userSubscriptionIds = await context
.Subscriptions
.Where(s => s.CreatedBy == userId)
.Select(s => s.CreatorId)
.ToListAsync(cancellationToken: ct);
var query = context.Contents
.Where(c => c.DeletedAt == null)
.Where(x => userSubscriptionIds.Contains(x.CreatedBy));
if (req.LastId.HasValue)
{
query = query.Where(c => c.Id > req.LastId.Value);
}
query = query.OrderByDescending(c => c.CreatedAt);
var content = await query
.Select(c => new ContentModel
{
Id = c.Id,
CreatedBy = c.CreatedBy,
CreatedByName = c.Creator!.Name,
CreatedByPortraitUrl = c.Creator.Images.Logo,
CreatedAt = c.CreatedAt,
ColorMenu = c.Creator.Colors.Menu,
ColorAccent = c.Creator.Colors.Accent,
DeletedBy = c.DeletedBy,
DeletedAt = c.DeletedAt,
Title = c.Title,
Description = c.Description,
Urls = c.Urls,
Reactions = c.Reactions.Select(x => new ReactionModel
{
Reaction = x.Reaction.FromEnum(),
UserId = x.UserId,
UserName = x.UserName
}).ToList()
})
.Take(req.PageSize)
.ToListAsync(ct);
await SendAsync(content, cancellation: ct);
}
}