feat: post_interaction routing

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-12-16 01:20:10 +03:00
parent 0f09dd6a82
commit b988be7056
6 changed files with 185 additions and 6 deletions

View file

@ -1,7 +1,7 @@
-- Add up migration script here
CREATE TABLE IF NOT EXISTS "post_interaction"(
interaction_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),
interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id),
interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW()
interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id)
);

View file

@ -1,7 +1,7 @@
-- Add up migration script here
CREATE TABLE IF NOT EXISTS "comment_interaction"(
interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW(),
comment_creation_time TIMESTAMPTZ NOT NULL REFERENCES "comment"(creation_time),
user_id BIGSERIAL NOT NULL REFERENCES "user"(id),
interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id),
interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW()
interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id)
);

View file

@ -3,8 +3,8 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct CommentInteraction {
pub interaction_time: DateTime<Utc>,
pub comment_creation_time: DateTime<Utc>,
pub interaction_id: i64,
pub user_id: i64,
pub interaction_time: DateTime<Utc>,
}

View file

@ -1,10 +1,59 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Postgres};
use crate::database::post_interaction;
#[derive(Debug, Serialize, Deserialize)]
pub struct PostInteraction {
pub interaction_time: DateTime<Utc>,
pub post_creation_time: DateTime<Utc>,
pub interaction_id: i64,
pub user_id: i64,
pub interaction_time: DateTime<Utc>,
}
impl PostInteraction {
pub async fn create(
post_creation_time: &DateTime<Utc>,
user_id: &i64,
interaction_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<PostInteraction, sqlx::Error> {
post_interaction::create(
post_creation_time,
user_id,
interaction_id,
database_connection,
)
.await
}
pub async fn read(
interaction_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<PostInteraction, sqlx::Error> {
post_interaction::read(interaction_time, database_connection).await
}
pub async fn update(
interaction_time: &DateTime<Utc>,
interaction_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<PostInteraction, sqlx::Error> {
post_interaction::update(interaction_time, interaction_id, database_connection).await
}
pub async fn delete(
interaction_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<PostInteraction, sqlx::Error> {
post_interaction::delete(interaction_time, database_connection).await
}
pub async fn read_all_for_post(
post_creation_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<Vec<PostInteraction>, sqlx::Error> {
post_interaction::read_all_for_post(post_creation_time, database_connection).await
}
}

View file

@ -1,6 +1,7 @@
pub mod comment;
pub mod interaction;
pub mod post;
pub mod post_interaction;
pub mod role;
pub mod user;
@ -32,6 +33,10 @@ pub async fn route(State(app_state): State<AppState>) -> Router {
"/interactions",
interaction::route(axum::extract::State(app_state.clone())),
)
.nest(
"/post_interactions",
post_interaction::route(axum::extract::State(app_state.clone())),
)
.layer(CorsLayer::permissive())
.with_state(app_state)
}

View file

@ -0,0 +1,125 @@
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::post_interaction::PostInteraction, AppState};
#[derive(Debug, Serialize, Deserialize)]
struct CreatePostInteraction {
pub post_creation_time: DateTime<Utc>,
pub interaction_id: i64,
pub user_id: i64,
}
#[derive(Debug, Serialize, Deserialize)]
struct UpdatePostInteraction {
pub interaction_time: DateTime<Utc>,
pub post_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("/posts/:post_creation_time", get(read_all_for_post))
.with_state(app_state)
}
async fn create(
State(app_state): State<AppState>,
Json(create_post_interaction): Json<CreatePostInteraction>,
) -> impl IntoResponse {
match PostInteraction::create(
&create_post_interaction.post_creation_time,
&create_post_interaction.user_id,
&create_post_interaction.interaction_id,
&app_state.database_connection,
)
.await
{
Ok(post_interaction) => (
StatusCode::CREATED,
Json(serde_json::json!(post_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 PostInteraction::read(&interaction_time, &app_state.database_connection).await {
Ok(post_interaction) => (StatusCode::OK, Json(serde_json::json!(post_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_post_interaction): Json<UpdatePostInteraction>,
) -> impl IntoResponse {
match PostInteraction::update(
&update_post_interaction.interaction_time,
&update_post_interaction.interaction_id,
&app_state.database_connection,
)
.await
{
Ok(post_interaction) => (
StatusCode::ACCEPTED,
Json(serde_json::json!(post_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 PostInteraction::delete(&interaction_time, &app_state.database_connection).await {
Ok(post_interaction) => (
StatusCode::NO_CONTENT,
Json(serde_json::json!(post_interaction)),
),
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 PostInteraction::read_all_for_post(&post_creation_time, &app_state.database_connection)
.await
{
Ok(post_interactions) => (StatusCode::OK, Json(serde_json::json!(post_interactions))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}