Example #1
0
 public async Task <IHttpActionResult> SaveCustomerReviewEvaluation([FromBody] CustomerReviewEvaluation evaluation)
 {
     using (await AsyncLock.GetLockByKey(GetAsyncLockCustomerReviewKey(evaluation.CustomerReviewId)).LockAsync())
     {
         _customerReviewEvaluationService.SaveEvaluation(evaluation);
     }
     return(StatusCode(HttpStatusCode.NoContent));
 }
        public virtual CustomerReviewEvaluationEntity FromModel(CustomerReviewEvaluation evaluation)
        {
            if (evaluation == null)
            {
                throw new ArgumentNullException(nameof(evaluation));
            }

            Id           = evaluation.Id;
            CreatedBy    = evaluation.CreatedBy;
            CreatedDate  = evaluation.CreatedDate;
            ModifiedBy   = evaluation.ModifiedBy;
            ModifiedDate = evaluation.ModifiedDate;

            CustomerReviewId = evaluation.CustomerReviewId;
            CustomerId       = evaluation.CustomerId;
            ReviewIsLiked    = evaluation.ReviewIsLiked;

            return(this);
        }
        public virtual CustomerReviewEvaluation ToModel(CustomerReviewEvaluation evaluation)
        {
            if (evaluation == null)
            {
                throw new ArgumentNullException(nameof(evaluation));
            }

            evaluation.Id           = Id;
            evaluation.CreatedBy    = CreatedBy;
            evaluation.CreatedDate  = CreatedDate;
            evaluation.ModifiedBy   = ModifiedBy;
            evaluation.ModifiedDate = ModifiedDate;

            evaluation.CustomerReviewId = CustomerReviewId;
            evaluation.CustomerId       = CustomerId;
            evaluation.ReviewIsLiked    = ReviewIsLiked;


            return(evaluation);
        }
        public virtual void SaveEvaluation(CustomerReviewEvaluation evaluation)
        {
            if (evaluation == null)
            {
                throw new ArgumentNullException(nameof(evaluation));
            }

            using (var repository = _repositoryFactory())
                using (var changeTracker = base.GetChangeTracker(repository))
                {
                    var customerReviewEntity = repository.CustomerReviews.FirstOrDefault(x => x.Id == evaluation.CustomerReviewId);

                    if (customerReviewEntity == null)
                    {
                        throw new InvalidOperationException($"Customer review with such Id={evaluation.CustomerReviewId} was not found");
                    }

                    var targetEntity = repository.CustomerReviewEvaluations
                                       .Where(x => x.CustomerReviewId == evaluation.CustomerReviewId && x.CustomerId == evaluation.CustomerId)
                                       .FirstOrDefault();

                    var sourceEntity = AbstractTypeFactory <CustomerReviewEvaluationEntity>
                                       .TryCreateInstance()
                                       .FromModel(evaluation);


                    if ((targetEntity != null))
                    {
                        if (targetEntity.ReviewIsLiked != sourceEntity.ReviewIsLiked)
                        {
                            changeTracker.Attach(targetEntity);
                            sourceEntity.Patch(targetEntity);
                            //like counter changing
                            if (sourceEntity.ReviewIsLiked)
                            {
                                customerReviewEntity.LikeCount++;
                                customerReviewEntity.DislikeCount--;
                            }
                            else
                            {
                                customerReviewEntity.DislikeCount++;
                                customerReviewEntity.LikeCount--;
                            }
                        }
                    }
                    else
                    {
                        repository.Add(sourceEntity);

                        //like counter changing
                        if (sourceEntity.ReviewIsLiked)
                        {
                            customerReviewEntity.LikeCount++;
                        }
                        else
                        {
                            customerReviewEntity.DislikeCount++;
                        }
                    }

                    CommitChanges(repository);
                }
        }