コード例 #1
0
ファイル: CommentsController.cs プロジェクト: shakami/Weblog
        public IActionResult CreateComment(int userId, int blogId, int postId,
                                           [FromBody] CommentForCreationDto comment,
                                           [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType)
        {
            if (!_weblogDataRepository.UserExists(userId) ||
                !_weblogDataRepository.BlogExists(blogId) ||
                !_weblogDataRepository.PostExists(postId))
            {
                return(NotFound());
            }

            if (!_weblogDataRepository.UserExists((int)comment.UserId))
            {
                // adding comment with userId that doesn't exist
                ModelState.AddModelError(nameof(comment.UserId),
                                         "UserId does not exist.");
                return(ErrorHandler.UnprocessableEntity(ModelState, HttpContext));
            }

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

            if (!_weblogDataRepository.Authorized((int)comment.UserId, emailAddress, password))
            {
                return(Unauthorized());
            }

            var commentEntity = _mapper.Map <Entities.Comment>(comment);

            _weblogDataRepository.AddComment(postId, commentEntity);
            _weblogDataRepository.Save();

            var commentToReturn = _mapper.Map <CommentDto>(commentEntity);

            var includeLinks = MediaTypes.IncludeLinks(mediaType);

            if (!includeLinks)
            {
                return(CreatedAtRoute
                       (
                           nameof(GetComment),
                           new { userId, blogId, postId, commentId = commentToReturn.CommentId },
                           commentToReturn
                       ));
            }

            var links = CreateLinksForComment(userId, blogId, postId,
                                              commentToReturn.CommentId, commentToReturn.UserId);

            var commentWithLinks = new CommentDtoWithLinks(commentToReturn, links);

            return(CreatedAtRoute
                   (
                       nameof(GetComment),
                       new { userId, blogId, postId, commentId = commentToReturn.CommentId },
                       commentWithLinks
                   ));
        }
コード例 #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));
        }
コード例 #3
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));
        }
コード例 #4
0
        public IActionResult UpdateUser(int userId,
                                        [FromBody] UserForUpdateDto user)
        {
            var emailAddress = user.Credentials.EmailAddress;
            var password     = user.Credentials.Password;

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

            var userFromRepo = _weblogDataRepository.GetUser(userId);

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

            _mapper.Map(user, userFromRepo);

            _weblogDataRepository.UpdateUser(userFromRepo);

            try
            {
                _weblogDataRepository.Save();
            }
            catch (ApplicationException ex)
            {
                // changing user with email address that already exists
                ModelState.AddModelError(nameof(user.EmailAddress),
                                         ex.Message + "\n" + ex?.InnerException.Message);

                return(ErrorHandler.UnprocessableEntity(ModelState, HttpContext));
            }

            return(NoContent());
        }
コード例 #5
0
        public void Authorized()
        {
            //-- arrange
            var user = new User
            {
                FirstName    = "fname",
                LastName     = "lname",
                EmailAddress = "email@users",
                Password     = "******"
            };

            _repository.AddUser(user);
            _repository.Save();

            //-- act
            var actual = _repository.Authorized(1, "email@users", "secret");

            //-- assert
            Assert.IsTrue(actual);

            //-- clean up
            _repository.DeleteUser(user);
            _repository.Save();
        }