added get_one, get_popular datasource methods, added respective routes
This commit is contained in:
@ -1,22 +1,29 @@
|
||||
use crate::{datasources::posts::PostsDatasource, AppState};
|
||||
use axum::{
|
||||
extract::State,
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
response::IntoResponse,
|
||||
routing::get,
|
||||
Json, Router,
|
||||
};
|
||||
use chrono::Utc;
|
||||
use serde::{Serialize, Serializer};
|
||||
use sqlx::{PgPool, Pool, Postgres};
|
||||
use serde::{Deserialize, Serialize, Serializer};
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
#[derive(sqlx::FromRow, Serialize)]
|
||||
#[derive(sqlx::FromRow, Serialize, Debug)]
|
||||
pub struct Post {
|
||||
pub post_id: 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: chrono::DateTime<Utc>,
|
||||
pub created_at: Option<chrono::DateTime<Utc>>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct PostGetOneParams {
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
pub struct PostsRoute;
|
||||
@ -25,7 +32,8 @@ impl PostsRoute {
|
||||
// add more post routes here!
|
||||
Router::new()
|
||||
.route("/all", get(PostsRoute::get_all))
|
||||
// .route("/:id", get(PostsRoute::get_one))
|
||||
.route("/:id", get(PostsRoute::get_one))
|
||||
.route("/popular", get(PostsRoute::get_popular_posts))
|
||||
.with_state(app_state.db.clone())
|
||||
}
|
||||
|
||||
@ -38,21 +46,34 @@ impl PostsRoute {
|
||||
}
|
||||
|
||||
// get one post
|
||||
// async fn get_one(State(pool): State<PgPool>) -> Json<()> {
|
||||
// let results = PostsDatasource::get_one(pool).await;
|
||||
// Json {}
|
||||
// }
|
||||
|
||||
// get the top three posts with the highest view count
|
||||
// async fn get_popular_posts(State(pool): State<PgPool>) {}
|
||||
async fn get_one(
|
||||
State(pool): State<Pool<Postgres>>,
|
||||
Path(params): Path<PostGetOneParams>,
|
||||
) -> impl IntoResponse {
|
||||
match PostsDatasource::get_one(&pool, params.id).await {
|
||||
Ok(post) => Ok(Json(post)),
|
||||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
// get the top three posts with the most comments
|
||||
async fn get_popular_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
||||
match PostsDatasource::get_popular(&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_hot_posts(State(pool): State<PgPool>) {}
|
||||
}
|
||||
|
||||
fn serialize_datetime<S>(date: &chrono::DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
fn serialize_datetime<S>(
|
||||
date: &Option<chrono::DateTime<Utc>>,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(&date.to_rfc3339())
|
||||
serializer.serialize_str(&date.unwrap().to_rfc3339())
|
||||
}
|
||||
|
Reference in New Issue
Block a user