예제 #1
0
        public IActionResult CreateNewBlogPostComment(CreateBlogPostCommentRequest createBlogPostComment)
        {
            try
            {
                var contextBlogPost = _context.BlogPosts.Find(createBlogPostComment?.BlogPost?.ID);

                if (contextBlogPost == null || string.IsNullOrEmpty(createBlogPostComment?.Comment))
                {
                    throw new Exception("Cannot add comment to the blog as the request is invaid.");
                }

                var newComment = new BlogCommentModel
                {
                    ID         = Guid.NewGuid(),
                    BlogPost   = contextBlogPost,
                    Comment    = createBlogPostComment.Comment,
                    IsApproved = true,
                    UserId     = Guid.Parse("A37110A0-12D5-4AB6-89A0-504A335F64E4")
                };

                _context.Add(newComment);
                _context.SaveChanges();

                return(Ok(JsonSerializer.Serialize(newComment)));
            }
            catch (Exception ex)
            {
                _logger.LogError($"An unexpected error occurred retrieving all blog posts for the current user, {ex.Message}{Environment.NewLine}{ex.StackTrace}");
                var i = 0; // Added i to prevent the while loop to accidentally be looping indefinitely.
                while (ex.InnerException != null && i < 10)
                {
                    _logger.LogError(ex.InnerException.StackTrace);
                    ex = ex.InnerException;
                    i++;
                }

                throw new Exception("An unexpected error occurred trying to create the blog post for the current user");
            }
        }
예제 #2
0
 public async Task <IActionResult> CreateBlogPostComment([FromBody] CreateBlogPostCommentRequest request)
 {
     request.UserId = (await userManager.GetUserAsync(User))?.Id;
     return(Ok(await mediator.Send(request)));
 }