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 ChangeBannerRequest( Guid CreatorId, IFormFile File); public class ChangeBannerHandler( IHttpContextAccessor contextAccessor, ContentDbContext context, IBlobStorage blobStorage) : Endpoint { public override void Configure() { Post("/api/creators/{CreatorId}/banner"); Options(o => o.WithTags("Contents")); AllowFileUploads(); } public override async Task HandleAsync(ChangeBannerRequest request, CancellationToken ct) { var creator = await context .Creators .Include(c => c.Images) .SingleOrDefaultAsync( c => c.Id == request.CreatorId, cancellationToken: ct); if (creator is null) { await SendNotFoundAsync(ct); return; } // TODO: this upload should be done to the Creators container var blobUrl = await blobStorage.UploadFileAsync( ContainerNames.Users, $"{request.CreatorId}/{SubDirectoryNames.Profile}/{CommonFileNames.BannerPicture}", request.File.OpenReadStream(), request.File.ContentType, ct); creator.Images.Banner = blobUrl; await context.SaveChangesAsync(ct); await SendOkAsync(blobUrl, ct); } }