Ejemplo n.º 1
0
        public async Task <IActionResult> PostReview(ReviewPost model)
        {
            if (!await Services.Context.Ratings.AnyAsync(x => x.RatingId.Equals(model.RatingId)))
            {
                ModelState.AddModelError(nameof(model.RatingId), Message.NotFound.ToString());
            }

            if (!await Services.Context.Services.AnyAsync(x => x.ServiceId == model.ServiceId.Value))
            {
                ModelState.AddModelError(nameof(model.ServiceId), Message.NotFound.ToString());
            }

            if (!await Services.Context.Users.AnyAsync(x => x.UserId.Equals(model.UserId)))
            {
                ModelState.AddModelError(nameof(model.UserId), Message.NotFound.ToString());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var entity = model.AsEntity();
            await Services.Context.Reviews.AddAsync(entity);

            await Services.Context.SaveChangesAsync();

            entity = await Services.Reviews.Get(entity.ReviewId);

            return(CreatedAtAction(nameof(GetReview), new { id = entity.ReviewId }, entity.AsModel()));
        }