From 536e192ff4393353f140718ab12c132eb43423d7 Mon Sep 17 00:00:00 2001 From: Jonathan Bourdon Date: Thu, 15 Aug 2024 12:38:40 -0400 Subject: [PATCH] Change CreateContent to allows content without images --- .../Contents/Handlers/CreateContent.cs | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/Web/Features/Contents/Handlers/CreateContent.cs b/src/Web/Features/Contents/Handlers/CreateContent.cs index 964e507..999fdd6 100644 --- a/src/Web/Features/Contents/Handlers/CreateContent.cs +++ b/src/Web/Features/Contents/Handlers/CreateContent.cs @@ -12,7 +12,7 @@ public record PostContentRequest( Guid CreatorId, string Title, string Description, - IFormFileCollection Files); + IFormFileCollection? Files); [PublicAPI] public sealed class PostContentRequestValidator : Validator @@ -53,38 +53,40 @@ public sealed class PostContent( PostContentRequest req, CancellationToken ct) { - var urls = new ConcurrentBag(); - await Parallel.ForEachAsync( - req.Files, - ct, - async (file, ict) => - { - try + if (req.Files is not null) + { + await Parallel.ForEachAsync( + req.Files, + ct, + async (file, ict) => { - var contentUrl = await SaveFileAsync( - req.CreatorId, - req.Id, - file, - ict); + try + { + var contentUrl = await SaveFileAsync( + req.CreatorId, + req.Id, + file, + ict); - urls.Add(contentUrl); - } - catch (Exception ex) - { - Logger.LogError("{ErrorMessage}", ex.Message); - } - }); + urls.Add(contentUrl); + } + catch (Exception ex) + { + Logger.LogError("{ErrorMessage}", ex.Message); + } + }); + } await context.Contents.AddAsync( - new() + new Content { Id = req.Id, CreatedBy = User.GetUserId(), Title = req.Title, Description = req.Description, - Urls = urls.ToArray() + Urls = urls.IsEmpty ? null : urls.ToArray() }, ct);