feat: ✨ comment_interaction routing
This commit is contained in:
parent
b988be7056
commit
04b39ba9c9
3 changed files with 188 additions and 0 deletions
|
@ -1,5 +1,8 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::database::comment_interaction;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CommentInteraction {
|
||||
|
@ -8,3 +11,49 @@ pub struct CommentInteraction {
|
|||
pub interaction_id: i64,
|
||||
pub user_id: i64,
|
||||
}
|
||||
|
||||
impl CommentInteraction {
|
||||
pub async fn create(
|
||||
comment_creation_time: &DateTime<Utc>,
|
||||
user_id: &i64,
|
||||
interaction_id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<CommentInteraction, sqlx::Error> {
|
||||
comment_interaction::create(
|
||||
comment_creation_time,
|
||||
user_id,
|
||||
interaction_id,
|
||||
database_connection,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn read(
|
||||
interaction_time: &DateTime<Utc>,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<CommentInteraction, sqlx::Error> {
|
||||
comment_interaction::read(interaction_time, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
interaction_time: &DateTime<Utc>,
|
||||
interaction_id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<CommentInteraction, sqlx::Error> {
|
||||
comment_interaction::update(interaction_time, interaction_id, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
interaction_time: &DateTime<Utc>,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<CommentInteraction, sqlx::Error> {
|
||||
comment_interaction::delete(interaction_time, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn read_all_for_comment(
|
||||
comment_creation_time: &DateTime<Utc>,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Vec<CommentInteraction>, sqlx::Error> {
|
||||
comment_interaction::read_all_for_comment(comment_creation_time, database_connection).await
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
pub mod comment;
|
||||
pub mod comment_interaction;
|
||||
pub mod interaction;
|
||||
pub mod post;
|
||||
pub mod post_interaction;
|
||||
|
@ -37,6 +38,10 @@ pub async fn route(State(app_state): State<AppState>) -> Router {
|
|||
"/post_interactions",
|
||||
post_interaction::route(axum::extract::State(app_state.clone())),
|
||||
)
|
||||
.nest(
|
||||
"/comment_interactions",
|
||||
comment_interaction::route(axum::extract::State(app_state.clone())),
|
||||
)
|
||||
.layer(CorsLayer::permissive())
|
||||
.with_state(app_state)
|
||||
}
|
||||
|
|
134
src/routing/comment_interaction.rs
Normal file
134
src/routing/comment_interaction.rs
Normal file
|
@ -0,0 +1,134 @@
|
|||
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_interaction::CommentInteraction, AppState};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct CreateCommentInteraction {
|
||||
pub comment_creation_time: DateTime<Utc>,
|
||||
pub interaction_id: i64,
|
||||
pub user_id: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct UpdateCommentInteraction {
|
||||
pub interaction_time: DateTime<Utc>,
|
||||
pub comment_creation_time: DateTime<Utc>,
|
||||
pub interaction_id: i64,
|
||||
pub user_id: i64,
|
||||
}
|
||||
|
||||
pub fn route(State(app_state): State<AppState>) -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/", post(create))
|
||||
.route("/:interaction_time", get(read))
|
||||
.route("/", patch(update))
|
||||
.route("/:interaction_time", delete(delete_))
|
||||
.route(
|
||||
"/comments/:comment_creation_time",
|
||||
get(read_all_for_comment),
|
||||
)
|
||||
.with_state(app_state)
|
||||
}
|
||||
|
||||
async fn create(
|
||||
State(app_state): State<AppState>,
|
||||
Json(create_comment_interaction): Json<CreateCommentInteraction>,
|
||||
) -> impl IntoResponse {
|
||||
match CommentInteraction::create(
|
||||
&create_comment_interaction.comment_creation_time,
|
||||
&create_comment_interaction.user_id,
|
||||
&create_comment_interaction.interaction_id,
|
||||
&app_state.database_connection,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(comment_interaction) => (
|
||||
StatusCode::CREATED,
|
||||
Json(serde_json::json!(comment_interaction)),
|
||||
),
|
||||
Err(err_val) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!(err_val.to_string())),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
async fn read(
|
||||
State(app_state): State<AppState>,
|
||||
Path(interaction_time): Path<DateTime<Utc>>,
|
||||
) -> impl IntoResponse {
|
||||
match CommentInteraction::read(&interaction_time, &app_state.database_connection).await {
|
||||
Ok(comment_interaction) => (StatusCode::OK, Json(serde_json::json!(comment_interaction))),
|
||||
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_interaction): Json<UpdateCommentInteraction>,
|
||||
) -> impl IntoResponse {
|
||||
match CommentInteraction::update(
|
||||
&update_comment_interaction.interaction_time,
|
||||
&update_comment_interaction.interaction_id,
|
||||
&app_state.database_connection,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(comment_interaction) => (
|
||||
StatusCode::ACCEPTED,
|
||||
Json(serde_json::json!(comment_interaction)),
|
||||
),
|
||||
Err(err_val) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!(err_val.to_string())),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
async fn delete_(
|
||||
State(app_state): State<AppState>,
|
||||
Path(interaction_time): Path<DateTime<Utc>>,
|
||||
) -> impl IntoResponse {
|
||||
match CommentInteraction::delete(&interaction_time, &app_state.database_connection).await {
|
||||
Ok(comment_interaction) => (
|
||||
StatusCode::NO_CONTENT,
|
||||
Json(serde_json::json!(comment_interaction)),
|
||||
),
|
||||
Err(err_val) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(serde_json::json!(err_val.to_string())),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
async fn read_all_for_comment(
|
||||
State(app_state): State<AppState>,
|
||||
Path(comment_creation_time): Path<DateTime<Utc>>,
|
||||
) -> impl IntoResponse {
|
||||
match CommentInteraction::read_all_for_comment(
|
||||
&comment_creation_time,
|
||||
&app_state.database_connection,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(comment_interactions) => (
|
||||
StatusCode::OK,
|
||||
Json(serde_json::json!(comment_interactions)),
|
||||
),
|
||||
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