From e2c345a7702636bcb788d8b65b1dd79aa0d2d051 Mon Sep 17 00:00:00 2001 From: Jonathan Bourdon Date: Mon, 5 Aug 2024 22:41:05 -0400 Subject: [PATCH] Adds ChangeLogo for Creators --- .../Features/Contents/Handlers/ChangeLogo.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/Web/Features/Contents/Handlers/ChangeLogo.cs 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); + } +}