added recent posts endpoint, added image column to authors table
This commit is contained in:
parent
b8f3d4ca7a
commit
44f1f35caa
@ -11,7 +11,7 @@ impl AuthorsDatasource {
|
|||||||
let offset: i64 = (pagination.page_number - 1) * pagination.page_size;
|
let offset: i64 = (pagination.page_number - 1) * pagination.page_size;
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
Author,
|
Author,
|
||||||
"SELECT author_id, first_name, last_name, bio FROM authors ORDER BY created_at DESC LIMIT $1 OFFSET $2",
|
"SELECT author_id, first_name, last_name, bio, image FROM authors ORDER BY created_at DESC LIMIT $1 OFFSET $2",
|
||||||
pagination.page_size,
|
pagination.page_size,
|
||||||
offset,
|
offset,
|
||||||
)
|
)
|
||||||
@ -22,7 +22,7 @@ impl AuthorsDatasource {
|
|||||||
pub async fn get_one(pool: &Pool<Postgres>, author_id: i32) -> Result<Author, sqlx::Error> {
|
pub async fn get_one(pool: &Pool<Postgres>, author_id: i32) -> Result<Author, sqlx::Error> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
Author,
|
Author,
|
||||||
"SELECT author_id, first_name, last_name, bio FROM authors WHERE author_id = $1",
|
"SELECT author_id, first_name, last_name, bio, image FROM authors WHERE author_id = $1",
|
||||||
author_id
|
author_id
|
||||||
)
|
)
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
@ -35,7 +35,7 @@ impl AuthorsDatasource {
|
|||||||
) -> Result<Vec<Post>, sqlx::Error> {
|
) -> Result<Vec<Post>, sqlx::Error> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
Post,
|
Post,
|
||||||
"SELECT p.post_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.author_id = $1 ORDER BY created_at DESC",
|
"SELECT p.post_id, a.first_name, a.last_name, p.title, p.body, p.created_at, a.author_id FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.author_id = $1 ORDER BY created_at DESC",
|
||||||
author_id
|
author_id
|
||||||
)
|
)
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
|
@ -1,34 +1,49 @@
|
|||||||
use sqlx::{Pool, Postgres};
|
use sqlx::{Pool, Postgres};
|
||||||
|
|
||||||
use crate::routes::posts::Post;
|
use crate::routes::posts::{Post, PostFeaturedVariant};
|
||||||
|
|
||||||
pub struct PostsDatasource;
|
pub struct PostsDatasource;
|
||||||
impl PostsDatasource {
|
impl PostsDatasource {
|
||||||
pub async fn get_all(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
|
pub async fn get_all(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
|
||||||
sqlx::query_as!(Post, "SELECT p.post_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL ORDER BY p.created_at DESC LIMIT 10")
|
sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL ORDER BY p.created_at DESC LIMIT 10")
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_one(pool: &Pool<Postgres>, post_id: i32) -> Result<Post, sqlx::Error> {
|
pub async fn get_one(
|
||||||
|
pool: &Pool<Postgres>,
|
||||||
|
post_id: i32,
|
||||||
|
) -> Result<PostFeaturedVariant, sqlx::Error> {
|
||||||
let _ = sqlx::query("UPDATE posts SET view_count = view_count + 1 WHERE post_id = $1")
|
let _ = sqlx::query("UPDATE posts SET view_count = view_count + 1 WHERE post_id = $1")
|
||||||
.bind(post_id)
|
.bind(post_id)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
sqlx::query_as!(Post, "SELECT p.post_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.post_id = $1 ORDER BY p.created_at DESC", post_id)
|
sqlx::query_as!(PostFeaturedVariant, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at, p.is_featured FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.post_id = $1 ORDER BY p.created_at DESC", post_id)
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_recent(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
|
||||||
|
sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL GROUP BY p.post_id, a.first_name, a.last_name ORDER BY p.created_at DESC LIMIT 3")
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_popular(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
|
pub async fn get_popular(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
|
||||||
sqlx::query_as!(Post, "SELECT p.post_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id LEFT JOIN comments c ON p.post_id = c.post_id WHERE p.deleted_at IS NULL GROUP BY p.post_id, a.first_name, a.last_name ORDER BY p.created_at DESC LIMIT 3")
|
sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id LEFT JOIN comments c ON p.post_id = c.post_id WHERE p.deleted_at IS NULL GROUP BY p.post_id, a.first_name, a.last_name ORDER BY COUNT(c.comment_id) DESC LIMIT 3")
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_hot(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
|
pub async fn get_hot(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
|
||||||
sqlx::query_as!(Post, "SELECT p.post_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL ORDER BY p.view_count DESC LIMIT 3")
|
sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL ORDER BY p.view_count DESC LIMIT 3")
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_featured(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
|
||||||
|
sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.is_featured IS true GROUP BY p.post_id, a.first_name, a.last_name ORDER BY p.created_at DESC LIMIT 3")
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ pub struct Author {
|
|||||||
pub first_name: String,
|
pub first_name: String,
|
||||||
pub last_name: String,
|
pub last_name: String,
|
||||||
pub bio: Option<String>,
|
pub bio: Option<String>,
|
||||||
|
pub image: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -13,6 +13,7 @@ use sqlx::{Pool, Postgres};
|
|||||||
#[derive(sqlx::FromRow, Serialize, Debug)]
|
#[derive(sqlx::FromRow, Serialize, Debug)]
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
pub post_id: i32,
|
pub post_id: i32,
|
||||||
|
pub author_id: Option<i32>,
|
||||||
pub first_name: Option<String>,
|
pub first_name: Option<String>,
|
||||||
pub last_name: Option<String>,
|
pub last_name: Option<String>,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
@ -21,6 +22,19 @@ pub struct Post {
|
|||||||
pub created_at: Option<chrono::DateTime<Utc>>,
|
pub created_at: Option<chrono::DateTime<Utc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(sqlx::FromRow, Serialize, Debug)]
|
||||||
|
pub struct PostFeaturedVariant {
|
||||||
|
pub post_id: i32,
|
||||||
|
pub author_id: Option<i32>,
|
||||||
|
pub first_name: Option<String>,
|
||||||
|
pub last_name: Option<String>,
|
||||||
|
pub title: String,
|
||||||
|
pub body: String,
|
||||||
|
#[serde(serialize_with = "serialize_datetime")]
|
||||||
|
pub created_at: Option<chrono::DateTime<Utc>>,
|
||||||
|
pub is_featured: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct PostGetOneParams {
|
pub struct PostGetOneParams {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
@ -33,8 +47,10 @@ impl PostsRoute {
|
|||||||
Router::new()
|
Router::new()
|
||||||
.route("/all", get(PostsRoute::get_all))
|
.route("/all", get(PostsRoute::get_all))
|
||||||
.route("/:id", get(PostsRoute::get_one))
|
.route("/:id", get(PostsRoute::get_one))
|
||||||
|
.route("/recent", get(PostsRoute::get_recent_posts))
|
||||||
.route("/popular", get(PostsRoute::get_popular_posts))
|
.route("/popular", get(PostsRoute::get_popular_posts))
|
||||||
.route("/hot", get(PostsRoute::get_hot_posts))
|
.route("/hot", get(PostsRoute::get_hot_posts))
|
||||||
|
.route("/featured", get(PostsRoute::get_featured_posts))
|
||||||
.with_state(app_state.db.clone())
|
.with_state(app_state.db.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +73,15 @@ impl PostsRoute {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the top three posts with the most comments
|
// get recent posts
|
||||||
|
async fn get_recent_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
||||||
|
match PostsDatasource::get_recent(&pool).await {
|
||||||
|
Ok(posts) => Ok(Json(posts)),
|
||||||
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the top three posts with the highest view count
|
||||||
async fn get_popular_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
async fn get_popular_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
||||||
match PostsDatasource::get_popular(&pool).await {
|
match PostsDatasource::get_popular(&pool).await {
|
||||||
Ok(posts) => Ok(Json(posts)),
|
Ok(posts) => Ok(Json(posts)),
|
||||||
@ -65,13 +89,21 @@ impl PostsRoute {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the top three posts with the highest view count
|
// get the top three posts with the most comments
|
||||||
async fn get_hot_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
async fn get_hot_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
||||||
match PostsDatasource::get_hot(&pool).await {
|
match PostsDatasource::get_hot(&pool).await {
|
||||||
Ok(posts) => Ok(Json(posts)),
|
Ok(posts) => Ok(Json(posts)),
|
||||||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get posts that are featured
|
||||||
|
async fn get_featured_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
||||||
|
match PostsDatasource::get_featured(&pool).await {
|
||||||
|
Ok(posts) => Ok(Json(posts)),
|
||||||
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialize_datetime<S>(
|
pub fn serialize_datetime<S>(
|
||||||
|
Loading…
Reference in New Issue
Block a user