Esempio n. 1
0
        public async Task <ActionResult> Create(CommentCreateViewModel model)
        {
            _logger.Info("Creating comment! Params: " + model.ToJson());

            if (!ModelState.IsValid)
            {
                _logger.Info("Invalid comment Form! Errors: " + ModelState.ToJson());
                return(Json(ModelState.ToDictionary()));
            }
            if (!await postsManager.Exists(model.PostId))
            {
                ModelState.AddModelError("PostId", "Постът, който искате да коментирате не съществува.");
                return(Json(ModelState.ToDictionary()));
            }
            try
            {
                var createdComment = await commentsManager.Create(model, User.Identity.GetUserId());

                _logger.Info("Comment created successfully!");

                return(Json(new
                {
                    CreationDate = createdComment.CreationDate.ToString("MMMM dd, yyyy"),
                    UserImageUrl = createdComment.UserImageUrl,
                    Author = createdComment.Author,
                    Body = createdComment.Body,
                    CommentId = createdComment.CommentId
                }));
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Creating comment failed!");
                throw;
            }
        }