Use new API for blog
This commit is contained in:
parent
e389b2404a
commit
25c320bf6b
17 changed files with 356 additions and 314 deletions
22
Website/Data/BlogApi.cs
Normal file
22
Website/Data/BlogApi.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using System.Threading.Tasks;
|
||||
using Website.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Website.Data
|
||||
{
|
||||
public class BlogApi : ApiClient, IBlogApi {
|
||||
public BlogApi(HttpClient client) : base(client) {
|
||||
}
|
||||
|
||||
public async Task<BlogPost> GetPostByUrlAsync(string url) => await Get<BlogPost>("/get/" + url);
|
||||
public async Task<IEnumerable<BlogPost>> GetLatestPostsAsync(int count = 0, int offset = 0) => await Get<IEnumerable<BlogPost>>("/getlatestposts", new{count, offset});
|
||||
public async Task<BlogPost> GetLatestPostAsync() => await Get<BlogPost>("/getlatestpost");
|
||||
public async Task<int> GetCountAsync() => await Get<int>("/getcount");
|
||||
public async Task<BlogPost> GetPostByIdAsync(int id) => await Get<BlogPost>("/get/" + id);
|
||||
public async Task<BlogPost> SavePost(BlogPostSubmission post) => await Post<BlogPost>("/savepost", post);
|
||||
public async Task<IEnumerable<BlogPost>> GetAllPostsAsync() => await Get<IEnumerable<BlogPost>>("/getallposts");
|
||||
public async Task DeletePostAsync(int id) => await Post<object>("/deletepost", id);
|
||||
public async Task PublishPostAsync(int id) => await Post<object>("/publishpost", id);
|
||||
}
|
||||
}
|
|
@ -1,87 +0,0 @@
|
|||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using Dapper;
|
||||
using Website.Models;
|
||||
using Website.Data.States;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Website.Data
|
||||
{
|
||||
public class BlogRepository
|
||||
{
|
||||
private readonly IDatabaseProvider _dbProvider;
|
||||
|
||||
public BlogRepository(IDatabaseProvider dbProvider) => _dbProvider = dbProvider;
|
||||
|
||||
public async Task<BlogPost> GetPostByUrlAsync(string url) {
|
||||
const string query = "SELECT * FROM blog_posts WHERE post_url=@url AND post_deleted=0";
|
||||
using (var connection = _dbProvider.NewConnection()) {
|
||||
connection.Open();
|
||||
var result = await connection.QueryAsync<BlogPostState>(query, new{url});
|
||||
return new BlogPost(result.First());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<BlogPost>> GetLatestPostsAsync(int limit, int offset = 0) {
|
||||
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 results = await connection.QueryAsync<BlogPostState>(query, new{limit, offset});
|
||||
return results.Select(result => new BlogPost(result));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<BlogPost> GetLatestPostAsync() => (await GetLatestPostsAsync(1)).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();
|
||||
}
|
||||
}
|
||||
|
||||
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 newPost = post.Id == 0;
|
||||
|
||||
using (var connection = _dbProvider.NewConnection()) {
|
||||
connection.Open();
|
||||
var result = await connection.QueryAsync<int>(newPost ? newPostQuery : updatePostQuery, post);
|
||||
return newPost ? await GetPostByIdAsync(result.Single()) : post;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<BlogPost>> GetAllPostsAsync() {
|
||||
const string query = "SELECT * FROM blog_posts WHERE post_deleted=0";
|
||||
|
||||
using (var connection = _dbProvider.NewConnection()) {
|
||||
connection.Open();
|
||||
var result = await connection.QueryAsync<BlogPostState>(query);
|
||||
return result.Select(state => new BlogPost(state));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeletePostAsync(int id) {
|
||||
const string query = "UPDATE blog_posts SET post_deleted=1 WHERE post_id=@id";
|
||||
|
||||
using (var connection = _dbProvider.NewConnection()) {
|
||||
connection.Open();
|
||||
await connection.ExecuteAsync(query, new {id});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
17
Website/Data/IBlogApi.cs
Normal file
17
Website/Data/IBlogApi.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Website.Models;
|
||||
|
||||
namespace Website.Data {
|
||||
public interface IBlogApi {
|
||||
Task<BlogPost> GetPostByUrlAsync(string url);
|
||||
Task<IEnumerable<BlogPost>> GetLatestPostsAsync(int count = 0, int offset = 0);
|
||||
Task<BlogPost> GetLatestPostAsync();
|
||||
Task<int> GetCountAsync();
|
||||
Task<BlogPost> GetPostByIdAsync(int id);
|
||||
Task<BlogPost> SavePost(BlogPostSubmission post);
|
||||
Task<IEnumerable<BlogPost>> GetAllPostsAsync();
|
||||
Task DeletePostAsync(int id);
|
||||
Task PublishPostAsync(int id);
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
using System;
|
||||
using Dapper.Contrib.Extensions;
|
||||
|
||||
namespace Website.Data.States
|
||||
{
|
||||
[Table("blog_posts")]
|
||||
public class BlogPostState
|
||||
{
|
||||
[Key]
|
||||
public int Post_Id { get; set; }
|
||||
public string Post_Title { get; set; }
|
||||
public string Post_Content { get; set; }
|
||||
public DateTime Post_Timestamp { get; set; }
|
||||
public string Post_Draft { get; set; }
|
||||
public string Post_Url { get; set; }
|
||||
public bool Post_Deleted{ get; set; }
|
||||
public int User_Id { get; set; }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue