public async void Reviews_RemoveAllAsync_Test() { var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>() .UseInMemoryDatabase(databaseName: "Reviews_RemoveAllAsync_Test") .Options; using (var context = new DreamFoodDeliveryContext(options)) { context.AddRange(_reviews); int sum = 0; for (int i = 0; i < _reviews.Count; i++) { sum += _reviews[i].Rating.Value; } RatingDB rating = new RatingDB() { Sum = sum, Count = _reviews.Count }; context.Add(rating); await context.SaveChangesAsync(); } using (var context = new DreamFoodDeliveryContext(options)) { var service = new ReviewService(_mapper, context); var resultPositive = await service.RemoveAllAsync(); resultPositive.IsSuccess.Should().BeTrue(); resultPositive.Message.Should().BeNull(); } }
public async void Reviews_UpdateAsync_PositiveAndNegative_Test() { var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>() .UseInMemoryDatabase(databaseName: "Reviews_UpdateAsync_PositiveAndNegative_Test") .Options; using (var context = new DreamFoodDeliveryContext(options)) { context.AddRange(_reviews); int sum = 0; for (int i = 0; i < _reviews.Count; i++) { sum += _reviews[i].Rating.Value; } RatingDB rating = new RatingDB() { Sum = sum, Count = _reviews.Count }; context.Add(rating); await context.SaveChangesAsync(); } using (var context = new DreamFoodDeliveryContext(options)) { var review = await context.Reviews.AsNoTracking().FirstOrDefaultAsync(); var rating = await context.Rating.AsNoTracking().FirstOrDefaultAsync(); var service = new ReviewService(_mapper, context); ReviewToUpdate updateReview = new ReviewToUpdate() { Id = review.Id.ToString(), Headline = "Headline", Content = "Content", Rating = 1 }; ReviewToUpdate failReview = new ReviewToUpdate() { Id = new Guid().ToString(), Headline = "Headline", Content = "Content", Rating = 0 }; var resultPositive = await service.UpdateAsync(updateReview); var resultNegative = await service.UpdateAsync(failReview); resultPositive.IsSuccess.Should().BeTrue(); resultPositive.Data.Headline.Should().BeEquivalentTo(updateReview.Headline); resultPositive.Data.Headline.Should().NotBeEquivalentTo(review.Headline); resultNegative.IsSuccess.Should().BeFalse(); } }
public async void Reviews_RemoveAllByUserIdAsync_PositiveAndNegative_Test() { var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>() .UseInMemoryDatabase(databaseName: "Reviews_RemoveAllByUserIdAsync_PositiveAndNegative_Test") .Options; UserDB userWithReviews; using (var context = new DreamFoodDeliveryContext(options)) { context.AddRange(_users); await context.SaveChangesAsync(); userWithReviews = context.Users.AsNoTracking().FirstOrDefault(); foreach (var review in _reviews) { review.UserId = userWithReviews.Id; review.OrderId = new Guid(); } int sum = 0; for (int i = 0; i < _reviews.Count; i++) { sum += _reviews[i].Rating.Value; } RatingDB rating = new RatingDB() { Sum = sum, Count = _reviews.Count }; context.Add(rating); context.AddRange(_reviews); await context.SaveChangesAsync(); } using (var context = new DreamFoodDeliveryContext(options)) { //var review = await context.Reviews.AsNoTracking().FirstOrDefaultAsync(); var service = new ReviewService(_mapper, context); var resultPositive = await service.RemoveAllByUserIdAsync(userWithReviews.Id.ToString()); var resultNegative = await service.RemoveAllByUserIdAsync(new Guid().ToString()); resultPositive.IsSuccess.Should().BeTrue(); resultPositive.Message.Should().BeNull(); resultNegative.IsSuccess.Should().BeFalse(); resultNegative.Message.Should().Contain(ExceptionConstants.REVIEWS_WERE_NOT_FOUND); } }
public async Task <Result <ReviewView> > UpdateAsync(ReviewToUpdate review, CancellationToken cancellationToken = default) { ReviewDB reviewForUpdate = _mapper.Map <ReviewDB>(review); ReviewDB reviewOld = await _context.Reviews.AsNoTracking().FirstOrDefaultAsync(_ => _.Id == Guid.Parse(review.Id)); if (reviewOld is null) { return(Result <ReviewView> .Quite <ReviewView>(ExceptionConstants.REVIEW_WAS_NOT_FOUND)); } var rating = await _context.Rating.AsNoTracking().FirstOrDefaultAsync(cancellationToken); if (rating is null) { rating = new RatingDB() { Count = 0, Sum = 0 }; _context.Rating.Add(rating); } rating.Sum = rating.Sum.Value - reviewOld.Rating + reviewForUpdate.Rating; _context.Entry(rating).Property(c => c.Sum).IsModified = true; reviewForUpdate.Id = Guid.Parse(review.Id); reviewForUpdate.ModificationTime = DateTime.Now; _context.Entry(reviewForUpdate).Property(c => c.Headline).IsModified = true; _context.Entry(reviewForUpdate).Property(c => c.Rating).IsModified = true; _context.Entry(reviewForUpdate).Property(c => c.Content).IsModified = true; _context.Entry(reviewForUpdate).Property(c => c.ModificationTime).IsModified = true; try { await _context.SaveChangesAsync(); var reviewAfterUpdate = await _context.Reviews.Where(_ => _.Id == Guid.Parse(review.Id)).AsNoTracking().FirstOrDefaultAsync(cancellationToken); return(reviewAfterUpdate is null ? Result <ReviewView> .Quite <ReviewView>(ExceptionConstants.REVIEW_WAS_NOT_FOUND) : Result <ReviewView> .Ok(_mapper.Map <ReviewView>(reviewAfterUpdate))); } catch (DbUpdateConcurrencyException ex) { return(Result <ReviewView> .Fail <ReviewView>(ExceptionConstants.CANNOT_UPDATE_MODEL + ex.Message)); } catch (DbUpdateException ex) { return(Result <ReviewView> .Fail <ReviewView>(ExceptionConstants.CANNOT_UPDATE_MODEL + ex.Message)); } }
public async Task <Result <ReviewView> > AddAsync(ReviewToAdd review, string userIdFromIdentity, CancellationToken cancellationToken = default) { UserDB userDB = await _context.Users.Where(_ => _.IdFromIdentity == userIdFromIdentity).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken); var reviewToAdd = _mapper.Map <ReviewDB>(review); reviewToAdd.UserId = userDB.Id; reviewToAdd.OrderId = Guid.Parse(review.OrderId); _context.Reviews.Add(reviewToAdd); var rating = await _context.Rating.FirstOrDefaultAsync(cancellationToken); if (rating is null) { rating = new RatingDB() { Count = 0, Sum = 0 }; _context.Rating.Add(rating); } rating.Sum += reviewToAdd.Rating; rating.Count++; _context.Entry(rating).Property(c => c.Sum).IsModified = true; _context.Entry(rating).Property(c => c.Count).IsModified = true; try { await _context.SaveChangesAsync(cancellationToken); ReviewDB reviewAfterAdding = await _context.Reviews.Where(_ => _.Id == reviewToAdd.Id).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken); return(Result <ReviewView> .Ok(_mapper.Map <ReviewView>(reviewAfterAdding))); } catch (DbUpdateConcurrencyException ex) { return(Result <ReviewView> .Fail <ReviewView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } catch (DbUpdateException ex) { return(Result <ReviewView> .Fail <ReviewView>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } catch (ArgumentNullException ex) { return(Result <ReviewView> .Fail <ReviewView>(ExceptionConstants.SOURCE_IS_NULL + ex.Message)); } }
/// <summary> /// Delete ratings data /// </summary> /// <param name="reviews"></param> /// <param name="cancellationToken"></param> /// <returns></returns> private async Task <Result> RatingDown(List <ReviewDB> reviews, CancellationToken cancellationToken = default) { RatingDB rating = await _context.Rating.FirstOrDefaultAsync(cancellationToken); if (rating is null) { rating = new RatingDB() { Count = 0, Sum = 0 }; _context.Rating.Add(rating); } int count = reviews.Count; int sum = 0; for (int i = 0; i < reviews.Count; i++) { sum += reviews[i].Rating.Value; } rating.Count -= count; rating.Sum -= sum; if (rating.Count < 0 || rating.Sum < 0) { return(Result.Quite()); } _context.Entry(rating).Property(c => c.Sum).IsModified = true; _context.Entry(rating).Property(c => c.Count).IsModified = true; try { await _context.SaveChangesAsync(cancellationToken); return(Result.Ok()); } catch (DbUpdateConcurrencyException ex) { return(Result.Fail(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } catch (DbUpdateException ex) { return(Result.Fail(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } }
public async Task <Result> RemoveByIdAsync(string reviewId, CancellationToken cancellationToken = default) { Guid id = Guid.Parse(reviewId); var review = await _context.Reviews.IgnoreQueryFilters().FirstOrDefaultAsync(_ => _.Id == id); if (review is null) { return(await Task.FromResult(Result.Quite(ExceptionConstants.REVIEW_WAS_NOT_FOUND))); } var rating = await _context.Rating.FirstOrDefaultAsync(cancellationToken); if (rating is null) { rating = new RatingDB() { Count = 0, Sum = 0 }; _context.Rating.Add(rating); } rating.Sum -= review.Rating; rating.Count--; _context.Entry(rating).Property(c => c.Sum).IsModified = true; _context.Entry(rating).Property(c => c.Count).IsModified = true; try { _context.Reviews.Remove(review); await _context.SaveChangesAsync(cancellationToken); return(await Task.FromResult(Result.Ok())); } catch (DbUpdateConcurrencyException ex) { return(await Task.FromResult(Result.Fail(ExceptionConstants.CANNOT_DELETE_REVIEW + ex.Message))); } catch (DbUpdateException ex) { return(await Task.FromResult(Result.Fail(ExceptionConstants.CANNOT_DELETE_REVIEW + ex.Message))); } }