feat: post routing

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-12-15 04:30:39 +03:00
parent e167b864ee
commit dc849dd970
15 changed files with 185 additions and 26 deletions

2
.env
View file

@ -1 +1,3 @@
# This is for sqlx to do compile time sql checking
# Actual server and database configs are in config folder
DATABASE_URL=postgres://root:root@localhost:5432/rust_forum

View file

@ -1,6 +1,6 @@
-- Add up migration script here
CREATE TABLE IF NOT EXISTS "post"(
creation_time TIMESTAMPTZ PRIMARY KEY UNIQUE NOT NULL DEFAULT NOW(),
poster_id BIGSERIAL NOT NULL REFERENCES "user"(id),
user_id BIGSERIAL NOT NULL REFERENCES "user"(id),
post VARCHAR NOT NULL UNIQUE
);

View file

@ -2,6 +2,6 @@
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(),
commenter_id BIGSERIAL NOT NULL REFERENCES "user"(id),
user_id BIGSERIAL NOT NULL REFERENCES "user"(id),
comment VARCHAR NOT NULL
);

View file

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

View file

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

View file

@ -5,19 +5,19 @@ use crate::feature::comment::Comment;
pub async fn create(
post_creation_time: &DateTime<Utc>,
commenter_id: &i64,
user_id: &i64,
comment: &String,
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
sqlx::query_as!(
Comment,
r#"
INSERT INTO "comment"(post_creation_time, commenter_id, comment)
INSERT INTO "comment"(post_creation_time, user_id, comment)
VALUES ($1, $2, $3)
RETURNING *
"#,
post_creation_time,
commenter_id,
user_id,
comment,
)
.fetch_one(database_connection)

View file

@ -5,20 +5,20 @@ use crate::feature::comment_interaction::CommentInteraction;
pub async fn create(
comment_creation_time: &DateTime<Utc>,
user_id: &i64,
interaction_id: &i64,
interactor_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<CommentInteraction, sqlx::Error> {
sqlx::query_as!(
CommentInteraction,
r#"
INSERT INTO "comment_interaction"(comment_creation_time, interaction_id, interactor_id)
INSERT INTO "comment_interaction"(comment_creation_time, user_id, interaction_id)
VALUES ($1, $2, $3)
RETURNING *
"#,
comment_creation_time,
user_id,
interaction_id,
interactor_id,
)
.fetch_one(database_connection)
.await

View file

@ -4,18 +4,18 @@ use sqlx::{Pool, Postgres};
use crate::feature::post::Post;
pub async fn create(
poster_id: &i64,
user_id: &i64,
post: &String,
database_connection: &Pool<Postgres>,
) -> Result<Post, sqlx::Error> {
sqlx::query_as!(
Post,
r#"
INSERT INTO "post"(poster_id, post)
INSERT INTO "post"(user_id, post)
VALUES ($1, $2)
RETURNING *
"#,
poster_id,
user_id,
post
)
.fetch_one(database_connection)
@ -39,18 +39,18 @@ pub async fn read(
pub async fn update(
creation_time: &DateTime<Utc>,
poster_id: &i64,
user_id: &i64,
post: &String,
database_connection: &Pool<Postgres>,
) -> Result<Post, sqlx::Error> {
sqlx::query_as!(
Post,
r#"
UPDATE "post" SET poster_id = $2, post = $3 WHERE "creation_time" = $1
UPDATE "post" SET user_id = $2, post = $3 WHERE "creation_time" = $1
RETURNING *
"#,
creation_time,
poster_id,
user_id,
post
)
.fetch_one(database_connection)
@ -85,15 +85,15 @@ pub async fn read_all(database_connection: &Pool<Postgres>) -> Result<Vec<Post>,
}
pub async fn read_all_for_user(
poster_id: &i64,
user_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<Vec<Post>, sqlx::Error> {
sqlx::query_as!(
Post,
r#"
SELECT * FROM "post" WHERE "poster_id" = $1
SELECT * FROM "post" WHERE "user_id" = $1
"#,
poster_id
user_id
)
.fetch_all(database_connection)
.await

View file

@ -5,20 +5,20 @@ use crate::feature::post_interaction::PostInteraction;
pub async fn create(
post_creation_time: &DateTime<Utc>,
user_id: &i64,
interaction_id: &i64,
interactor_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<PostInteraction, sqlx::Error> {
sqlx::query_as!(
PostInteraction,
r#"
INSERT INTO "post_interaction"(post_creation_time, interaction_id, interactor_id)
INSERT INTO "post_interaction"(post_creation_time, user_id, interaction_id)
VALUES ($1, $2, $3)
RETURNING *
"#,
post_creation_time,
user_id,
interaction_id,
interactor_id,
)
.fetch_one(database_connection)
.await

View file

@ -5,6 +5,6 @@ use serde::{Deserialize, Serialize};
pub struct Comment {
pub post_creation_time: DateTime<Utc>,
pub creation_time: DateTime<Utc>,
pub commenter_id: i64,
pub user_id: i64,
pub comment: String,
}

View file

@ -5,6 +5,6 @@ use serde::{Deserialize, Serialize};
pub struct CommentInteraction {
pub comment_creation_time: DateTime<Utc>,
pub interaction_id: i64,
pub interactor_id: i64,
pub user_id: i64,
pub interaction_time: DateTime<Utc>,
}

View file

@ -1,9 +1,56 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Postgres};
use crate::database::post;
#[derive(Debug, Serialize, Deserialize)]
pub struct Post {
pub creation_time: DateTime<Utc>,
pub poster_id: i64,
pub user_id: i64,
pub post: String,
}
impl Post {
pub async fn create(
user_id: &i64,
post: &String,
database_connection: &Pool<Postgres>,
) -> Result<Post, sqlx::Error> {
post::create(user_id, post, database_connection).await
}
pub async fn read(
creation_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<Post, sqlx::Error> {
post::read(creation_time, database_connection).await
}
pub async fn update(
creation_time: &DateTime<Utc>,
user_id: &i64,
post: &String,
database_connection: &Pool<Postgres>,
) -> Result<Post, sqlx::Error> {
post::update(creation_time, user_id, post, database_connection).await
}
pub async fn delete(
creation_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<Post, sqlx::Error> {
post::delete(creation_time, database_connection).await
}
pub async fn read_all(database_connection: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
post::read_all(database_connection).await
}
pub async fn read_all_for_user(
user_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<Vec<Post>, sqlx::Error> {
post::read_all_for_user(user_id, database_connection).await
}
}

View file

@ -5,6 +5,6 @@ use serde::{Deserialize, Serialize};
pub struct PostInteraction {
pub post_creation_time: DateTime<Utc>,
pub interaction_id: i64,
pub interactor_id: i64,
pub user_id: i64,
pub interaction_time: DateTime<Utc>,
}

View file

@ -1,3 +1,4 @@
pub mod post;
pub mod role;
pub mod user;

109
src/routing/post.rs Normal file
View file

@ -0,0 +1,109 @@
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::Post, AppState};
#[derive(Debug, Serialize, Deserialize)]
struct CreatePost {
user_id: i64,
post: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct UpdatePost {
creation_time: DateTime<Utc>,
user_id: i64,
post: 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("/", get(read_all))
.with_state(app_state)
}
async fn create(
State(app_state): State<AppState>,
Json(create_post): Json<CreatePost>,
) -> impl IntoResponse {
match Post::create(
&create_post.user_id,
&create_post.post,
&app_state.database_connection,
)
.await
{
Ok(post) => (StatusCode::CREATED, Json(serde_json::json!(post))),
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 Post::read(&creation_time, &app_state.database_connection).await {
Ok(post) => (StatusCode::OK, Json(serde_json::json!(post))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn update(
State(app_state): State<AppState>,
Json(update_role): Json<UpdatePost>,
) -> impl IntoResponse {
match Post::update(
&update_role.creation_time,
&update_role.user_id,
&update_role.post,
&app_state.database_connection,
)
.await
{
Ok(post) => (StatusCode::ACCEPTED, Json(serde_json::json!(post))),
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 Post::delete(&creation_time, &app_state.database_connection).await {
Ok(post) => (StatusCode::NO_CONTENT, Json(serde_json::json!(post))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn read_all(State(app_state): State<AppState>) -> impl IntoResponse {
match Post::read_all(&app_state.database_connection).await {
Ok(posts) => (StatusCode::OK, Json(serde_json::json!(posts))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}