From 45cfa148e6199d546f0bfb061aee89f817fdbb4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=9A=D0=BE?= =?UTF-8?q?=D0=BB=D0=BE=D1=81=D0=BE=D0=B2?= Date: Fri, 10 Jan 2025 17:20:40 +0100 Subject: [PATCH] VCST-2523: Some improvements --- .../Commands/CreateReviewCommandHandler.cs | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/VirtoCommerce.CustomerReviews.ExperienceApi/Commands/CreateReviewCommandHandler.cs b/src/VirtoCommerce.CustomerReviews.ExperienceApi/Commands/CreateReviewCommandHandler.cs index 9735c95..0f3f1ff 100644 --- a/src/VirtoCommerce.CustomerReviews.ExperienceApi/Commands/CreateReviewCommandHandler.cs +++ b/src/VirtoCommerce.CustomerReviews.ExperienceApi/Commands/CreateReviewCommandHandler.cs @@ -69,20 +69,33 @@ public async Task Handle(CreateReviewCommand request, Cancel review.UserName = contact?.FullName ?? currentUser.UserName; } + IList files = null; + if (!request.ImageUrls.IsNullOrEmpty()) + { + files = await SaveImages(request, review); + } + await _reviewService.SaveChangesAsync([review]); response.Id = review.Id; response.UserName = review.UserName; - await SaveImages(request, review); + if (files.Count > 0) + { + foreach (var file in files) + { + file.OwnerEntityId = review.Id; + file.OwnerEntityType = nameof(CustomerReview); + } - await _reviewService.SaveChangesAsync([review]); + await _fileUploadService.SaveChangesAsync(files); + } } return response; } - protected virtual async Task SaveImages(CreateReviewCommand request, CustomerReview review) + protected virtual async Task> SaveImages(CreateReviewCommand request, CustomerReview review) { var files = await GetFiles(request.ImageUrls); var filesByUrls = files @@ -101,17 +114,11 @@ protected virtual async Task SaveImages(CreateReviewCommand request, CustomerRev if (filesByUrls.TryGetValue(url, out var file)) { review.Images.Add(ConvertToReviewImage(file)); - - file.OwnerEntityId = review.Id; - file.OwnerEntityType = nameof(CustomerReview); files.Add(file); } } - if (files.Count > 0) - { - await _fileUploadService.SaveChangesAsync(files); - } + return files; } protected virtual async Task> GetFiles(IEnumerable urls)