Merged PR 131: Features + filter on Followed content
Features + filter on Followed content
This commit is contained in:
@@ -5,39 +5,38 @@ using Hutopy.Web.Features.Contents.Handlers.Models;
|
|||||||
namespace Hutopy.Web.Features.Contents.Handlers;
|
namespace Hutopy.Web.Features.Contents.Handlers;
|
||||||
|
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public sealed class GetContentsRequest
|
public sealed class GetFeaturedContentsRequest
|
||||||
{
|
{
|
||||||
[BindFrom("page_size")] public int PageSize { get; set; } = 10;
|
[BindFrom("page_size")] public int PageSize { get; set; } = 10;
|
||||||
[BindFrom("last_id")] public Guid? LastId { get; set; }
|
[BindFrom("last_id")] public Guid? LastId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public class GetContentsHandler(
|
public class GetFeaturedContentsHandler(
|
||||||
ContentDbContext context)
|
ContentDbContext context)
|
||||||
: Endpoint<GetContentsRequest, List<ContentModel>>
|
: Endpoint<GetFeaturedContentsRequest, List<ContentModel>>
|
||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
Get("/api/contents/all");
|
Get("/api/contents/featured");
|
||||||
Options(o => o.WithTags("Contents"));
|
Options(o => o.WithTags("Contents"));
|
||||||
AllowAnonymous();
|
AllowAnonymous();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task HandleAsync(
|
public override async Task HandleAsync(
|
||||||
GetContentsRequest req,
|
GetFeaturedContentsRequest req,
|
||||||
CancellationToken ct)
|
CancellationToken ct)
|
||||||
{
|
{
|
||||||
|
|
||||||
var query = context.Contents
|
var query = context.Contents
|
||||||
.Where(c => c.DeletedAt == null)
|
.Where(c => c.DeletedAt == null);
|
||||||
.OrderByDescending(c => c.CreatedAt);
|
|
||||||
|
|
||||||
if (req.LastId.HasValue)
|
if (req.LastId.HasValue)
|
||||||
{
|
{
|
||||||
query = query.Where(c => c.Id > req.LastId.Value)
|
query = query.Where(c => c.Id > req.LastId.Value);
|
||||||
.OrderByDescending(c => c.CreatedAt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
query = query.OrderByDescending(x => x.Reactions.Count);
|
||||||
|
|
||||||
var content = await query
|
var content = await query
|
||||||
.Select(c => new ContentModel
|
.Select(c => new ContentModel
|
||||||
{
|
{
|
||||||
77
src/Web/Features/Contents/Handlers/GetFollowedContents.cs
Normal file
77
src/Web/Features/Contents/Handlers/GetFollowedContents.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user