Wip fonctionnement upload image Présentation

This commit is contained in:
PascalMarchesseault
2024-12-30 23:32:15 -05:00
parent 2c4f954af9
commit 7ac5b7315b

View File

@@ -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<ChangePresentationInfosRequest>
{
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<string?> 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);
}
}