Ejemplo n.º 1
0
        public async Task <IActionResult> CreateOrReplaceReview([FromRoute(Name = "venueId")] int venueId, [FromBody] UpdatedReview request)
        {
            var existingReviewResult = await reviews.FetchVenueUserReview(venueId, this.GetAuthContext().User.Value.UserId).ConfigureAwait(false);

            if (existingReviewResult.IsFailure)
            {
                return(StatusCode(500));
            }

            var    existingReview = existingReviewResult.Value;
            Review review;
            var    approved = false;

            if (existingReview.HasValue)
            {
                review = existingReview.Value;
                var existingSummary = review.ReviewSummary ?? "";
                var existingDetail  = review.ReviewDetail ?? "";
                approved = existingSummary.Equals(request.ReviewSummary, StringComparison.Ordinal) &&
                           existingDetail.Equals(request.ReviewDetail, StringComparison.Ordinal);
            }
            else
            {
                review = new Review
                {
                    VenueId            = venueId,
                    UserId             = this.GetAuthContext().User.Value.UserId,
                    ReviewSummary      = request.ReviewSummary,
                    ReviewDetail       = request.ReviewDetail,
                    Rating             = request.Rating,
                    RegisteredInterest = request.RegisteredInterest,
                    Approved           = approved,
                    Updated            = DateTime.UtcNow,
                };
            }

            return(await reviews.CreateOrReplaceReview(review)
                   .OnBoth(r => r.IsFailure ? StatusCode(500) : StatusCode(200))
                   .ConfigureAwait(false));
        }