Adds subscriber count

This commit is contained in:
Jonathan Bourdon
2024-08-04 04:35:08 -04:00
parent 0cd913b274
commit 7ec668a822
3 changed files with 76 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
using FastEndpoints;
using FluentValidation;
using Hutopy.Web.Features.Contents.Data;
using Hutopy.Web.Features.Contents.Handlers.Models;
using Microsoft.EntityFrameworkCore;
namespace Hutopy.Web.Features.Contents.Handlers;
@@ -23,7 +24,7 @@ public sealed class GetCreatorByAliasRequestValidator
public class GetCreatorByAliasHandler(
ContentDbContext context)
: Endpoint<GetCreatorByAliasRequest, Creator>
: Endpoint<GetCreatorByAliasRequest, CreatorModel>
{
public override void Configure()
{
@@ -37,16 +38,36 @@ public class GetCreatorByAliasHandler(
CancellationToken ct)
{
var creatorName = req.Name.ToLower();
var creator = await context
.Creators
.SingleOrDefaultAsync(
c => EF.Functions.Like(c.Name, creatorName),
.Where(c => EF.Functions.Like(c.Name, creatorName))
.FirstOrDefaultAsync(ct);
if (creator is null)
{
await SendNotFoundAsync(ct);
}
else
{
var subscriberCount = await context.Subscriptions.CountAsync(
s => s.CreatorId == creator.Id,
cancellationToken: ct);
var creators = await context.Creators.ToListAsync(cancellationToken: ct);
if (creator is null) await SendNotFoundAsync(ct);
else await SendAsync(creator, cancellation: ct);
var model = new CreatorModel
{
Id = creator.Id,
CreatedBy = creator.CreatedBy,
CreatedAt = creator.CreatedAt,
Name = creator.Name,
About = creator.About,
SocialNetworks = creator.SocialNetworks,
ProfileColors = creator.ProfileColors,
StoredDataUrls = creator.StoredDataUrls,
SubscriberCount = subscriberCount,
};
await SendAsync(model, cancellation: ct);
}
}
}