From 6e36d1327c8f78207a4fe25bdd5a741799b8f74b Mon Sep 17 00:00:00 2001 From: Dominic Villemure Date: Sun, 25 Aug 2024 16:15:06 -0400 Subject: [PATCH] Added endpoint for the feed ( for you page ) --- .../Features/Contents/Handlers/GetContents.cs | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/Web/Features/Contents/Handlers/GetContents.cs diff --git a/src/Web/Features/Contents/Handlers/GetContents.cs b/src/Web/Features/Contents/Handlers/GetContents.cs new file mode 100644 index 0000000..1f9a87b --- /dev/null +++ b/src/Web/Features/Contents/Handlers/GetContents.cs @@ -0,0 +1,68 @@ +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 GetContentsRequest +{ + [BindFrom("page_size")] public int PageSize { get; set; } = 10; + [BindFrom("last_id")] public Guid? LastId { get; set; } +} + +[PublicAPI] +public class GetContentsHandler( + ContentDbContext context) + : Endpoint> +{ + public override void Configure() + { + Get("/api/contents/all"); + Options(o => o.WithTags("Contents")); + AllowAnonymous(); + } + + public override async Task HandleAsync( + GetContentsRequest req, + CancellationToken ct) + { + + var query = context.Contents + .Where(c => 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, + 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); + } +}