public void Edit(EditReviewDto dto, string currentUserId)
        {
            UserManager.IsUserInRegularRole(currentUserId);

            var validationResult = _validationManager.ValidateOperateReviewDto(dto);

            if (validationResult.HasErrors)
            {
                throw new BusinessFaultException(validationResult.GetErrors());
            }

            var review = UnitOfWork.Repository <ICarServiceReviewRepository>().Get(dto.ReviewId);

            if (review == null)
            {
                throw new BusinessFaultException(BusinessLogicExceptionResources.ReviewNotFound);
            }

            if (review.User.ApplicationUser.Id != currentUserId)
            {
                throw new BusinessFaultException(BusinessLogicExceptionResources.ReviewDoesNotBelongToUser);
            }

            // Убираем очки от старого отзыва и добавляем новые
            review.Service.Points -= review.Mark;
            review.Service.Points += dto.Mark;

            review.Mark    = dto.Mark;
            review.Review  = dto.Review;
            review.Updated = DateTime.UtcNow;

            UnitOfWork.SaveChanges();
        }
        public void Edit_ChangesProperties()
        {
            // Arrange
            var context = this.ServiceProvider.GetRequiredService <WmipDbContext>();
            var review  = new Review {
                Id = 1, Title = "rrrr"
            };

            context.Reviews.Add(review);
            context.SaveChanges();
            var reviewsService = new ReviewsService(context);
            var editInfo       = new EditReviewDto()
            {
                Id = 1, Title = "rev1"
            };

            // Act
            reviewsService.Edit(editInfo);

            //Assert
            Assert.Equal(editInfo.Title, context.Reviews.First().Title);
        }
 public bool Edit(EditReviewDto editInfo)
 {
     try
     {
         var review = this.context.Reviews.Find(editInfo.Id);
         if (review == null)
         {
             return(false);
         }
         review.Title       = editInfo.Title;
         review.Body        = editInfo.Body;
         review.Summary     = editInfo.Summary;
         review.ReviewScore = editInfo.ReviewScore;
         review.ReviewType  = editInfo.ReviewType;
         this.context.Reviews.Update(review);
         this.context.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Exemplo n.º 4
0
        public async Task <IActionResult> UpdateOneReview([FromRoute] Guid experienceId, [FromRoute] Guid id, [FromBody] EditReviewDto updatedReview)
        {
            // find the review to update
            var reviewFromDb = await _reviewRepository.GetAReviewAsync(id);

            // if review does not exist return error
            if (reviewFromDb == null)
            {
                var err = new ErrorObject()
                {
                    Method     = "PUT",
                    At         = $"/api/experiences/{experienceId}/reviews/{id}",
                    StatusCode = 404,
                    Error      = "Could not find review to update"
                };
                return(NotFound(err));
            }

            // if you are a user, you can only update your own review
            if (User.IsInRole(Roles.User))
            {
                var profileId = User.Claims.FirstOrDefault(x => x.Type == "profileId").Value;
                if (Guid.Parse(profileId) != reviewFromDb.ProfileId)
                {
                    var err = new ErrorObject()
                    {
                        Method     = "PUT",
                        At         = $"/api/experiences/{experienceId}/reviews/{id}",
                        StatusCode = 401,
                        Error      = "Unauthorized to perform this action"
                    };
                    return(Unauthorized(err));
                }
            }

            // map the changes to the found review object
            _mapper.Map(updatedReview, reviewFromDb);

            reviewFromDb.ModifiedOn = DateTime.Now;

            // update the entity in the database
            _reviewRepository.Update(reviewFromDb);

            if (!await _reviewRepository.SaveAsync())
            {
                var errMsg = "Error updating review";
                _logger.Error(errMsg);
                var err = new ErrorObject()
                {
                    Method     = "PUT",
                    At         = $"*/api/experiences/{experienceId}/reviews/{id}",
                    StatusCode = 500,
                    Error      = errMsg
                };
                return(StatusCode(500, err));
            }
            return(NoContent());
        }
Exemplo n.º 5
0
 public IHttpActionResult Edit(EditReviewDto dto)
 {
     return(CallBusinessLogicAction(() => _manager.Edit(dto, User.Identity.GetUserId())));
 }