diff --git a/src/Web/Features/Contents/Handlers/ChangeLogo.cs b/src/Web/Features/Contents/Handlers/ChangeLogo.cs new file mode 100644 index 0000000..1d3d39e --- /dev/null +++ b/src/Web/Features/Contents/Handlers/ChangeLogo.cs @@ -0,0 +1,54 @@ +using FastEndpoints; +using Hutopy.Application.AzureBlobStorage.Constants; +using Hutopy.Application.Common.Interfaces; +using Hutopy.Web.Features.Contents.Data; +using Microsoft.EntityFrameworkCore; + +namespace Hutopy.Web.Features.Contents.Handlers; + +public record ChangeLogoRequest( + Guid CreatorId, + IFormFile File); + +public class ChangeLogoHandler( + IHttpContextAccessor contextAccessor, + ContentDbContext context, + IBlobStorage blobStorage) + : Endpoint +{ + public override void Configure() + { + Post("/api/creators/{CreatorId}/logo"); + Options(o => o.WithTags("Contents")); + AllowFileUploads(); + } + + public override async Task HandleAsync(ChangeLogoRequest request, CancellationToken ct) + { + var creator = await context + .Creators + .Include(c => c.StoredDataUrls) + .SingleOrDefaultAsync( + c => c.Id == request.CreatorId, + cancellationToken: ct); + + if (creator is null) + { + await SendNotFoundAsync(ct); + return; + } + + var blobUrl = await blobStorage.UploadFileAsync( + ContainerNames.Creators, + $"{request.CreatorId}/{SubDirectoryNames.Profile}/{CommonFileNames.ProfilePicture}", + request.File.OpenReadStream(), + request.File.ContentType, + ct); + + creator.StoredDataUrls.ProfilePictureUrl = blobUrl; + + await context.SaveChangesAsync(ct); + + await SendOkAsync(blobUrl, ct); + } +}