Esempio n. 1
0
        public IActionResult UpdatePost(int userId, int blogId, int postId,
                                        [FromBody] PostForManipulationDto post)
        {
            if (!_weblogDataRepository.UserExists(userId) ||
                !_weblogDataRepository.BlogExists(blogId))
            {
                return(NotFound());
            }

            var emailAddress = post.Credentials.EmailAddress;
            var password     = post.Credentials.Password;

            if (!_weblogDataRepository.Authorized(userId, emailAddress, password))
            {
                return(Unauthorized());
            }

            var postFromRepo = _weblogDataRepository.GetPost(postId);

            if (postFromRepo is null)
            {
                return(NotFound());
            }

            _mapper.Map(post, postFromRepo);

            _weblogDataRepository.UpdatePost(postFromRepo);
            _weblogDataRepository.Save();

            return(NoContent());
        }
Esempio n. 2
0
        public IActionResult CreatePost(int userId, int blogId,
                                        [FromBody] PostForManipulationDto post,
                                        [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType)
        {
            if (!_weblogDataRepository.UserExists(userId) ||
                !_weblogDataRepository.BlogExists(blogId))
            {
                return(NotFound());
            }

            var emailAddress = post.Credentials.EmailAddress;
            var password     = post.Credentials.Password;

            if (!_weblogDataRepository.Authorized(userId, emailAddress, password))
            {
                return(Unauthorized());
            }

            var postEntity = _mapper.Map <Entities.Post>(post);

            _weblogDataRepository.AddPost(blogId, postEntity);
            _weblogDataRepository.Save();

            var postToReturn = _mapper.Map <PostDto>(postEntity);

            var includeLinks = MediaTypes.IncludeLinks(mediaType);

            if (!includeLinks)
            {
                return(CreatedAtRoute(nameof(GetPost),
                                      new { userId, blogId, postId = postToReturn.PostId },
                                      postToReturn));
            }

            var links         = CreateLinksForPost(Url, userId, blogId, postToReturn.PostId);
            var postWithLinks = new PostDtoWithLinks(postToReturn, links);

            return(CreatedAtRoute(nameof(GetPost),
                                  new { userId, blogId, postId = postToReturn.PostId },
                                  postWithLinks));
        }