public async Task <IList <LegacyReview> > GetLegacyReviews() { List <LegacyReview> reviews = new List <LegacyReview>(); IDictionary <int, string> lookup = await _productReviewRepository.LoadLookupAsync(); if (lookup != null) { List <string> productIds = lookup.Values.Distinct().ToList(); int maxProducts = maximumReturnedRecords; int productsCounter = 0; foreach (string productId in productIds) { // Get all results - sort/limit later IList <LegacyReview> returnedReviewList = await _productReviewRepository.GetProductReviewsAsync(productId); if (returnedReviewList.Count > 0) { reviews.AddRange(returnedReviewList); productsCounter++; } else { // Remove any broken lookup references try { List <int> missingIds = new List <int>(); foreach (var lookupPair in lookup) { if (lookupPair.Value == null || lookupPair.Value.Equals(productId)) { missingIds.Add(lookupPair.Key); } } if (missingIds != null) { _context.Vtex.Logger.Warn("GetReviews", null, $"Removing broken lookup ids for product id {productId}\n{string.Join(",", missingIds.ToArray())}"); foreach (int idToRemove in missingIds) { lookup.Remove(idToRemove); } await _productReviewRepository.SaveLookupAsync(lookup); } } catch (Exception ex) { _context.Vtex.Logger.Error("GetReviews", null, $"Error removing broken lookup ids for product id {productId}", ex); } } //if (productsCounter > maxProducts) //{ // break; //} } } return(reviews); }
public async Task <bool> DeleteReviewAsync(int[] ids) { bool retval = true; IDictionary <int, string> lookup = await _productReviewRepository.LoadLookupAsync(); foreach (int id in ids) { if (lookup.TryGetValue(id, out var productId)) { IList <Review> reviews = await _productReviewRepository.GetProductReviewsAsync(productId); Review reviewToRemove = reviews.Where(r => r.Id == id).FirstOrDefault(); if (reviewToRemove != null && reviews.Remove(reviewToRemove)) { await _productReviewRepository.SaveProductReviewsAsync(productId, reviews); } } else { retval = false; } // also remove the reference to the review from the loopup lookup.Remove(id); } await _productReviewRepository.SaveLookupAsync(lookup); return(retval); }