From 9bd2cf373a21b42384741a250a93b8d1afe60a62 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Wed, 25 Sep 2024 18:29:12 -0400 Subject: [PATCH] wip: get all posts route working state --- backend/public/src/datasources/posts.rs | 16 +++++--- backend/public/src/main.rs | 10 ++--- backend/public/src/routes/comments.rs | 30 +++++++------- backend/public/src/routes/posts.rs | 54 ++++++++++++++++++------- backend/public/src/routes/root.rs | 5 +-- 5 files changed, 72 insertions(+), 43 deletions(-) diff --git a/backend/public/src/datasources/posts.rs b/backend/public/src/datasources/posts.rs index 22c1a3a..f247aae 100644 --- a/backend/public/src/datasources/posts.rs +++ b/backend/public/src/datasources/posts.rs @@ -1,11 +1,15 @@ -use sqlx::PgPool; +use sqlx::{PgPool, Pool, Postgres}; + +use crate::routes::posts::Post; pub struct PostsDatasource; impl PostsDatasource { - pub async fn get_all(pool: PgPool) { - sqlx::query("SELECT title, body, created_at FROM posts ORDER BY created_at DESC LIMIT 10") - .fetch_all(&pool) - .await; + pub async fn get_all(pool: &Pool) -> Result, sqlx::Error> { + sqlx::query_as::<_, Post>( + "SELECT post_id, title, body, created_at FROM posts ORDER BY created_at DESC LIMIT 10", + ) + .fetch_all(pool) + .await } - pub async fn get_one(pool: PgPool) {} + pub async fn get_one(pool: PgPool, post_id: i32) {} } diff --git a/backend/public/src/main.rs b/backend/public/src/main.rs index b054430..0905b60 100644 --- a/backend/public/src/main.rs +++ b/backend/public/src/main.rs @@ -46,11 +46,11 @@ async fn main() { // build our application with some routes let app = Router::new() .nest("/", routes::root::RootRoute::routes()) - .nest("/posts", routes::posts::PostsRoute::routes(&app_state)) - .nest( - "/comments", - routes::comments::CommentsRoute::routes(&app_state), - ); + .nest("/posts", routes::posts::PostsRoute::routes(&app_state)); + // .nest( + // "/comments", + // routes::comments::CommentsRoute::routes(&app_state), + // ); // run it with hyper let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap(); diff --git a/backend/public/src/routes/comments.rs b/backend/public/src/routes/comments.rs index f804ae9..cb38f74 100644 --- a/backend/public/src/routes/comments.rs +++ b/backend/public/src/routes/comments.rs @@ -19,21 +19,21 @@ impl CommentsRoute { pub fn routes(app_state: &AppState) -> axum::Router { // add more comment routes here! axum::Router::new() - .route("/post/:id", get(CommentsRoute::get_post_comments)) - .route("/add", post(CommentsRoute::insert_comment)) - .with_state(app_state.db) + // .route("/post/:id", get(CommentsRoute::get_post_comments)) + // .route("/add", post(CommentsRoute::insert_comment)) + .with_state(app_state.db.clone()) } - async fn get_post_comments(State(pool): State) -> Json<()> { - let results = CommentsDatasource::get_posts_comments(pool).await; - Json {} - } - - async fn insert_comment( - State(pool): State, - Form(comment_input): Form, - ) -> bool { - let results = CommentsDatasource::insert_comment(pool, comment_input).await; - true - } + // async fn get_post_comments(State(pool): State) -> Json<()> { + // let results = CommentsDatasource::get_posts_comments(pool).await; + // Json {} + // } + // + // async fn insert_comment( + // State(pool): State, + // Form(comment_input): Form, + // ) -> bool { + // let results = CommentsDatasource::insert_comment(pool, comment_input).await; + // true + // } } diff --git a/backend/public/src/routes/posts.rs b/backend/public/src/routes/posts.rs index c19b5b0..6eabe07 100644 --- a/backend/public/src/routes/posts.rs +++ b/backend/public/src/routes/posts.rs @@ -1,32 +1,58 @@ use crate::{datasources::posts::PostsDatasource, AppState}; -use axum::{extract::State, routing::get, Json, Router}; -use sqlx::PgPool; +use axum::{ + extract::State, + http::StatusCode, + response::{IntoResponse, Response}, + routing::get, + Json, Router, +}; +use chrono::Utc; +use serde::{Serialize, Serializer}; +use sqlx::{PgPool, Pool, Postgres}; + +#[derive(sqlx::FromRow, Serialize)] +pub struct Post { + pub post_id: i32, + pub title: String, + pub body: String, + #[serde(serialize_with = "serialize_datetime")] + pub created_at: chrono::DateTime, +} pub struct PostsRoute; impl PostsRoute { pub fn routes(app_state: &AppState) -> Router { // add more post routes here! Router::new() - .route("/", get(PostsRoute::get_all)) - .route("/:id", get(PostsRoute::get_one)) - .with_state(app_state.db) + .route("/all", get(PostsRoute::get_all)) + // .route("/:id", get(PostsRoute::get_one)) + .with_state(app_state.db.clone()) } // get all posts - async fn get_all(State(pool): State) -> Json<()> { - let results = PostsDatasource::get_all(pool).await; - Json {} + async fn get_all(State(pool): State>) -> impl IntoResponse { + match PostsDatasource::get_all(&pool).await { + Ok(posts) => Ok(Json(posts)), + Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())), + } } // get one post - async fn get_one(State(pool): State) -> Json<()> { - let results = PostsDatasource::get_one(pool).await; - Json {} - } + // async fn get_one(State(pool): State) -> 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) -> Json<()> {} + // async fn get_popular_posts(State(pool): State) {} // get the top three posts with the most comments - async fn get_hot_posts(State(pool): State) -> Json<()> {} + // async fn get_hot_posts(State(pool): State) {} +} + +fn serialize_datetime(date: &chrono::DateTime, serializer: S) -> Result +where + S: Serializer, +{ + serializer.serialize_str(&date.to_rfc3339()) } diff --git a/backend/public/src/routes/root.rs b/backend/public/src/routes/root.rs index 56b4399..ab23023 100644 --- a/backend/public/src/routes/root.rs +++ b/backend/public/src/routes/root.rs @@ -8,9 +8,8 @@ use axum::{ pub struct RootRoute; impl RootRoute { pub fn routes() -> Router { - Router::new() - .route("/", get(RootRoute::root)) - .fallback(RootRoute::not_found) + Router::new().route("/", get(RootRoute::root)) + // .fallback(RootRoute::not_found) } async fn root() -> Html<&'static str> {