Beispiel #1
0
        public IActionResult GetBlog(int userId, int blogId,
                                     [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType)
        {
            if (!_weblogDataRepository.UserExists(userId))
            {
                return(NotFound());
            }

            var blogEntity = _weblogDataRepository.GetBlog(blogId);

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

            var blogToReturn = _mapper.Map <BlogDto>(blogEntity);

            var includeLinks = MediaTypes.IncludeLinks(mediaType);

            if (!includeLinks)
            {
                return(Ok(blogToReturn));
            }

            var links         = CreateLinksForBlog(Url, userId, blogId);
            var blogWithLinks = new BlogDtoWithLinks(blogToReturn, links);

            return(Ok(blogWithLinks));
        }
Beispiel #2
0
        public IActionResult CreateBlog(int userId,
                                        [FromBody] BlogForManipulationDto blog,
                                        [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType)
        {
            if (!_weblogDataRepository.UserExists(userId))
            {
                return(NotFound());
            }

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

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

            var blogEntity = _mapper.Map <Entities.Blog>(blog);

            _weblogDataRepository.AddBlog(userId, blogEntity);
            _weblogDataRepository.Save();

            var blogToReturn = _mapper.Map <BlogDto>(blogEntity);

            var includeLinks = MediaTypes.IncludeLinks(mediaType);

            if (!includeLinks)
            {
                return(CreatedAtRoute(nameof(GetBlog),
                                      new { userId, blogId = blogToReturn.BlogId },
                                      blogToReturn));
            }

            var links         = CreateLinksForBlog(Url, userId, blogToReturn.BlogId);
            var blogWithLinks = new BlogDtoWithLinks(blogToReturn, links);

            return(CreatedAtRoute(nameof(GetBlog),
                                  new { userId, blogId = blogToReturn.BlogId },
                                  blogWithLinks));
        }