feat: ✨ comment routing
This commit is contained in:
parent
dc849dd970
commit
0d9cef79f3
4 changed files with 168 additions and 4 deletions
|
@ -1,7 +1,7 @@
|
||||||
-- Add up migration script here
|
-- Add up migration script here
|
||||||
CREATE TABLE IF NOT EXISTS "comment"(
|
CREATE TABLE IF NOT EXISTS "comment"(
|
||||||
post_creation_time TIMESTAMPTZ NOT NULL REFERENCES "post"(creation_time),
|
|
||||||
creation_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW(),
|
creation_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW(),
|
||||||
|
post_creation_time TIMESTAMPTZ NOT NULL REFERENCES "post"(creation_time),
|
||||||
user_id BIGSERIAL NOT NULL REFERENCES "user"(id),
|
user_id BIGSERIAL NOT NULL REFERENCES "user"(id),
|
||||||
comment VARCHAR NOT NULL
|
comment VARCHAR NOT NULL
|
||||||
);
|
);
|
|
@ -1,10 +1,53 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::{Pool, Postgres};
|
||||||
|
|
||||||
|
use crate::database::comment;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Comment {
|
pub struct Comment {
|
||||||
pub post_creation_time: DateTime<Utc>,
|
|
||||||
pub creation_time: DateTime<Utc>,
|
pub creation_time: DateTime<Utc>,
|
||||||
|
pub post_creation_time: DateTime<Utc>,
|
||||||
pub user_id: i64,
|
pub user_id: i64,
|
||||||
pub comment: String,
|
pub comment: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Comment {
|
||||||
|
pub async fn create(
|
||||||
|
post_creation_time: &DateTime<Utc>,
|
||||||
|
user_id: &i64,
|
||||||
|
comment: &String,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Comment, sqlx::Error> {
|
||||||
|
comment::create(post_creation_time, user_id, comment, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read(
|
||||||
|
creation_time: &DateTime<Utc>,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Comment, sqlx::Error> {
|
||||||
|
comment::read(creation_time, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update(
|
||||||
|
creation_time: &DateTime<Utc>,
|
||||||
|
comment: &String,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Comment, sqlx::Error> {
|
||||||
|
comment::update(creation_time, comment, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete(
|
||||||
|
creation_time: &DateTime<Utc>,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Comment, sqlx::Error> {
|
||||||
|
comment::delete(creation_time, database_connection).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read_all_for_post(
|
||||||
|
post_creation_time: &DateTime<Utc>,
|
||||||
|
database_connection: &Pool<Postgres>,
|
||||||
|
) -> Result<Vec<Comment>, sqlx::Error> {
|
||||||
|
comment::read_all_for_post(post_creation_time, database_connection).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
pub mod comment;
|
||||||
pub mod post;
|
pub mod post;
|
||||||
pub mod role;
|
pub mod role;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
@ -11,13 +12,21 @@ pub async fn route(State(app_state): State<AppState>) -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/", get(alive))
|
.route("/", get(alive))
|
||||||
.nest(
|
.nest(
|
||||||
"/role",
|
"/roles",
|
||||||
role::route(axum::extract::State(app_state.clone())),
|
role::route(axum::extract::State(app_state.clone())),
|
||||||
)
|
)
|
||||||
.nest(
|
.nest(
|
||||||
"/user",
|
"/users",
|
||||||
user::route(axum::extract::State(app_state.clone())),
|
user::route(axum::extract::State(app_state.clone())),
|
||||||
)
|
)
|
||||||
|
.nest(
|
||||||
|
"/posts",
|
||||||
|
post::route(axum::extract::State(app_state.clone())),
|
||||||
|
)
|
||||||
|
.nest(
|
||||||
|
"/comments",
|
||||||
|
comment::route(axum::extract::State(app_state.clone())),
|
||||||
|
)
|
||||||
.layer(CorsLayer::permissive())
|
.layer(CorsLayer::permissive())
|
||||||
.with_state(app_state)
|
.with_state(app_state)
|
||||||
}
|
}
|
||||||
|
|
112
src/routing/comment.rs
Normal file
112
src/routing/comment.rs
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
use axum::{
|
||||||
|
extract::{Path, State},
|
||||||
|
http::StatusCode,
|
||||||
|
response::IntoResponse,
|
||||||
|
routing::{delete, get, patch, post},
|
||||||
|
Json, Router,
|
||||||
|
};
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::{feature::comment::Comment, AppState};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct CreateComment {
|
||||||
|
post_creation_time: DateTime<Utc>,
|
||||||
|
user_id: i64,
|
||||||
|
comment: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct UpdateComment {
|
||||||
|
creation_time: DateTime<Utc>,
|
||||||
|
comment: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn route(State(app_state): State<AppState>) -> Router<AppState> {
|
||||||
|
Router::new()
|
||||||
|
.route("/", post(create))
|
||||||
|
.route("/:creation_time", get(read))
|
||||||
|
.route("/", patch(update))
|
||||||
|
.route("/:creation_time", delete(delete_))
|
||||||
|
.route("/posts/:post_creation_time", get(read_all_for_post))
|
||||||
|
.with_state(app_state)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create(
|
||||||
|
State(app_state): State<AppState>,
|
||||||
|
Json(create_comment): Json<CreateComment>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
match Comment::create(
|
||||||
|
&create_comment.post_creation_time,
|
||||||
|
&create_comment.user_id,
|
||||||
|
&create_comment.comment,
|
||||||
|
&app_state.database_connection,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(comment) => (StatusCode::CREATED, Json(serde_json::json!(comment))),
|
||||||
|
Err(err_val) => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(serde_json::json!(err_val.to_string())),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read(
|
||||||
|
State(app_state): State<AppState>,
|
||||||
|
Path(creation_time): Path<DateTime<Utc>>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
match Comment::read(&creation_time, &app_state.database_connection).await {
|
||||||
|
Ok(comment) => (StatusCode::OK, Json(serde_json::json!(comment))),
|
||||||
|
Err(err_val) => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(serde_json::json!(err_val.to_string())),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update(
|
||||||
|
State(app_state): State<AppState>,
|
||||||
|
Json(update_comment): Json<UpdateComment>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
match Comment::update(
|
||||||
|
&update_comment.creation_time,
|
||||||
|
&update_comment.comment,
|
||||||
|
&app_state.database_connection,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(comment) => (StatusCode::ACCEPTED, Json(serde_json::json!(comment))),
|
||||||
|
Err(err_val) => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(serde_json::json!(err_val.to_string())),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_(
|
||||||
|
State(app_state): State<AppState>,
|
||||||
|
Path(creation_time): Path<DateTime<Utc>>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
match Comment::delete(&creation_time, &app_state.database_connection).await {
|
||||||
|
Ok(comment) => (StatusCode::NO_CONTENT, Json(serde_json::json!(comment))),
|
||||||
|
Err(err_val) => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(serde_json::json!(err_val.to_string())),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_all_for_post(
|
||||||
|
State(app_state): State<AppState>,
|
||||||
|
Path(post_creation_time): Path<DateTime<Utc>>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
match Comment::read_all_for_post(&post_creation_time, &app_state.database_connection).await {
|
||||||
|
Ok(comments) => (StatusCode::OK, Json(serde_json::json!(comments))),
|
||||||
|
Err(err_val) => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(serde_json::json!(err_val.to_string())),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue