Change CreateContent to allows content without images

This commit is contained in:
2024-08-15 12:38:40 -04:00
parent 256d7b7ff7
commit 536e192ff4

View File

@@ -12,7 +12,7 @@ public record PostContentRequest(
Guid CreatorId, Guid CreatorId,
string Title, string Title,
string Description, string Description,
IFormFileCollection Files); IFormFileCollection? Files);
[PublicAPI] [PublicAPI]
public sealed class PostContentRequestValidator : Validator<PostContentRequest> public sealed class PostContentRequestValidator : Validator<PostContentRequest>
@@ -53,38 +53,40 @@ public sealed class PostContent(
PostContentRequest req, PostContentRequest req,
CancellationToken ct) CancellationToken ct)
{ {
var urls = new ConcurrentBag<string>(); var urls = new ConcurrentBag<string>();
await Parallel.ForEachAsync( if (req.Files is not null)
req.Files, {
ct, await Parallel.ForEachAsync(
async (file, ict) => req.Files,
{ ct,
try async (file, ict) =>
{ {
var contentUrl = await SaveFileAsync( try
req.CreatorId, {
req.Id, var contentUrl = await SaveFileAsync(
file, req.CreatorId,
ict); req.Id,
file,
ict);
urls.Add(contentUrl); urls.Add(contentUrl);
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.LogError("{ErrorMessage}", ex.Message); Logger.LogError("{ErrorMessage}", ex.Message);
} }
}); });
}
await context.Contents.AddAsync( await context.Contents.AddAsync(
new() new Content
{ {
Id = req.Id, Id = req.Id,
CreatedBy = User.GetUserId(), CreatedBy = User.GetUserId(),
Title = req.Title, Title = req.Title,
Description = req.Description, Description = req.Description,
Urls = urls.ToArray() Urls = urls.IsEmpty ? null : urls.ToArray()
}, },
ct); ct);