From 50ecfa397ca60bc23cc5ebc88a09154ea678fb93 Mon Sep 17 00:00:00 2001 From: Jonathan Bourdon Date: Wed, 7 Aug 2024 13:05:33 -0400 Subject: [PATCH] Fix GetContentsByCreator ugly ef query --- .../Contents/Handlers/GetContentsByCreator.cs | 40 ++++++++++++------- .../Contents/Handlers/Models/ContentModel.cs | 33 +++++---------- src/Web/Web.http | 6 ++- 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/Web/Features/Contents/Handlers/GetContentsByCreator.cs b/src/Web/Features/Contents/Handlers/GetContentsByCreator.cs index 910eaa3..cba8624 100644 --- a/src/Web/Features/Contents/Handlers/GetContentsByCreator.cs +++ b/src/Web/Features/Contents/Handlers/GetContentsByCreator.cs @@ -1,4 +1,5 @@ -using Hutopy.Web.Features.Contents.Data; +using System.Text; +using Hutopy.Web.Features.Contents.Data; using Hutopy.Web.Features.Contents.Handlers.Models; namespace Hutopy.Web.Features.Contents.Handlers; @@ -6,7 +7,7 @@ namespace Hutopy.Web.Features.Contents.Handlers; [PublicAPI] public sealed class GetContentsByCreatorRequest { - public Guid UserId { get; set; } + public Guid CreatorId { get; set; } [BindFrom("page_size")] public int PageSize { get; set; } = 10; [BindFrom("last_id")] public Guid? LastId { get; set; } } @@ -18,7 +19,7 @@ public class GetContentsByCreatorHandler( { public override void Configure() { - Get("/api/contents/creator/{UserId:guid}"); + Get("/api/contents/creator/{CreatorId:guid}"); Options(o => o.WithTags("Contents")); AllowAnonymous(); } @@ -27,21 +28,32 @@ public class GetContentsByCreatorHandler( GetContentsByCreatorRequest req, CancellationToken ct) { - var query = context - .Contents - .Where(c => c.CreatedBy == req.UserId); + var queryBuilder = new StringBuilder(); + queryBuilder.AppendLine($""" + SELECT content."Id", content."CreatedBy", creator."Name" as CreatedByName, i."Logo" as CreatedByPortraitUrl, content."CreatedAt", content."Title", content."Description", content."Urls" + FROM "Content"."Contents" AS content + INNER JOIN "Content"."Creators" AS creator ON content."CreatedBy" = creator."Id" + LEFT JOIN "Content"."Images" AS i ON creator."Id" = i."CreatorId" + WHERE content."CreatedBy" = '{req.CreatorId}' + """); - query = query.OrderByDescending(c => c.CreatedAt); + if (req.LastId.HasValue) + { + queryBuilder.AppendLine($"""AND content."Id" < '{req.LastId.Value}'"""); + } - if (req.LastId is not null) - query = query.Where(c => c.Id < req.LastId.Value); + queryBuilder.AppendLine($""" + ORDER BY content."CreatedAt" DESC + LIMIT {req.PageSize} + """); - query = query.Take(req.PageSize); - - var posts = await query - .Select(x => x.ToResponse()) + var query = queryBuilder.ToString(); + + var results = await context + .Database + .SqlQueryRaw(query) .ToListAsync(cancellationToken: ct); - await SendAsync(posts, cancellation: ct); + await SendAsync(results, cancellation: ct); } } diff --git a/src/Web/Features/Contents/Handlers/Models/ContentModel.cs b/src/Web/Features/Contents/Handlers/Models/ContentModel.cs index 56eccf5..2746b99 100644 --- a/src/Web/Features/Contents/Handlers/Models/ContentModel.cs +++ b/src/Web/Features/Contents/Handlers/Models/ContentModel.cs @@ -1,27 +1,14 @@ -using Hutopy.Web.Features.Contents.Data; - -namespace Hutopy.Web.Features.Contents.Handlers.Models; +namespace Hutopy.Web.Features.Contents.Handlers.Models; [PublicAPI] -public record struct ContentModel( - Guid Id, - Guid CreatedBy, - string CreatedByName, - string? CreatedByPortraitUrl, - DateTimeOffset CreatedAt, - string Title, - string Description, - string[]? Urls); - -public static class ContentExtensions +public class ContentModel { - public static ContentModel ToResponse(this Content c) => new( - c.Id, - c.CreatedBy, - c.Creator!.Name, - c.Creator.Images.Logo, - c.CreatedAt, - c.Title, - c.Description, - c.Urls); + public Guid Id { get; set; } + public Guid CreatedBy { get; set; } + public string CreatedByName { get; set; } + public string? CreatedByPortraitUrl { get; set; } + public DateTimeOffset CreatedAt { get; set; } + public string Title { get; set; } + public string Description { get; set; } + public string[]? Urls { get; set; } } diff --git a/src/Web/Web.http b/src/Web/Web.http index bc91375..9f24562 100644 --- a/src/Web/Web.http +++ b/src/Web/Web.http @@ -59,4 +59,8 @@ Authorization: Bearer {{auth_token}} ### # GET /api/posts/by-user GET {{base_url}}/api/messages/by-user/325C69E8-DBC4-4CEE-B56E-C3C90AEE7963 -Authorization: Bearer {{auth_token}} \ No newline at end of file +Authorization: Bearer {{auth_token}} + +### +# GET /api/contents/creator/{CreatorId} +GET {{base_url}}/api/contents/creator/8590ba59-58a7-4466-bb50-08dcb5e47c6d/