Пример #1
0
 public IActionResult UpdateComment(int id, [FromBody] CommentDTO comment)
 {
     try
     {
         _commentService.UpdateComment(id, comment, AuthInfo());
         _logger.LogInformation("User successfully updated a comment");
         return(NoContent());
     }
     catch (ArgumentNullException ex)
     {
         _logger.LogError(ex, ex.Message);
         return(NotFound());
     }
     catch (Exception ex)
     {
         _logger.LogError(ex, "Error occurred while user tried to update comment");
         throw;
     }
 }
Пример #2
0
        public IHttpActionResult PostComment(CommentDTO comment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ClaimsIdentity identity = (ClaimsIdentity)User.Identity;

            identity.Claims.ToDictionary(x => x.Type, x => x.Value).TryGetValue(ClaimTypes.NameIdentifier, out string userId);
            Comment newComment = new Comment
            {
                Content      = comment.Content,
                CreationDate = DateTime.Now,
                PostId       = comment.PostId,
                AuthorId     = new Guid(userId)
            };

            unitOfWork.CommentRepository.Insert(newComment);
            unitOfWork.Save();

            return(Ok(newComment.Id));
        }
Пример #3
0
        public ActionResult AddMasterComment(string comment, string postId)
        {
            object jsonObject;

            if (string.IsNullOrWhiteSpace(comment) ||
                string.IsNullOrWhiteSpace(postId))
            {
                jsonObject = new { status = 500 };
                return(Json(jsonObject));
            }

            CommentDTO commentDTO = new CommentDTO()
            {
                Content = comment,
                PostId  = postId
            };

            bool addSuccessfully = commentService.Add(commentDTO);

            if (!addSuccessfully)
            {
                jsonObject = new { status = 500 };
            }
            else
            {
                CommentModel commentModel = dataMapper.MapCommentDTOToModel(commentDTO);

                jsonObject = new
                {
                    status = 200,
                    data   = commentModel
                };
            }

            return(Json(jsonObject));
        }