public async Task AddReviewToOffer(CreateReviewModel inputModel) { var offer = await this.context.Offers.FirstOrDefaultAsync(x => x.Id == inputModel.Id); if (offer == null) { return; } var userComments = this.GetOfferCommentsAsync(inputModel.Id).Result.Where(x => x.CreatorId == inputModel.CreatorId).ToList(); var comment = new Comment() { Description = inputModel.Review, OfferId = inputModel.Id, CreatorId = inputModel.CreatorId, }; var rated = await this.offerService.IsOfferRatedAsync(offer.Id, inputModel.CreatorId); int?offerRatedByUser = 0; offerRatedByUser = await this.offerService.GetRateForOffer(offer.Id, inputModel.CreatorId); if (userComments.Any()) { var offerUserRate = await this.context.OfferUserRates .FirstOrDefaultAsync(x => x.UserId == inputModel.CreatorId && x.OfferId == inputModel.Id); offerUserRate.Rate = int.Parse(inputModel.Rating); } OfferUserRate offerRate = null; if (offerRatedByUser == 0) { offerRate = new OfferUserRate() { OfferId = offer.Id, UserId = inputModel.CreatorId, Rate = int.Parse(inputModel.Rating), }; if (offerRatedByUser == 0) { this.context.OfferUserRates.Add(offerRate); } } if (comment.Description != null) { offer.Comments.Add(comment); } await this.context.SaveChangesAsync(); }
public async Task GetUserRatingAsync_WithValidData_ShouldReturnUserRating() { var expected = 4; var guid = Guid.NewGuid().ToString(); var guid2 = Guid.NewGuid().ToString(); var guidOffer = Guid.NewGuid().ToString(); var moqHttpContextAccessor = new Mock <IHttpContextAccessor>(); var moqCategoriesService = new Mock <ICategoryService>(); var moqCloudinaryService = new Mock <ICloudinaryService>(); var moqIFormFile = new Mock <IFormFile>(); var context = InitializeContext.CreateContextForInMemory(); this.userService = new UserService(context, moqHttpContextAccessor.Object); var user1 = new ApplicationUser() { Id = guid, UserName = "******", ProfilePhotoUrl = "TestUrl.com", }; var user2 = new ApplicationUser() { Id = guid2, UserName = "******", ProfilePhotoUrl = "TestUrl2.com", }; var createOfferInputModel = new CreateOfferModel() { Name = "Wow Account", CategotyName = "Wow", CreatorId = guid, Description = "Some Test Description", Price = 10.00, PicUrl = "link", }; this.offerService = new OfferService(context, moqCategoriesService.Object, moqCloudinaryService.Object); var offer = await this.offerService.CreateOfferAsync(createOfferInputModel); var rating1 = new OfferUserRate() { OfferId = offer.Id, Rate = 5, UserId = guid, }; var rating2 = new OfferUserRate() { OfferId = offer.Id, Rate = 3, UserId = guid2, }; context.Users.Add(user1); context.Users.Add(user2); context.OfferUserRates.Add(rating1); context.OfferUserRates.Add(rating2); await context.SaveChangesAsync(); // Assert var userGet = await this.userService.GetUserRatingAsync(guid); Assert.Equal(expected, userGet); }