Files
social-media/backend/src/Web/Features/Contents/Handlers/GetCreatorByAlias.cs
2025-02-07 15:44:59 -05:00

97 lines
2.6 KiB
C#

using Hutopy.Web.Features.Contents.Data;
namespace Hutopy.Web.Features.Contents.Handlers;
[PublicAPI]
public sealed class GetCreatorByAliasRequest
{
public required string Name { get; set; }
}
[PublicAPI]
public class GetCreatorByAliasResponse(
Guid id,
Guid createdBy,
DateTimeOffset createdAt,
bool verified,
bool acceptDonation,
string name,
string? title,
Socials socials,
Colors colors,
PresentationInfos presentationInfos,
Images images)
{
public Guid Id { get; } = id;
public Guid CreatedBy { get; } = createdBy;
public DateTimeOffset CreatedAt { get; } = createdAt;
public bool Verified { get; } = verified;
public bool AcceptDonation { get; } = acceptDonation;
public string Name { get; } = name;
public string? Title { get; } = title;
public Socials Socials { get; } = socials;
public Colors Colors { get; } = colors;
public PresentationInfos PresentationInfos { get; } = presentationInfos;
public Images Images { get; } = images;
}
[UsedImplicitly]
public sealed class GetCreatorByAliasRequestValidator
: Validator<GetCreatorByAliasRequest>
{
public GetCreatorByAliasRequestValidator()
{
RuleFor(r => r.Name)
.NotNull().WithMessage("You should specify the Name")
.NotEmpty().WithMessage("You should specify a valid/not empty Name");
}
}
[PublicAPI]
public class GetCreatorByAliasHandler(
ContentDbContext context)
: Endpoint<GetCreatorByAliasRequest, GetCreatorByAliasResponse>
{
public override void Configure()
{
Get("/api/creators/@{Name}");
Options((o => o.WithTags("Creators")));
AllowAnonymous();
}
public override async Task HandleAsync(
GetCreatorByAliasRequest req,
CancellationToken ct)
{
var creatorName = req.Name.ToLower();
var creator = await context
.Creators
.Where(c => EF.Functions.ILike(c.Slugs.Name, creatorName))
.AsNoTracking()
.Select(c => new GetCreatorByAliasResponse
(
c.Id,
c.CreatedBy,
c.CreatedAt,
c.Verified,
c.AcceptDonation,
c.Slugs.NormalizedName,
c.Title,
c.Socials,
c.Colors,
c.PresentationInfos,
c.Images))
.SingleOrDefaultAsync(ct);
if (creator is null)
{
await SendNotFoundAsync(ct);
}
else
{
await SendAsync(creator, cancellation: ct);
}
}
}