コード例 #1
0
        public int GetMostRecentBlogPost()
        {
            using var ctx = new BlogsiteDbContext(_connectionString);
            BlogPost post = ctx.BlogPosts.Include(p => p.Comments).OrderByDescending(p => p.DatePosted).FirstOrDefault();

            return(post.Id);
        }
コード例 #2
0
        public int GetLastPage()
        {
            var ctx   = new BlogsiteDbContext(_connectionString);
            var total = ctx.BlogPosts.Count();

            if (total % 3 == 0)
            {
                return(total / 3);
            }

            return((total / 3) + 1);
        }
コード例 #3
0
 public BlogPost GetById(int id)
 {
     using var ctx = new BlogsiteDbContext(_connectionString);
     return(ctx.BlogPosts.Include(p => p.Comments).FirstOrDefault(p => p.Id == id));
 }
コード例 #4
0
 public List <BlogPost> GetAll(int page)
 {
     using var ctx = new BlogsiteDbContext(_connectionString);
     return(ctx.BlogPosts.Include(p => p.Comments).OrderByDescending(p => p.DatePosted).Skip((page - 1) * 3).Take(3).ToList());
 }
コード例 #5
0
 public int GetPostsCount()
 {
     using var ctx = new BlogsiteDbContext(_connectionString);
     return(ctx.BlogPosts.Count());
 }
コード例 #6
0
 public void AddComment(Comment comment)
 {
     using var ctx = new BlogsiteDbContext(_connectionString);
     ctx.Comments.Add(comment);
     ctx.SaveChanges();
 }
コード例 #7
0
 public void AddBlogpost(BlogPost blogpost)
 {
     using var ctx = new BlogsiteDbContext(_connectionString);
     ctx.BlogPosts.Add(blogpost);
     ctx.SaveChanges();
 }