wip: added get post comments, working insert comment to post
This commit is contained in:
parent
c303e6aaee
commit
8128459b4e
@ -1,8 +1,22 @@
|
|||||||
use crate::routes::comments::CommentInput;
|
use crate::routes::comments::{Comment, CommentInputPayload};
|
||||||
use sqlx::PgPool;
|
use sqlx::{Pool, Postgres};
|
||||||
|
|
||||||
pub struct CommentsDatasource;
|
pub struct CommentsDatasource;
|
||||||
impl CommentsDatasource {
|
impl CommentsDatasource {
|
||||||
pub async fn get_posts_comments(pool: PgPool) {}
|
pub async fn get_posts_comments(
|
||||||
pub async fn insert_comment(pool: PgPool, comment_input: CommentInput) {}
|
pool: &Pool<Postgres>,
|
||||||
|
post_id: i32,
|
||||||
|
) -> Result<Vec<Comment>, sqlx::Error> {
|
||||||
|
sqlx::query_as!(Comment, "SELECT c.comment_id, c.name, c.body, c.created_at FROM comments c LEFT JOIN posts p ON p.post_id = c.post_id WHERE p.post_id = $1 AND c.deleted_at IS NULL ORDER BY created_at DESC LIMIT 20", post_id)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
pub async fn insert_comment(
|
||||||
|
pool: &Pool<Postgres>,
|
||||||
|
comment_input: CommentInputPayload,
|
||||||
|
) -> Result<Comment, sqlx::Error> {
|
||||||
|
sqlx::query!("INSERT INTO comments (post_id, name, body) VALUES ($1, $2, $3) RETURNING comment_id, name, body, created_at", comment_input.post_id, comment_input.name, comment_input.body)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ async fn main() {
|
|||||||
)
|
)
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
if std::env::var("RUST_ENV").expect("development") != "development" {
|
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string()) != "development" {
|
||||||
println!("we're not in development, starting up the rate limiter");
|
println!("we're not in development, starting up the rate limiter");
|
||||||
let governor_conf = Arc::new(
|
let governor_conf = Arc::new(
|
||||||
GovernorConfigBuilder::default()
|
GovernorConfigBuilder::default()
|
||||||
@ -97,11 +97,11 @@ async fn main() {
|
|||||||
TraceLayer::new_for_http()
|
TraceLayer::new_for_http()
|
||||||
.make_span_with(trace::DefaultMakeSpan::new().level(tracing::Level::INFO))
|
.make_span_with(trace::DefaultMakeSpan::new().level(tracing::Level::INFO))
|
||||||
.on_response(trace::DefaultOnResponse::new().level(tracing::Level::INFO)),
|
.on_response(trace::DefaultOnResponse::new().level(tracing::Level::INFO)),
|
||||||
|
)
|
||||||
|
.nest(
|
||||||
|
"/comments",
|
||||||
|
routes::comments::CommentsRoute::routes(&app_state),
|
||||||
);
|
);
|
||||||
// .nest(
|
|
||||||
// "/comments",
|
|
||||||
// routes::comments::CommentsRoute::routes(&app_state),
|
|
||||||
// );
|
|
||||||
|
|
||||||
// run it with hyper
|
// run it with hyper
|
||||||
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||||
|
@ -1,17 +1,35 @@
|
|||||||
|
use super::posts::serialize_datetime;
|
||||||
use crate::{datasources::comments::CommentsDatasource, AppState};
|
use crate::{datasources::comments::CommentsDatasource, AppState};
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Form, State},
|
extract::{Form, Path, State},
|
||||||
|
http::StatusCode,
|
||||||
|
response::IntoResponse,
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Json,
|
Json,
|
||||||
};
|
};
|
||||||
use serde::Deserialize;
|
use chrono::Utc;
|
||||||
use sqlx::PgPool;
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::{Pool, Postgres};
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct CommentInput {
|
pub struct CommentInputPayload {
|
||||||
name: String,
|
pub name: String,
|
||||||
body: String,
|
pub body: String,
|
||||||
post_id: i32,
|
pub post_id: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct CommentPathParams {
|
||||||
|
id: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(sqlx::FromRow, Serialize, Debug)]
|
||||||
|
pub struct Comment {
|
||||||
|
pub comment_id: i32,
|
||||||
|
pub name: String,
|
||||||
|
pub body: String,
|
||||||
|
#[serde(serialize_with = "serialize_datetime")]
|
||||||
|
pub created_at: Option<chrono::DateTime<Utc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CommentsRoute;
|
pub struct CommentsRoute;
|
||||||
@ -19,21 +37,28 @@ impl CommentsRoute {
|
|||||||
pub fn routes(app_state: &AppState) -> axum::Router {
|
pub fn routes(app_state: &AppState) -> axum::Router {
|
||||||
// add more comment routes here!
|
// add more comment routes here!
|
||||||
axum::Router::new()
|
axum::Router::new()
|
||||||
// .route("/post/:id", get(CommentsRoute::get_post_comments))
|
.route("/post/:id", get(CommentsRoute::get_post_comments))
|
||||||
// .route("/add", post(CommentsRoute::insert_comment))
|
.route("/add", post(CommentsRoute::insert_comment))
|
||||||
.with_state(app_state.db.clone())
|
.with_state(app_state.db.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
// async fn get_post_comments(State(pool): State<PgPool>) -> Json<()> {
|
async fn get_post_comments(
|
||||||
// let results = CommentsDatasource::get_posts_comments(pool).await;
|
State(pool): State<Pool<Postgres>>,
|
||||||
// Json {}
|
Path(params): Path<CommentPathParams>,
|
||||||
// }
|
) -> impl IntoResponse {
|
||||||
//
|
match CommentsDatasource::get_posts_comments(&pool, params.id).await {
|
||||||
// async fn insert_comment(
|
Ok(c) => Ok(Json(c)),
|
||||||
// State(pool): State<PgPool>,
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||||
// Form(comment_input): Form<CommentInput>,
|
}
|
||||||
// ) -> bool {
|
}
|
||||||
// let results = CommentsDatasource::insert_comment(pool, comment_input).await;
|
//
|
||||||
// true
|
async fn insert_comment(
|
||||||
// }
|
State(pool): State<Pool<Postgres>>,
|
||||||
|
Json(comment_input): Json<CommentInputPayload>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
match CommentsDatasource::insert_comment(&pool, comment_input).await {
|
||||||
|
Ok(c) => Ok((StatusCode::CREATED, Json(c))),
|
||||||
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ impl PostsRoute {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_datetime<S>(
|
pub fn serialize_datetime<S>(
|
||||||
date: &Option<chrono::DateTime<Utc>>,
|
date: &Option<chrono::DateTime<Utc>>,
|
||||||
serializer: S,
|
serializer: S,
|
||||||
) -> Result<S::Ok, S::Error>
|
) -> Result<S::Ok, S::Error>
|
||||||
|
Loading…
Reference in New Issue
Block a user