Ejemplo n.º 1
0
        public IHttpActionResult GetEditedPost(string postId)
        {
            object  jsonObject;
            PostDTO postDTO = postService.GetPost(postId);

            if (postDTO == null)
            {
                jsonObject = new { status = 500 };
            }
            else
            {
                APIEditedPostModel editedPostModel = dataMapper.MapEditedPostDTOToModel(postDTO);

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

            return(Json(jsonObject));
        }
        public PostDTO GetPost(int id)
        {
            Post           post     = unitOfWork.PostRepository.GetByID(id);
            User           author   = unitOfWork.UserRepository.GetByID(post.AuthorId);
            ClaimsIdentity identity = (ClaimsIdentity)User.Identity;

            identity.Claims.ToDictionary(x => x.Type, x => x.Value).TryGetValue(ClaimTypes.NameIdentifier, out string userId);
            PostDTO newPost = new PostDTO
            {
                Id           = post.Id,
                Title        = post.Title,
                Content      = post.Content,
                CreationDate = post.CreationDate,
                IsAuthor     = userId != null ? new Guid(userId) == post.Author.Id : false,
                Author       = new AuthorDTO
                {
                    UserName = post.Author.UserName,
                    Id       = post.Author.Id,
                    Email    = post.Author.Email
                }
            };

            return(newPost);
        }