added recent posts endpoint, added image column to authors table
This commit is contained in:
@ -13,6 +13,7 @@ use sqlx::{Pool, Postgres};
|
||||
#[derive(sqlx::FromRow, Serialize, Debug)]
|
||||
pub struct Post {
|
||||
pub post_id: i32,
|
||||
pub author_id: Option<i32>,
|
||||
pub first_name: Option<String>,
|
||||
pub last_name: Option<String>,
|
||||
pub title: String,
|
||||
@ -21,6 +22,19 @@ pub struct Post {
|
||||
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)]
|
||||
pub struct PostGetOneParams {
|
||||
pub id: i32,
|
||||
@ -33,8 +47,10 @@ impl PostsRoute {
|
||||
Router::new()
|
||||
.route("/all", get(PostsRoute::get_all))
|
||||
.route("/:id", get(PostsRoute::get_one))
|
||||
.route("/recent", get(PostsRoute::get_recent_posts))
|
||||
.route("/popular", get(PostsRoute::get_popular_posts))
|
||||
.route("/hot", get(PostsRoute::get_hot_posts))
|
||||
.route("/featured", get(PostsRoute::get_featured_posts))
|
||||
.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 {
|
||||
match PostsDatasource::get_popular(&pool).await {
|
||||
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 {
|
||||
match PostsDatasource::get_hot(&pool).await {
|
||||
Ok(posts) => Ok(Json(posts)),
|
||||
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>(
|
||||
|
Reference in New Issue
Block a user