Fix GetContentsByCreator ugly ef query
This commit is contained in:
@@ -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 query = queryBuilder.ToString();
|
||||
|
||||
var posts = await query
|
||||
.Select(x => x.ToResponse())
|
||||
var results = await context
|
||||
.Database
|
||||
.SqlQueryRaw<ContentModel>(query)
|
||||
.ToListAsync(cancellationToken: ct);
|
||||
|
||||
await SendAsync(posts, cancellation: ct);
|
||||
await SendAsync(results, cancellation: ct);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
|
||||
@@ -60,3 +60,7 @@ 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}}
|
||||
|
||||
###
|
||||
# GET /api/contents/creator/{CreatorId}
|
||||
GET {{base_url}}/api/contents/creator/8590ba59-58a7-4466-bb50-08dcb5e47c6d/
|
||||
|
||||
Reference in New Issue
Block a user