git-subtree-dir: backend git-subtree-mainline:ab911955edgit-subtree-split:040cfd7a75
69 lines
2.1 KiB
C#
69 lines
2.1 KiB
C#
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 GetContentsByCreatorRequest
|
|
{
|
|
public Guid CreatorId { get; set; }
|
|
[BindFrom("page_size")] public int PageSize { get; set; } = 10;
|
|
[BindFrom("last_id")] public Guid? LastId { get; set; }
|
|
}
|
|
|
|
[PublicAPI]
|
|
public class GetContentsByCreatorHandler(
|
|
ContentDbContext context)
|
|
: Endpoint<GetContentsByCreatorRequest, List<ContentModel>>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("/api/contents/creator/{CreatorId:guid}");
|
|
Options(o => o.WithTags("Contents"));
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
GetContentsByCreatorRequest req,
|
|
CancellationToken ct)
|
|
{
|
|
var query = context.Contents
|
|
.Where(c => c.CreatedBy == req.CreatorId && c.DeletedAt == null)
|
|
.OrderByDescending(c => c.CreatedAt);
|
|
|
|
if (req.LastId.HasValue)
|
|
{
|
|
query = query.Where(c => c.Id > req.LastId.Value)
|
|
.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,
|
|
DeletedBy = c.DeletedBy,
|
|
DeletedAt = c.DeletedAt,
|
|
Title = c.Title,
|
|
Description = c.Description,
|
|
Urls = c.Urls,
|
|
ThumbnailUrl = c.ThumbnailUrl,
|
|
HtmlFileUrl = c.HtmlFileUrl ?? "",
|
|
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);
|
|
}
|
|
}
|