Add blog index with pagination
This commit is contained in:
parent
68fa2cf1c7
commit
b8f371e3e8
5 changed files with 78 additions and 6 deletions
|
@ -3,6 +3,7 @@ using System.Linq;
|
|||
using Dapper;
|
||||
using Website.Models;
|
||||
using Website.Data.States;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Website.Data
|
||||
{
|
||||
|
@ -21,12 +22,24 @@ namespace Website.Data
|
|||
}
|
||||
}
|
||||
|
||||
public async Task<BlogPost> GetLatestPostAsync() {
|
||||
const string query = "SELECT * FROM blog_posts WHERE post_content<>'' AND post_deleted=0 ORDER BY post_timestamp DESC LIMIT 1";
|
||||
public async Task<IEnumerable<BlogPost>> GetLatestPostsAsync(int limit, int offset) {
|
||||
const string query = "SELECT * FROM blog_posts WHERE post_content<>'' AND post_deleted=0 ORDER BY post_timestamp DESC LIMIT @offset,@limit";
|
||||
using (var connection = _dbProvider.NewConnection()) {
|
||||
connection.Open();
|
||||
var result = await connection.QueryAsync<BlogPostState>(query);
|
||||
return new BlogPost(result.First());
|
||||
var results = await connection.QueryAsync<BlogPostState>(query, new{limit, offset});
|
||||
return results.Select(result => new BlogPost(result));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<BlogPost> GetLatestPostAsync() => (await GetLatestPostsAsync(1, 0)).First();
|
||||
|
||||
public async Task<int> GetCountAsync()
|
||||
{
|
||||
var query="SELECT COUNT(*) FROM blog_posts WHERE post_content<>'' AND post_deleted=0";
|
||||
using(var connection = _dbProvider.NewConnection()) {
|
||||
connection.Open();
|
||||
var result = await connection.QueryAsync<int>(query);
|
||||
return result.First();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue