private void Seed()
        {
            using (var context = new BlogPostsContext(_contextOptions))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                int count = 3;

                for (int i = 0; i < count; i++)
                {
                    int id       = i + 1;
                    var blogPost = new BlogPost
                    {
                        PostId  = id,
                        Title   = $"Title {id}",
                        Body    = $"Body {id}",
                        Creator = $"Creator {id}",
                        Dt      = DateTime.Now
                    };

                    context.Set <BlogPost>().Add(blogPost);
                }

                context.SaveChanges();
            }
        }
        public void BlogPostsController_Get_All_BlogPosts_Result_Is_3_BlogPosts()
        {
            var context = new BlogPostsContext(_contextOptions);
            var repo    = new DataRepository <BlogPost>(context);


            BlogPostsController controller = new BlogPostsController(context, repo);
            var result = controller.GetBlogPost();

            Assert.NotNull(result);
            Assert.NotNull(result.Result);
            Assert.IsAssignableFrom <IEnumerable <BlogPost> >(result.Result.Value);
            Assert.Equal(3, result.Result.Value.Count());
        }
Пример #3
0
        public int GetTotalPages()
        {
            using var context = new BlogPostsContext(_connectionString);
            var blogs = GetBlogPosts(1);

            if (blogs.Count % 3 == 0)
            {
                return(blogs.Count);
            }
            else
            {
                return(blogs.Count + 1);
            }
        }
        public void BlogPostsController_Adds_BlogPosts_Result_Is_Not_Null_Repo_Add_and_Save_Once()
        {
            var blogPost = new Mock <BlogPost>();

            var context        = new BlogPostsContext(_contextOptions);
            var mockRepository = new Mock <IDataRepository <BlogPost> >();

            mockRepository.Setup(m => m.Add(It.IsAny <BlogPost>()));
            mockRepository.Setup(m => m.SaveAsync(It.IsAny <BlogPost>()));

            BlogPostsController controller = new BlogPostsController(context, mockRepository.Object);
            var result = controller.PostBlogPost(blogPost.Object);

            mockRepository.Verify(m => m.Add(It.IsAny <BlogPost>()), Times.Once());
            mockRepository.Verify(m => m.SaveAsync(It.IsAny <BlogPost>()), Times.Once());

            Assert.NotNull(result);
            Assert.NotNull(result.Result);
            Assert.IsType <ActionResult <BlogPost> >(result.Result);
        }
Пример #5
0
 public TagRepo(BlogPostsContext context)
 {
     _context = context;
 }
Пример #6
0
 public BlogsController(BlogPostsContext db) => _db = db;
Пример #7
0
 public UnitOfWork(BlogPostsContext context)
 {
     _context = context;
 }
Пример #8
0
 public List <BlogPost> GetBlogPosts(int skip)
 {
     using var context = new BlogPostsContext(_connectionString);
     return(context.BlogPosts.OrderByDescending(bp => bp.Id).Include(c => c.Comments)
            .Skip((skip - 1) * 3).Take(3).ToList());
 }
Пример #9
0
 public BlogPostsController(BlogPostsContext context, IDataRepository <BlogPost> repo, ILogger <BlogPostsController> logger)
 {
     _context = context;
     _repo    = repo;
     _logger  = logger;
 }
Пример #10
0
 public void AddComment(Comment comment)
 {
     using var context = new BlogPostsContext(_connectionString);
     context.Comments.Add(comment);
     context.SaveChanges();
 }
Пример #11
0
 public List <Comment> GetCommentsForBlogPost(int blogPostId)
 {
     using var context = new BlogPostsContext(_connectionString);
     return(context.Comments.Where(c => c.BlogPostId == blogPostId).ToList());
 }
Пример #12
0
 public BlogPost GetById(int id)
 {
     using var context = new BlogPostsContext(_connectionString);
     return(context.BlogPosts.Where(bp => bp.Id == id).Include(c => c.Comments).FirstOrDefault());
 }
Пример #13
0
 public BlogPost GetMostRecent()
 {
     using var context = new BlogPostsContext(_connectionString);
     return(context.BlogPosts.OrderByDescending(bp => bp.Id).Include(c => c.Comments).FirstOrDefault());
 }
Пример #14
0
 public DataRepository(BlogPostsContext context)
 {
     _context = context;
 }
Пример #15
0
 public BlogPostController(BlogPostsContext blogPostsContext, IRepository <BlogPost> repository)
 {
     _blogPostsContext = blogPostsContext;
     _dataRepository   = repository;
 }
Пример #16
0
 public BlogPostsController(BlogPostsContext context, IDataRepository <BlogPost> repo)
 {
     _context = context;
     _repo    = repo;
 }
Пример #17
0
 public void AddBlogPost(BlogPost bp)
 {
     using var context = new BlogPostsContext(_connectionString);
     context.BlogPosts.Add(bp);
     context.SaveChanges();
 }