Fix GetContentsByCreator ugly ef query

This commit is contained in:
Jonathan Bourdon
2024-08-07 13:05:33 -04:00
parent 7baf1146ac
commit 50ecfa397c
3 changed files with 41 additions and 38 deletions

View File

@@ -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; using Hutopy.Web.Features.Contents.Handlers.Models;
namespace Hutopy.Web.Features.Contents.Handlers; namespace Hutopy.Web.Features.Contents.Handlers;
@@ -6,7 +7,7 @@ namespace Hutopy.Web.Features.Contents.Handlers;
[PublicAPI] [PublicAPI]
public sealed class GetContentsByCreatorRequest public sealed class GetContentsByCreatorRequest
{ {
public Guid UserId { get; set; } public Guid CreatorId { get; set; }
[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; }
} }
@@ -18,7 +19,7 @@ public class GetContentsByCreatorHandler(
{ {
public override void Configure() public override void Configure()
{ {
Get("/api/contents/creator/{UserId:guid}"); Get("/api/contents/creator/{CreatorId:guid}");
Options(o => o.WithTags("Contents")); Options(o => o.WithTags("Contents"));
AllowAnonymous(); AllowAnonymous();
} }
@@ -27,21 +28,32 @@ public class GetContentsByCreatorHandler(
GetContentsByCreatorRequest req, GetContentsByCreatorRequest req,
CancellationToken ct) CancellationToken ct)
{ {
var query = context var queryBuilder = new StringBuilder();
.Contents queryBuilder.AppendLine($"""
.Where(c => c.CreatedBy == req.UserId); 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) queryBuilder.AppendLine($"""
query = query.Where(c => c.Id < req.LastId.Value); ORDER BY content."CreatedAt" DESC
LIMIT {req.PageSize}
""");
query = query.Take(req.PageSize); var query = queryBuilder.ToString();
var posts = await query var results = await context
.Select(x => x.ToResponse()) .Database
.SqlQueryRaw<ContentModel>(query)
.ToListAsync(cancellationToken: ct); .ToListAsync(cancellationToken: ct);
await SendAsync(posts, cancellation: ct); await SendAsync(results, cancellation: ct);
} }
} }

View File

@@ -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] [PublicAPI]
public record struct ContentModel( public class ContentModel
Guid Id,
Guid CreatedBy,
string CreatedByName,
string? CreatedByPortraitUrl,
DateTimeOffset CreatedAt,
string Title,
string Description,
string[]? Urls);
public static class ContentExtensions
{ {
public static ContentModel ToResponse(this Content c) => new( public Guid Id { get; set; }
c.Id, public Guid CreatedBy { get; set; }
c.CreatedBy, public string CreatedByName { get; set; }
c.Creator!.Name, public string? CreatedByPortraitUrl { get; set; }
c.Creator.Images.Logo, public DateTimeOffset CreatedAt { get; set; }
c.CreatedAt, public string Title { get; set; }
c.Title, public string Description { get; set; }
c.Description, public string[]? Urls { get; set; }
c.Urls);
} }

View File

@@ -60,3 +60,7 @@ Authorization: Bearer {{auth_token}}
# GET /api/posts/by-user # GET /api/posts/by-user
GET {{base_url}}/api/messages/by-user/325C69E8-DBC4-4CEE-B56E-C3C90AEE7963 GET {{base_url}}/api/messages/by-user/325C69E8-DBC4-4CEE-B56E-C3C90AEE7963
Authorization: Bearer {{auth_token}} Authorization: Bearer {{auth_token}}
###
# GET /api/contents/creator/{CreatorId}
GET {{base_url}}/api/contents/creator/8590ba59-58a7-4466-bb50-08dcb5e47c6d/