Edit and create blog posts
This commit is contained in:
parent
47431d4650
commit
44875a6a45
6 changed files with 126 additions and 1 deletions
|
@ -42,5 +42,27 @@ namespace Website.Data
|
|||
return result.First();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<BlogPost> GetPostByIdAsync(int id) {
|
||||
const string query = "SELECT * FROM blog_posts WHERE post_id=@id AND post_deleted=0";
|
||||
using (var connection = _dbProvider.NewConnection()) {
|
||||
connection.Open();
|
||||
var result = await connection.QueryAsync<BlogPostState>(query, new {id});
|
||||
return new BlogPost(result.First());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<BlogPost> SavePost(BlogPost post) {
|
||||
const string newPostQuery = "INSERT INTO blog_posts (post_title, post_content, post_draft, post_url) VALUES (@title, @content, @draft, @url); SELECT CAST(LAST_INSERT_ID() as int)";
|
||||
const string updatePostQuery = "UPDATE blog_posts SET post_id = @id, post_title = @title, post_content = @content, post_draft = @draft, post_url = @url WHERE post_id = @id ";
|
||||
|
||||
var query = post.Id == 0 ? newPostQuery : updatePostQuery;
|
||||
|
||||
using (var connection = _dbProvider.NewConnection()) {
|
||||
connection.Open();
|
||||
var result = await connection.QueryAsync<int>(query, post);
|
||||
return await GetPostByIdAsync(result.Single());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue