Пример #1
0
        public void TestInitialize()
        {
            _connection = new SqliteConnection("DataSource=:memory:");
            _connection.Open();

            var options = new DbContextOptionsBuilder <WeblogContext>()
                          .UseSqlite(_connection)
                          .Options;

            _context = new WeblogContext(options);
            _context.Database.EnsureCreated();

            _repository = new WeblogDataRepository(_context);

            _resourceParameters = new CommentsResourceParameters
            {
                PageNumber = 1,
                PageSize   = 10
            };

            _repository.AddUser(new User
            {
                FirstName    = "fname",
                LastName     = "lname",
                EmailAddress = "user@email",
                Password     = "******"
            });
            _repository.Save();

            _repository.AddBlog(1, new Blog
            {
                Title   = "blog-title",
                Excerpt = "blog-excerpt"
            });
            _repository.Save();

            _repository.AddPost(1, new Post
            {
                Title       = "post-title",
                Body        = "post-body",
                TimeCreated = DateTime.Now
            });
            _repository.Save();
        }
Пример #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));
        }
Пример #3
0
        public void AddBlog()
        {
            //-- arrange
            var countBeforeAdd = _repository.GetBlogs(_resourceParameters).Count();

            var blog = new Blog
            {
                Title   = "title",
                Excerpt = "excerpt"
            };

            //-- act
            _repository.AddBlog(1, blog);
            _repository.Save();

            var actual = _repository.GetBlogs(_resourceParameters).Count();

            //-- assert
            Assert.AreEqual(countBeforeAdd + 1, actual);

            //-- clean up
            _repository.DeleteBlog(blog);
            _repository.Save();
        }