From 7ac5b7315b8f6575d00f2c689ce42692ba7d9690 Mon Sep 17 00:00:00 2001 From: PascalMarchesseault <97350299+PascalMarchesseault@users.noreply.github.com> Date: Mon, 30 Dec 2024 23:32:15 -0500 Subject: [PATCH] =?UTF-8?q?Wip=20fonctionnement=20upload=20image=20Pr?= =?UTF-8?q?=C3=A9sentation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Handlers/ChangePresentationInfos.cs | 81 ++++++++++++------- 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/src/Web/Features/Contents/Handlers/ChangePresentationInfos.cs b/src/Web/Features/Contents/Handlers/ChangePresentationInfos.cs index d8b2e79..0136fd1 100644 --- a/src/Web/Features/Contents/Handlers/ChangePresentationInfos.cs +++ b/src/Web/Features/Contents/Handlers/ChangePresentationInfos.cs @@ -1,4 +1,5 @@ -using Hutopy.Web.Features.Contents.Data; +using Hutopy.Web.Common.BlobStorage; +using Hutopy.Web.Features.Contents.Data; namespace Hutopy.Web.Features.Contents.Handlers; @@ -8,29 +9,32 @@ public record ChangePresentationInfosRequest( string? PhoneNumber, string? Email, string? Title, - string? MainImageUrl, string? MainImageText, string? MainVideoText, string? ImagesSubtitle, - string? Image1Url, - string? Image2Url, - string? Image3Url, - string? Image4Url, string? ImagesText, string? VideoSubtitle, string? VideoSubtitleMain, string? VideoUrlMain, string? VideoUrl, - string? VideoText); + string? VideoText, + IFormFile? MainImage, + IFormFile? Image1, + IFormFile? Image2, + IFormFile? Image3, + IFormFile? Image4); +[PublicAPI] public class ChangePresentationInfosHandler( - ContentDbContext context) + ContentDbContext context, + AzureBlobStorage blobStorage) : Endpoint { public override void Configure() { Post("/api/creators/{CreatorId}/presentation-infos"); Options(o => o.WithTags("Creators")); + AllowFileUploads(); } public override async Task HandleAsync( @@ -40,30 +44,51 @@ public class ChangePresentationInfosHandler( var creator = await context .Creators .Include(c => c.PresentationInfos) - .SingleAsync( + .SingleOrDefaultAsync( c => c.Id == request.CreatorId, cancellationToken: ct); - creator.PresentationInfos.PhoneNumber = request.PhoneNumber ?? ""; - creator.PresentationInfos.Email = request.Email ?? ""; - creator.PresentationInfos.Title = request.Title ?? ""; - creator.PresentationInfos.MainImageUrl = request.MainImageUrl ?? ""; - creator.PresentationInfos.MainImageText = request.MainImageText ?? ""; - creator.PresentationInfos.MainVideoText = request.MainVideoText ?? ""; - creator.PresentationInfos.ImagesSubtitle = request.ImagesSubtitle ?? ""; - creator.PresentationInfos.Image1Url = request.Image1Url ?? ""; - creator.PresentationInfos.Image2Url = request.Image2Url ?? ""; - creator.PresentationInfos.Image3Url = request.Image3Url ?? ""; - creator.PresentationInfos.Image4Url = request.Image4Url ?? ""; - creator.PresentationInfos.ImagesText = request.ImagesText ?? ""; - creator.PresentationInfos.VideoSubtitle = request.VideoSubtitle ?? ""; - creator.PresentationInfos.VideoSubtitleMain = request.VideoSubtitleMain ?? ""; - creator.PresentationInfos.VideoUrlMain = request.VideoUrlMain ?? ""; - creator.PresentationInfos.VideoUrl = request.VideoUrl ?? ""; - creator.PresentationInfos.VideoText = request.VideoText ?? ""; - - await context.SaveChangesAsync(ct); + if (creator is null) + { + await SendNotFoundAsync(ct); + return; + } + // Upload images if provided and update URLs + async Task UploadFileAsync(IFormFile? file, string subDirectory, string fileName) + { + if (file is null) + return null; + + return await blobStorage.UploadFileAsync( + ContainerNames.Creators, + $"{request.CreatorId}/{subDirectory}/{fileName}", + file.OpenReadStream(), + file.ContentType, + ct); + } + + creator.PresentationInfos.MainImageUrl = await UploadFileAsync(request.MainImage, "Profile", "MainImage") ?? creator.PresentationInfos.MainImageUrl; + creator.PresentationInfos.Image1Url = await UploadFileAsync(request.Image1, "Profile", "Image1") ?? creator.PresentationInfos.Image1Url; + creator.PresentationInfos.Image2Url = await UploadFileAsync(request.Image2, "Profile", "Image2") ?? creator.PresentationInfos.Image2Url; + creator.PresentationInfos.Image3Url = await UploadFileAsync(request.Image3, "Profile", "Image3") ?? creator.PresentationInfos.Image3Url; + creator.PresentationInfos.Image4Url = await UploadFileAsync(request.Image4, "Profile", "Image4") ?? creator.PresentationInfos.Image4Url; + + // Update other fields + creator.PresentationInfos.PhoneNumber = request.PhoneNumber ?? creator.PresentationInfos.PhoneNumber; + creator.PresentationInfos.Email = request.Email ?? creator.PresentationInfos.Email; + creator.PresentationInfos.Title = request.Title ?? creator.PresentationInfos.Title; + creator.PresentationInfos.MainImageText = request.MainImageText ?? creator.PresentationInfos.MainImageText; + creator.PresentationInfos.MainVideoText = request.MainVideoText ?? creator.PresentationInfos.MainVideoText; + creator.PresentationInfos.ImagesSubtitle = request.ImagesSubtitle ?? creator.PresentationInfos.ImagesSubtitle; + creator.PresentationInfos.ImagesText = request.ImagesText ?? creator.PresentationInfos.ImagesText; + creator.PresentationInfos.VideoSubtitle = request.VideoSubtitle ?? creator.PresentationInfos.VideoSubtitle; + creator.PresentationInfos.VideoSubtitleMain = request.VideoSubtitleMain ?? creator.PresentationInfos.VideoSubtitleMain; + creator.PresentationInfos.VideoUrlMain = request.VideoUrlMain ?? creator.PresentationInfos.VideoUrlMain; + creator.PresentationInfos.VideoUrl = request.VideoUrl ?? creator.PresentationInfos.VideoUrl; + creator.PresentationInfos.VideoText = request.VideoText ?? creator.PresentationInfos.VideoText; + + await context.SaveChangesAsync(ct); await SendOkAsync(ct); } }