Add and remove reaction from a content

This commit is contained in:
Dominic Villemure
2024-08-24 18:39:09 -04:00
parent 63dc032aa4
commit 588be7941c
17 changed files with 660 additions and 45 deletions

View File

@@ -1,4 +1,4 @@
using System.Text;
using Hutopy.Web.Extensions;
using Hutopy.Web.Features.Contents.Data;
using Hutopy.Web.Features.Contents.Handlers.Models;
@@ -28,45 +28,72 @@ public class GetContentsByCreatorHandler(
GetContentsByCreatorRequest req,
CancellationToken ct)
{
var queryBuilder = new StringBuilder();
queryBuilder.AppendLine($"""
SELECT content."Id",
content."CreatedBy",
creator."Name" as CreatedByName,
i."Logo" as CreatedByPortraitUrl,
c."Menu" as ColorMenu,
c."Accent" as ColorAccent,
content."CreatedAt",
content."DeletedBy",
content."DeletedAt",
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"
LEFT JOIN "Content"."Colors" AS c ON creator."Id" = c."CreatorId"
WHERE content."CreatedBy" = '{req.CreatorId}'
AND content."DeletedBy" IS NULL
""");
// var queryBuilder = new StringBuilder();
// queryBuilder.AppendLine($"""
// SELECT content."Id",
// content."CreatedBy",
// creator."Name" as CreatedByName,
// i."Logo" as CreatedByPortraitUrl,
// c."Menu" as ColorMenu,
// c."Accent" as ColorAccent,
// content."CreatedAt",
// content."DeletedBy",
// content."DeletedAt",
// 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"
// LEFT JOIN "Content"."Colors" AS c ON creator."Id" = c."CreatorId"
// LEFT JOIN "Content"."ContentReactions" AS cr ON content."Id" = cr."ContentId"
// WHERE content."CreatedBy" = '{req.CreatorId}'
// AND content."DeletedBy" IS NULL
// """);
//
// if (req.LastId.HasValue)
// {
// queryBuilder.AppendLine($"""AND content."Id" > '{req.LastId.Value}'""");
// }
//
// queryBuilder.AppendLine($"""
// ORDER BY content."CreatedAt" DESC
// LIMIT {req.PageSize}
// """);
//
// var query = queryBuilder.ToString();
//
// var results = await context
// .Database
// .SqlQueryRaw<ContentModel>(query)
// .Include(c => c.Reactions)
// .ToListAsync(cancellationToken: ct);
if (req.LastId.HasValue)
{
queryBuilder.AppendLine($"""AND content."Id" > '{req.LastId.Value}'""");
}
queryBuilder.AppendLine($"""
ORDER BY content."CreatedAt" DESC
LIMIT {req.PageSize}
""");
var content = await context
.Contents
.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()
})
.Where(x => x.CreatedBy == req.CreatorId && x.DeletedAt == null)
.ToListAsync(ct);
var query = queryBuilder.ToString();
var results = await context
.Database
.SqlQueryRaw<ContentModel>(query)
.ToListAsync(cancellationToken: ct);
await SendAsync(results, cancellation: ct);
await SendAsync(content, cancellation: ct);
}
}