Exemplo n.º 1
0
        public async Task <ActionResult <AddCommentResponse> > PostComment(long movieId, AddCommentRequest commentRequest)
        {
            var movie = await _context.Movies.Include(movie => movie.Comments).FirstOrDefaultAsync(movie => movie.Id == movieId);

            User user;

            if (commentRequest.Author != null)
            {
                user = commentRequest.Author;
            }
            else
            {
                user = _context.User.Where(user => user.Id == commentRequest.UserId).First();
            }
            var comment = CommentMapper.mapFormAddCommentRequestToComment(commentRequest);

            comment.Author = user;
            movie.Comments.Add(comment);
            await _context.SaveChangesAsync();

            return(Ok(CommentMapper.mapFromCommentToAddCommentResponse(comment)));
        }