From 6d3b6a4e795065e495aa34a545902c4da26a04c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Tue, 28 Jan 2025 22:38:14 +0300 Subject: [PATCH] feat: :sparkles: admin routing part 5 --- .../20241204225128_create_role_table.up.sql | 10 +-- .../20241204225135_create_user_table.up.sql | 2 +- .../20241204225143_create_post_table.up.sql | 8 +- ...20241204225151_create_comment_table.up.sql | 9 +- ...1204225155_create_interaction_table.up.sql | 4 +- ...30240_create_post_interaction_table.up.sql | 9 +- ...48_create_comment_interaction_table.up.sql | 9 +- migrations/20241213232937_contact.up.sql | 4 +- migrations/20241215002127_user_contact.up.sql | 2 +- src/database/comment.rs | 37 ++++---- src/database/comment_interaction.rs | 36 ++++---- src/database/contact.rs | 18 ++-- src/database/interaction.rs | 18 ++-- src/database/post.rs | 29 +++--- src/database/post_interaction.rs | 38 ++++---- src/database/role.rs | 18 ++-- src/feature/comment.rs | 28 +++--- src/feature/comment_interaction.rs | 28 +++--- src/feature/contact.rs | 14 +-- src/feature/interaction.rs | 14 +-- src/feature/post.rs | 19 ++-- src/feature/post_interaction.rs | 29 +++--- src/feature/role.rs | 14 +-- src/feature/user.rs | 7 -- src/routing/admin.rs | 2 + src/routing/admin/comment.rs | 25 ++++++ src/routing/admin/contact.rs | 16 ++-- src/routing/admin/interaction.rs | 16 ++-- src/routing/admin/post.rs | 31 +++---- src/routing/admin/role.rs | 16 ++-- src/routing/comment.rs | 70 ++++++++++----- src/routing/comment_interaction.rs | 85 ++++++------------ src/routing/post.rs | 27 +++--- src/routing/post_interaction.rs | 88 +++++++------------ src/routing/role.rs | 6 +- 35 files changed, 385 insertions(+), 401 deletions(-) create mode 100644 src/routing/admin/comment.rs diff --git a/migrations/20241204225128_create_role_table.up.sql b/migrations/20241204225128_create_role_table.up.sql index 0afdef8..1f6d16a 100644 --- a/migrations/20241204225128_create_role_table.up.sql +++ b/migrations/20241204225128_create_role_table.up.sql @@ -1,10 +1,10 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS "role"( - id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, + role_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, name VARCHAR(16) NOT NULL UNIQUE ); -INSERT INTO "role"(id, name) VALUES (0, 'Builder') ON CONFLICT(id) DO UPDATE SET "name" = 'Builder'; -INSERT INTO "role"(id, name) VALUES (1, 'Admin') ON CONFLICT(id) DO UPDATE SET "name" = 'Admin'; -INSERT INTO "role"(id, name) VALUES (10, 'Normal') ON CONFLICT(id) DO UPDATE SET "name" = 'Normal'; -INSERT INTO "role"(id, name) VALUES (-1, 'Banned') ON CONFLICT(id) DO UPDATE SET "name" = 'Banned'; +INSERT INTO "role"(role_id, name) VALUES (0, 'Builder') ON CONFLICT(role_id) DO UPDATE SET "name" = 'Builder'; +INSERT INTO "role"(role_id, name) VALUES (1, 'Admin') ON CONFLICT(role_id) DO UPDATE SET "name" = 'Admin'; +INSERT INTO "role"(role_id, name) VALUES (10, 'Normal') ON CONFLICT(role_id) DO UPDATE SET "name" = 'Normal'; +INSERT INTO "role"(role_id, name) VALUES (-1, 'Banned') ON CONFLICT(role_id) DO UPDATE SET "name" = 'Banned'; diff --git a/migrations/20241204225135_create_user_table.up.sql b/migrations/20241204225135_create_user_table.up.sql index bf5a2aa..7b77d00 100644 --- a/migrations/20241204225135_create_user_table.up.sql +++ b/migrations/20241204225135_create_user_table.up.sql @@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS "user"( surname VARCHAR(256) NOT NULL, gender BOOLEAN NOT NULL, birth_date DATE NOT NULL, - role_id BIGINT NOT NULL REFERENCES "role" DEFAULT 10, + role_id BIGINT NOT NULL REFERENCES "role"(role_id) DEFAULT 10, creation_time TIMESTAMPTZ NOT NULL DEFAULT NOW() ); diff --git a/migrations/20241204225143_create_post_table.up.sql b/migrations/20241204225143_create_post_table.up.sql index 98b6f1a..52c2f23 100644 --- a/migrations/20241204225143_create_post_table.up.sql +++ b/migrations/20241204225143_create_post_table.up.sql @@ -1,7 +1,7 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS "post"( - user_id BIGSERIAL NOT NULL REFERENCES "user"(user_id), - creation_time TIMESTAMPTZ UNIQUE NOT NULL DEFAULT NOW(), - post VARCHAR(8192) NOT NULL UNIQUE, - PRIMARY KEY(user_id, creation_time) + post_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, + user_id BIGINT NOT NULL REFERENCES "user"(user_id), + creation_time TIMESTAMPTZ NOT NULL DEFAULT NOW(), + post TEXT NOT NULL UNIQUE ); diff --git a/migrations/20241204225151_create_comment_table.up.sql b/migrations/20241204225151_create_comment_table.up.sql index b5263f0..a7ccd03 100644 --- a/migrations/20241204225151_create_comment_table.up.sql +++ b/migrations/20241204225151_create_comment_table.up.sql @@ -1,7 +1,8 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS "comment"( - 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"(user_id), - comment VARCHAR(8192) NOT NULL + comment_id BIGSERIAL NOT NULL PRIMARY KEY, + user_id BIGINT NOT NULL REFERENCES "user"(user_id), + post_id BIGINT NOT NULL REFERENCES "post"(post_id), + creation_time TIMESTAMPTZ NOT NULL DEFAULT NOW(), + comment TEXT NOT NULL ); diff --git a/migrations/20241204225155_create_interaction_table.up.sql b/migrations/20241204225155_create_interaction_table.up.sql index 51565cf..9c58022 100644 --- a/migrations/20241204225155_create_interaction_table.up.sql +++ b/migrations/20241204225155_create_interaction_table.up.sql @@ -1,5 +1,5 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS "interaction"( - id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, + interaction_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, name VARCHAR(32) UNIQUE NOT NULL -); \ No newline at end of file +); diff --git a/migrations/20241204230240_create_post_interaction_table.up.sql b/migrations/20241204230240_create_post_interaction_table.up.sql index 0ddb8d1..40e2496 100644 --- a/migrations/20241204230240_create_post_interaction_table.up.sql +++ b/migrations/20241204230240_create_post_interaction_table.up.sql @@ -1,7 +1,8 @@ -- 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"(user_id), - interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id) + post_id BIGINT NOT NULL REFERENCES "post"(post_id), + user_id BIGINT NOT NULL REFERENCES "user"(user_id), + interaction_time TIMESTAMPTZ NOT NULL DEFAULT NOW(), + interaction_id BIGINT NOT NULL REFERENCES "interaction"(interaction_id), + PRIMARY KEY(post_id, user_id) ); diff --git a/migrations/20241204230248_create_comment_interaction_table.up.sql b/migrations/20241204230248_create_comment_interaction_table.up.sql index 9d7aec5..4424ccb 100644 --- a/migrations/20241204230248_create_comment_interaction_table.up.sql +++ b/migrations/20241204230248_create_comment_interaction_table.up.sql @@ -1,7 +1,8 @@ -- 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"(user_id), - interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id) + comment_id BIGINT NOT NULL REFERENCES "comment"(comment_id), + user_id BIGINT NOT NULL REFERENCES "user"(user_id), + interaction_time TIMESTAMPTZ NOT NULL DEFAULT NOW(), + interaction_id BIGINT NOT NULL REFERENCES "interaction"(interaction_id), + PRIMARY KEY(comment_id, user_id) ); diff --git a/migrations/20241213232937_contact.up.sql b/migrations/20241213232937_contact.up.sql index cfc4961..500b317 100644 --- a/migrations/20241213232937_contact.up.sql +++ b/migrations/20241213232937_contact.up.sql @@ -1,7 +1,7 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS "contact"( - id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, + contact_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, name VARCHAR(32) NOT NULL UNIQUE ); -INSERT INTO "contact"(id, name) VALUES (0, 'Email') ON CONFLICT(id) DO UPDATE SET "name" = 'Email'; +INSERT INTO "contact"(contact_id, name) VALUES (0, 'Email') ON CONFLICT(contact_id) DO UPDATE SET "name" = 'Email'; diff --git a/migrations/20241215002127_user_contact.up.sql b/migrations/20241215002127_user_contact.up.sql index 0c56f51..a4638f8 100644 --- a/migrations/20241215002127_user_contact.up.sql +++ b/migrations/20241215002127_user_contact.up.sql @@ -1,7 +1,7 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS "user_contact"( user_id BIGSERIAL NOT NULL REFERENCES "user"(user_id), - contact_id BIGSERIAL NOT NULL REFERENCES "contact"(id), + contact_id BIGSERIAL NOT NULL REFERENCES "contact"(contact_id), contact_value VARCHAR(256) NOT NULL, PRIMARY KEY (user_id, contact_id), UNIQUE (contact_id, contact_value) diff --git a/src/database/comment.rs b/src/database/comment.rs index 260577d..34c0423 100644 --- a/src/database/comment.rs +++ b/src/database/comment.rs @@ -1,80 +1,79 @@ -use chrono::{DateTime, Utc}; - use crate::feature::comment::Comment; use super::DATABASE_CONNECTIONS; pub async fn create( - post_creation_time: &DateTime, user_id: &i64, + post_id: &i64, comment: &String, ) -> Result { sqlx::query_as!( Comment, r#" - INSERT INTO "comment"(post_creation_time, user_id, comment) + INSERT INTO "comment"(user_id, post_id, comment) VALUES ($1, $2, $3) RETURNING * "#, - post_creation_time, user_id, + post_id, comment, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn read(creation_time: &DateTime) -> Result { +pub async fn read(comment_id: &i64) -> Result { sqlx::query_as!( Comment, r#" - SELECT * FROM "comment" WHERE "creation_time" = $1 + SELECT * FROM "comment" WHERE "comment_id" = $1 "#, - creation_time + comment_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } pub async fn update( - creation_time: &DateTime, + comment_id: &i64, + user_id: &i64, comment: &String, ) -> Result { sqlx::query_as!( Comment, r#" - UPDATE "comment" SET "comment" = $2 WHERE "creation_time" = $1 + UPDATE "comment" SET "comment" = $3 WHERE "comment_id" = $1 AND "user_id" = $2 RETURNING * "#, - creation_time, + comment_id, + user_id, comment ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn delete(creation_time: &DateTime) -> Result { +pub async fn delete(comment_id: &i64, user_id: &i64) -> Result { sqlx::query_as!( Comment, r#" - DELETE FROM "comment" WHERE "creation_time" = $1 + DELETE FROM "comment" WHERE "comment_id" = $1 AND "user_id" = $2 RETURNING * "#, - creation_time + comment_id, + user_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn read_all_for_post( - post_creation_time: &DateTime, -) -> Result, sqlx::Error> { +pub async fn read_all_for_post(post_id: &i64) -> Result, sqlx::Error> { sqlx::query_as!( Comment, r#" - SELECT * FROM "comment" WHERE "post_creation_time" = $1 + SELECT * FROM "comment" WHERE "post_id" = $1 "#, - post_creation_time + post_id, ) .fetch_all(&*DATABASE_CONNECTIONS) .await diff --git a/src/database/comment_interaction.rs b/src/database/comment_interaction.rs index a38e745..a86b79c 100644 --- a/src/database/comment_interaction.rs +++ b/src/database/comment_interaction.rs @@ -1,22 +1,20 @@ -use chrono::{DateTime, Utc}; - use crate::feature::comment_interaction::CommentInteraction; use super::DATABASE_CONNECTIONS; pub async fn create( - comment_creation_time: &DateTime, + comment_id: &i64, user_id: &i64, interaction_id: &i64, ) -> Result { sqlx::query_as!( CommentInteraction, r#" - INSERT INTO "comment_interaction"(comment_creation_time, user_id, interaction_id) + INSERT INTO "comment_interaction"(comment_id, user_id, interaction_id) VALUES ($1, $2, $3) RETURNING * "#, - comment_creation_time, + comment_id, user_id, interaction_id, ) @@ -24,57 +22,61 @@ pub async fn create( .await } -pub async fn read(interaction_time: &DateTime) -> Result { +pub async fn read(comment_id: &i64, user_id: &i64) -> Result { sqlx::query_as!( CommentInteraction, r#" - SELECT * FROM "comment_interaction" WHERE "interaction_time" = $1 + SELECT * FROM "comment_interaction" WHERE "comment_id" = $1 AND "user_id" = $2 "#, - interaction_time + comment_id, + user_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } pub async fn update( - interaction_time: &DateTime, + comment_id: &i64, + user_id: &i64, interaction_id: &i64, ) -> Result { sqlx::query_as!( CommentInteraction, r#" - UPDATE "comment_interaction" SET "interaction_id" = $2 WHERE "interaction_time" = $1 + UPDATE "comment_interaction" SET "interaction_id" = $3 WHERE "comment_id" = $1 AND "user_id" = $2 RETURNING * "#, - interaction_time, + comment_id, + user_id, interaction_id ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn delete(interaction_time: &DateTime) -> Result { +pub async fn delete(comment_id: &i64, user_id: &i64) -> Result { sqlx::query_as!( CommentInteraction, r#" - DELETE FROM "comment_interaction" WHERE "interaction_time" = $1 + DELETE FROM "comment_interaction" WHERE "comment_id" = $1 AND "user_id" = $2 RETURNING * "#, - interaction_time + comment_id, + user_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } pub async fn read_all_for_comment( - comment_creation_time: &DateTime, + comment_id: &i64, ) -> Result, sqlx::Error> { sqlx::query_as!( CommentInteraction, r#" - SELECT * FROM "comment_interaction" WHERE "comment_creation_time" = $1 + SELECT * FROM "comment_interaction" WHERE "comment_id" = $1 "#, - comment_creation_time + comment_id ) .fetch_all(&*DATABASE_CONNECTIONS) .await diff --git a/src/database/contact.rs b/src/database/contact.rs index 68e498a..9197858 100644 --- a/src/database/contact.rs +++ b/src/database/contact.rs @@ -16,40 +16,40 @@ pub async fn create(name: &String) -> Result { .await } -pub async fn read(id: &i64) -> Result { +pub async fn read(contact_id: &i64) -> Result { sqlx::query_as!( Contact, r#" - SELECT * FROM "contact" WHERE "id" = $1 + SELECT * FROM "contact" WHERE "contact_id" = $1 "#, - id + contact_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn update(id: &i64, name: &String) -> Result { +pub async fn update(contact_id: &i64, name: &String) -> Result { sqlx::query_as!( Contact, r#" - UPDATE "contact" SET "name" = $2 WHERE "id" = $1 + UPDATE "contact" SET "name" = $2 WHERE "contact_id" = $1 RETURNING * "#, - id, + contact_id, name, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn delete(id: &i64) -> Result { +pub async fn delete(contact_id: &i64) -> Result { sqlx::query_as!( Contact, r#" - DELETE FROM "contact" WHERE "id" = $1 + DELETE FROM "contact" WHERE "contact_id" = $1 RETURNING * "#, - id + contact_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await diff --git a/src/database/interaction.rs b/src/database/interaction.rs index 3eedfbe..505bf9a 100644 --- a/src/database/interaction.rs +++ b/src/database/interaction.rs @@ -16,40 +16,40 @@ pub async fn create(name: &String) -> Result { .await } -pub async fn read(id: &i64) -> Result { +pub async fn read(interaction_id: &i64) -> Result { sqlx::query_as!( Interaction, r#" - SELECT * FROM "interaction" WHERE "id" = $1 + SELECT * FROM "interaction" WHERE "interaction_id" = $1 "#, - id + interaction_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn update(id: &i64, name: &String) -> Result { +pub async fn update(interaction_id: &i64, name: &String) -> Result { sqlx::query_as!( Interaction, r#" - UPDATE "interaction" SET "name" = $2 WHERE "id" = $1 + UPDATE "interaction" SET "name" = $2 WHERE "interaction_id" = $1 RETURNING * "#, - id, + interaction_id, name ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn delete(id: &i64) -> Result { +pub async fn delete(interaction_id: &i64) -> Result { sqlx::query_as!( Interaction, r#" - DELETE FROM "interaction" WHERE "id" = $1 + DELETE FROM "interaction" WHERE "interaction_id" = $1 RETURNING * "#, - id + interaction_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await diff --git a/src/database/post.rs b/src/database/post.rs index 0be9501..a12433d 100644 --- a/src/database/post.rs +++ b/src/database/post.rs @@ -1,5 +1,3 @@ -use chrono::{DateTime, Utc}; - use crate::feature::post::Post; use super::DATABASE_CONNECTIONS; @@ -13,53 +11,48 @@ pub async fn create(user_id: &i64, post: &String) -> Result { RETURNING * "#, user_id, - post + post, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn read(user_id: &i64, creation_time: &DateTime) -> Result { +pub async fn read(post_id: &i64) -> Result { sqlx::query_as!( Post, r#" - SELECT * FROM "post" WHERE "user_id"= $1 AND "creation_time" = $2 + SELECT * FROM "post" WHERE "post_id"= $1 "#, - user_id, - creation_time + post_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn update( - user_id: &i64, - creation_time: &DateTime, - post: &String, -) -> Result { +pub async fn update(post_id: &i64, user_id: &i64, post: &String) -> Result { sqlx::query_as!( Post, r#" - UPDATE "post" SET post = $3 WHERE "user_id" = $1 AND "creation_time" = $2 + UPDATE "post" SET post = $3 WHERE "post_id" = $1 AND "user_id" = $2 RETURNING * "#, + post_id, user_id, - creation_time, - post + post, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn delete(user_id: &i64, creation_time: &DateTime) -> Result { +pub async fn delete(post_id: &i64, user_id: &i64) -> Result { sqlx::query_as!( Post, r#" - DELETE FROM "post" WHERE "user_id" = $1 AND "creation_time" = $2 + DELETE FROM "post" WHERE "post_id" = $1 AND "user_id" = $2 RETURNING * "#, + post_id, user_id, - creation_time ) .fetch_one(&*DATABASE_CONNECTIONS) .await diff --git a/src/database/post_interaction.rs b/src/database/post_interaction.rs index 87c57f7..b92893a 100644 --- a/src/database/post_interaction.rs +++ b/src/database/post_interaction.rs @@ -1,22 +1,20 @@ -use chrono::{DateTime, Utc}; - use crate::feature::post_interaction::PostInteraction; use super::DATABASE_CONNECTIONS; pub async fn create( - post_creation_time: &DateTime, + post_id: &i64, user_id: &i64, interaction_id: &i64, ) -> Result { sqlx::query_as!( PostInteraction, r#" - INSERT INTO "post_interaction"(post_creation_time, user_id, interaction_id) + INSERT INTO "post_interaction"(post_id, user_id, interaction_id) VALUES ($1, $2, $3) RETURNING * "#, - post_creation_time, + post_id, user_id, interaction_id, ) @@ -24,57 +22,59 @@ pub async fn create( .await } -pub async fn read(interaction_time: &DateTime) -> Result { +pub async fn read(post_id: &i64, user_id: &i64) -> Result { sqlx::query_as!( PostInteraction, r#" - SELECT * FROM "post_interaction" WHERE "interaction_time" = $1 + SELECT * FROM "post_interaction" WHERE "post_id" = $1 AND "user_id" = $2 "#, - interaction_time + post_id, + user_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } pub async fn update( - interaction_time: &DateTime, + post_id: &i64, + user_id: &i64, interaction_id: &i64, ) -> Result { sqlx::query_as!( PostInteraction, r#" - UPDATE "post_interaction" SET "interaction_id" = $2 WHERE "interaction_time" = $1 + UPDATE "post_interaction" SET "interaction_id" = $3 WHERE "post_id" = $1 AND "user_id" = $2 RETURNING * "#, - interaction_time, + post_id, + user_id, interaction_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn delete(interaction_time: &DateTime) -> Result { +pub async fn delete(post_id: &i64, user_id: &i64) -> Result { sqlx::query_as!( PostInteraction, r#" - DELETE FROM "post_interaction" WHERE "interaction_time" = $1 + DELETE FROM "post_interaction" WHERE "post_id" = $1 AND "user_id" = $2 RETURNING * "#, - interaction_time + post_id, + user_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn read_all_for_post( - post_creation_time: &DateTime, -) -> Result, sqlx::Error> { +pub async fn read_all_for_post(post_id: &i64) -> Result, sqlx::Error> { sqlx::query_as!( PostInteraction, r#" - SELECT * FROM "post_interaction" WHERE "post_creation_time" = $1 + SELECT * FROM "post_interaction" WHERE "post_id" = $1 "#, - post_creation_time + post_id, ) .fetch_all(&*DATABASE_CONNECTIONS) .await diff --git a/src/database/role.rs b/src/database/role.rs index 048cf5f..1541e3b 100644 --- a/src/database/role.rs +++ b/src/database/role.rs @@ -16,40 +16,40 @@ pub async fn create(name: &String) -> Result { .await } -pub async fn read(id: &i64) -> Result { +pub async fn read(role_id: &i64) -> Result { sqlx::query_as!( Role, r#" - SELECT * FROM "role" WHERE "id" = $1 + SELECT * FROM "role" WHERE "role_id" = $1 "#, - id + role_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn update(id: &i64, name: &String) -> Result { +pub async fn update(role_id: &i64, name: &String) -> Result { sqlx::query_as!( Role, r#" - UPDATE "role" SET "name" = $2 WHERE "id" = $1 + UPDATE "role" SET "name" = $2 WHERE "role_id" = $1 RETURNING * "#, - id, + role_id, name, ) .fetch_one(&*DATABASE_CONNECTIONS) .await } -pub async fn delete(id: &i64) -> Result { +pub async fn delete(role_id: &i64) -> Result { sqlx::query_as!( Role, r#" - DELETE FROM "role" WHERE "id" = $1 + DELETE FROM "role" WHERE "role_id" = $1 RETURNING * "#, - id + role_id, ) .fetch_one(&*DATABASE_CONNECTIONS) .await diff --git a/src/feature/comment.rs b/src/feature/comment.rs index c533e3c..bcffda5 100644 --- a/src/feature/comment.rs +++ b/src/feature/comment.rs @@ -5,39 +5,39 @@ use crate::database::comment; #[derive(Debug, Serialize, Deserialize)] pub struct Comment { - pub creation_time: DateTime, - pub post_creation_time: DateTime, + pub comment_id: i64, pub user_id: i64, + pub post_id: i64, + pub creation_time: DateTime, pub comment: String, } impl Comment { pub async fn create( - post_creation_time: &DateTime, user_id: &i64, + post_id: &i64, comment: &String, ) -> Result { - comment::create(post_creation_time, user_id, comment).await + comment::create(user_id, post_id, comment).await } - pub async fn read(creation_time: &DateTime) -> Result { - comment::read(creation_time).await + pub async fn read(comment_id: &i64) -> Result { + comment::read(comment_id).await } pub async fn update( - creation_time: &DateTime, + comment_id: &i64, + user_id: &i64, comment: &String, ) -> Result { - comment::update(creation_time, comment).await + comment::update(comment_id, user_id, comment).await } - pub async fn delete(creation_time: &DateTime) -> Result { - comment::delete(creation_time).await + pub async fn delete(comment_id: &i64, user_id: &i64) -> Result { + comment::delete(comment_id, user_id).await } - pub async fn read_all_for_post( - post_creation_time: &DateTime, - ) -> Result, sqlx::Error> { - comment::read_all_for_post(post_creation_time).await + pub async fn read_all_for_post(post_id: &i64) -> Result, sqlx::Error> { + comment::read_all_for_post(post_id).await } } diff --git a/src/feature/comment_interaction.rs b/src/feature/comment_interaction.rs index e9c8e04..5b76871 100644 --- a/src/feature/comment_interaction.rs +++ b/src/feature/comment_interaction.rs @@ -5,41 +5,43 @@ use crate::database::comment_interaction; #[derive(Debug, Serialize, Deserialize)] pub struct CommentInteraction { - pub interaction_time: DateTime, - pub comment_creation_time: DateTime, - pub interaction_id: i64, + pub comment_id: i64, pub user_id: i64, + pub interaction_time: DateTime, + pub interaction_id: i64, } impl CommentInteraction { pub async fn create( - comment_creation_time: &DateTime, + comment_id: &i64, user_id: &i64, interaction_id: &i64, ) -> Result { - comment_interaction::create(comment_creation_time, user_id, interaction_id).await + comment_interaction::create(comment_id, user_id, interaction_id).await } - pub async fn read(interaction_time: &DateTime) -> Result { - comment_interaction::read(interaction_time).await + pub async fn read(comment_id: &i64, user_id: &i64) -> Result { + comment_interaction::read(comment_id, user_id).await } pub async fn update( - interaction_time: &DateTime, + comment_id: &i64, + user_id: &i64, interaction_id: &i64, ) -> Result { - comment_interaction::update(interaction_time, interaction_id).await + comment_interaction::update(comment_id, user_id, interaction_id).await } pub async fn delete( - interaction_time: &DateTime, + comment_id: &i64, + user_id: &i64, ) -> Result { - comment_interaction::delete(interaction_time).await + comment_interaction::delete(comment_id, user_id).await } pub async fn read_all_for_comment( - comment_creation_time: &DateTime, + comment_id: &i64, ) -> Result, sqlx::Error> { - comment_interaction::read_all_for_comment(comment_creation_time).await + comment_interaction::read_all_for_comment(comment_id).await } } diff --git a/src/feature/contact.rs b/src/feature/contact.rs index bfffbe8..009bb81 100644 --- a/src/feature/contact.rs +++ b/src/feature/contact.rs @@ -4,7 +4,7 @@ use crate::database::contact; #[derive(Debug, Serialize, Deserialize)] pub struct Contact { - pub id: i64, + pub contact_id: i64, pub name: String, } @@ -13,16 +13,16 @@ impl Contact { contact::create(name).await } - pub async fn read(id: &i64) -> Result { - contact::read(id).await + pub async fn read(contact_id: &i64) -> Result { + contact::read(contact_id).await } - pub async fn update(id: &i64, name: &String) -> Result { - contact::update(id, name).await + pub async fn update(contact_id: &i64, name: &String) -> Result { + contact::update(contact_id, name).await } - pub async fn delete(id: &i64) -> Result { - contact::delete(id).await + pub async fn delete(contact_id: &i64) -> Result { + contact::delete(contact_id).await } pub async fn read_all() -> Result, sqlx::Error> { diff --git a/src/feature/interaction.rs b/src/feature/interaction.rs index 2e259b5..5a16ee7 100644 --- a/src/feature/interaction.rs +++ b/src/feature/interaction.rs @@ -4,7 +4,7 @@ use crate::database::interaction; #[derive(Debug, Serialize, Deserialize)] pub struct Interaction { - pub id: i64, + pub interaction_id: i64, pub name: String, } @@ -13,16 +13,16 @@ impl Interaction { interaction::create(name).await } - pub async fn read(id: &i64) -> Result { - interaction::read(id).await + pub async fn read(interaction_id: &i64) -> Result { + interaction::read(interaction_id).await } - pub async fn update(id: &i64, name: &String) -> Result { - interaction::update(id, name).await + pub async fn update(interaction_id: &i64, name: &String) -> Result { + interaction::update(interaction_id, name).await } - pub async fn delete(id: &i64) -> Result { - interaction::delete(id).await + pub async fn delete(interaction_id: &i64) -> Result { + interaction::delete(interaction_id).await } pub async fn read_all() -> Result, sqlx::Error> { diff --git a/src/feature/post.rs b/src/feature/post.rs index 0253fd3..40f648d 100644 --- a/src/feature/post.rs +++ b/src/feature/post.rs @@ -5,8 +5,9 @@ use crate::database::post; #[derive(Debug, Serialize, Deserialize)] pub struct Post { - pub creation_time: DateTime, + pub post_id: i64, pub user_id: i64, + pub creation_time: DateTime, pub post: String, } @@ -15,20 +16,16 @@ impl Post { post::create(user_id, post).await } - pub async fn read(user_id: &i64, creation_time: &DateTime) -> Result { - post::read(user_id, creation_time).await + pub async fn read(post_id: &i64) -> Result { + post::read(post_id).await } - pub async fn update( - user_id: &i64, - creation_time: &DateTime, - post: &String, - ) -> Result { - post::update(user_id, creation_time, post).await + pub async fn update(post_id: &i64, user_id: &i64, post: &String) -> Result { + post::update(post_id, user_id, post).await } - pub async fn delete(user_id: &i64, creation_time: &DateTime) -> Result { - post::delete(user_id, creation_time).await + pub async fn delete(post_id: &i64, user_id: &i64) -> Result { + post::delete(post_id, user_id).await } pub async fn read_all() -> Result, sqlx::Error> { diff --git a/src/feature/post_interaction.rs b/src/feature/post_interaction.rs index a4864f3..2db3a3d 100644 --- a/src/feature/post_interaction.rs +++ b/src/feature/post_interaction.rs @@ -5,39 +5,38 @@ use crate::database::post_interaction; #[derive(Debug, Serialize, Deserialize)] pub struct PostInteraction { - pub interaction_time: DateTime, - pub post_creation_time: DateTime, - pub interaction_id: i64, + pub post_id: i64, pub user_id: i64, + pub interaction_time: DateTime, + pub interaction_id: i64, } impl PostInteraction { pub async fn create( - post_creation_time: &DateTime, + post_id: &i64, user_id: &i64, interaction_id: &i64, ) -> Result { - post_interaction::create(post_creation_time, user_id, interaction_id).await + post_interaction::create(post_id, user_id, interaction_id).await } - pub async fn read(interaction_time: &DateTime) -> Result { - post_interaction::read(interaction_time).await + pub async fn read(post_id: &i64, user_id: &i64) -> Result { + post_interaction::read(post_id, user_id).await } pub async fn update( - interaction_time: &DateTime, + post_id: &i64, + user_id: &i64, interaction_id: &i64, ) -> Result { - post_interaction::update(interaction_time, interaction_id).await + post_interaction::update(post_id, user_id, interaction_id).await } - pub async fn delete(interaction_time: &DateTime) -> Result { - post_interaction::delete(interaction_time).await + pub async fn delete(post_id: &i64, user_id: &i64) -> Result { + post_interaction::delete(post_id, user_id).await } - pub async fn read_all_for_post( - post_creation_time: &DateTime, - ) -> Result, sqlx::Error> { - post_interaction::read_all_for_post(post_creation_time).await + pub async fn read_all_for_post(post_id: &i64) -> Result, sqlx::Error> { + post_interaction::read_all_for_post(post_id).await } } diff --git a/src/feature/role.rs b/src/feature/role.rs index 83ce071..5210fc3 100644 --- a/src/feature/role.rs +++ b/src/feature/role.rs @@ -4,7 +4,7 @@ use crate::database::role; #[derive(Debug, Serialize, Deserialize)] pub struct Role { - pub id: i64, + pub role_id: i64, pub name: String, } @@ -13,16 +13,16 @@ impl Role { role::create(name).await } - pub async fn read(id: &i64) -> Result { - role::read(id).await + pub async fn read(role_id: &i64) -> Result { + role::read(role_id).await } - pub async fn update(id: &i64, name: &String) -> Result { - role::update(id, name).await + pub async fn update(role_id: &i64, name: &String) -> Result { + role::update(role_id, name).await } - pub async fn delete(id: &i64) -> Result { - role::delete(id).await + pub async fn delete(role_id: &i64) -> Result { + role::delete(role_id).await } pub async fn read_all() -> Result, sqlx::Error> { diff --git a/src/feature/user.rs b/src/feature/user.rs index 939459f..5a14c98 100644 --- a/src/feature/user.rs +++ b/src/feature/user.rs @@ -3,13 +3,6 @@ use serde::{Deserialize, Serialize}; use crate::database::user; -#[derive(Debug, Serialize, Deserialize)] -pub struct Contact { - pub email: String, - pub phone: String, - pub website: Option, -} - #[derive(Debug, Serialize, Deserialize)] pub struct User { pub user_id: i64, diff --git a/src/routing/admin.rs b/src/routing/admin.rs index e2b533f..cbe411d 100644 --- a/src/routing/admin.rs +++ b/src/routing/admin.rs @@ -1,3 +1,4 @@ +pub mod comment; pub mod contact; pub mod interaction; pub mod login; @@ -19,6 +20,7 @@ pub fn route() -> Router { .nest("/user_contacts", user_contact::route()) .nest("/interactions", interaction::route()) .nest("/posts", post::route()) + .nest("/comments", comment::route()) .route_layer(axum::middleware::from_fn( builder_or_admin_by_authorization_token, )) diff --git a/src/routing/admin/comment.rs b/src/routing/admin/comment.rs new file mode 100644 index 0000000..1351692 --- /dev/null +++ b/src/routing/admin/comment.rs @@ -0,0 +1,25 @@ +use axum::{ + extract::Path, http::StatusCode, response::IntoResponse, routing::delete, Json, Router, +}; + +use crate::feature::comment::Comment; + +pub fn route() -> Router { + Router::new().route("/{comment_id}", delete(delete_)) +} + +async fn delete_(Path(comment_id): Path) -> impl IntoResponse { + match Comment::read(&comment_id).await { + Ok(comment) => match Comment::delete(&comment_id, &comment.user_id).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())), + ), + }, + Err(err_val) => ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!(err_val.to_string())), + ), + } +} diff --git a/src/routing/admin/contact.rs b/src/routing/admin/contact.rs index 0557935..ac0f004 100644 --- a/src/routing/admin/contact.rs +++ b/src/routing/admin/contact.rs @@ -16,15 +16,14 @@ struct CreateContact { #[derive(Debug, Serialize, Deserialize)] struct UpdateContact { - id: i64, name: String, } pub fn route() -> Router { Router::new() .route("/", post(create)) - .route("/", patch(update)) - .route("/{id}", delete(delete_)) + .route("/{contact_id}", patch(update)) + .route("/{contact_id}", delete(delete_)) } async fn create(Json(create_contact): Json) -> impl IntoResponse { @@ -37,8 +36,11 @@ async fn create(Json(create_contact): Json) -> impl IntoResponse } } -async fn update(Json(update_contact): Json) -> impl IntoResponse { - match Contact::update(&update_contact.id, &update_contact.name).await { +async fn update( + Path(contact_id): Path, + Json(update_contact): Json, +) -> impl IntoResponse { + match Contact::update(&contact_id, &update_contact.name).await { Ok(contact) => (StatusCode::ACCEPTED, Json(serde_json::json!(contact))), Err(err_val) => ( StatusCode::BAD_REQUEST, @@ -47,8 +49,8 @@ async fn update(Json(update_contact): Json) -> impl IntoResponse } } -async fn delete_(Path(id): Path) -> impl IntoResponse { - match Contact::delete(&id).await { +async fn delete_(Path(contact_id): Path) -> impl IntoResponse { + match Contact::delete(&contact_id).await { Ok(contact) => (StatusCode::NO_CONTENT, Json(serde_json::json!(contact))), Err(err_val) => ( StatusCode::BAD_REQUEST, diff --git a/src/routing/admin/interaction.rs b/src/routing/admin/interaction.rs index db92e8e..43b85e7 100644 --- a/src/routing/admin/interaction.rs +++ b/src/routing/admin/interaction.rs @@ -16,15 +16,14 @@ struct CreateInteraction { #[derive(Debug, Serialize, Deserialize)] struct UpdateInteraction { - id: i64, name: String, } pub fn route() -> Router { Router::new() .route("/", post(create)) - .route("/", patch(update)) - .route("/{id}", delete(delete_)) + .route("/{interaction_id}", patch(update)) + .route("/{interaction_id}", delete(delete_)) } async fn create(Json(create_interaction): Json) -> impl IntoResponse { @@ -37,8 +36,11 @@ async fn create(Json(create_interaction): Json) -> impl IntoR } } -async fn update(Json(update_interaction): Json) -> impl IntoResponse { - match Interaction::update(&update_interaction.id, &update_interaction.name).await { +async fn update( + Path(interaction_id): Path, + Json(update_interaction): Json, +) -> impl IntoResponse { + match Interaction::update(&interaction_id, &update_interaction.name).await { Ok(interaction) => (StatusCode::ACCEPTED, Json(serde_json::json!(interaction))), Err(err_val) => ( StatusCode::BAD_REQUEST, @@ -47,8 +49,8 @@ async fn update(Json(update_interaction): Json) -> impl IntoR } } -async fn delete_(Path(id): Path) -> impl IntoResponse { - match Interaction::delete(&id).await { +async fn delete_(Path(interaction_id): Path) -> impl IntoResponse { + match Interaction::delete(&interaction_id).await { Ok(interaction) => (StatusCode::NO_CONTENT, Json(serde_json::json!(interaction))), Err(err_val) => ( StatusCode::BAD_REQUEST, diff --git a/src/routing/admin/post.rs b/src/routing/admin/post.rs index 87b1f41..8e77e40 100644 --- a/src/routing/admin/post.rs +++ b/src/routing/admin/post.rs @@ -1,29 +1,22 @@ -use std::sync::Arc; - use axum::{ - extract::Path, http::StatusCode, response::IntoResponse, routing::delete, Extension, Json, - Router, + extract::Path, http::StatusCode, response::IntoResponse, routing::delete, Json, Router, }; -use chrono::{DateTime, Utc}; -use crate::{ - feature::{post::Post, user::User}, - routing::middleware::by_uri_then_insert, -}; +use crate::feature::post::Post; pub fn route() -> Router { - Router::new().route( - "/users/{user_id}/creation_times/{creation_time}", - delete(delete_).route_layer(axum::middleware::from_fn(by_uri_then_insert)), - ) + Router::new().route("/{post_id}", delete(delete_)) } -async fn delete_( - Extension(user): Extension>, - Path(creation_time): Path>, -) -> impl IntoResponse { - match Post::delete(&user.user_id, &creation_time).await { - Ok(post) => (StatusCode::NO_CONTENT, Json(serde_json::json!(post))), +async fn delete_(Path(post_id): Path) -> impl IntoResponse { + match Post::read(&post_id).await { + Ok(post) => match Post::delete(&post_id, &post.user_id).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())), + ), + }, Err(err_val) => ( StatusCode::BAD_REQUEST, Json(serde_json::json!(err_val.to_string())), diff --git a/src/routing/admin/role.rs b/src/routing/admin/role.rs index 430aa2c..b90bff9 100644 --- a/src/routing/admin/role.rs +++ b/src/routing/admin/role.rs @@ -11,18 +11,20 @@ use crate::feature::role::Role; #[derive(Debug, Serialize, Deserialize)] struct UpdateRole { - id: i64, name: String, } pub fn route() -> Router { Router::new() - .route("/", patch(update)) - .route("/{id}", delete(delete_)) + .route("/{role_id}", patch(update)) + .route("/{role_id}", delete(delete_)) } -async fn update(Json(update_role): Json) -> impl IntoResponse { - match Role::update(&update_role.id, &update_role.name).await { +async fn update( + Path(role_id): Path, + Json(update_role): Json, +) -> impl IntoResponse { + match Role::update(&role_id, &update_role.name).await { Ok(role) => (StatusCode::ACCEPTED, Json(serde_json::json!(role))), Err(err_val) => ( StatusCode::BAD_REQUEST, @@ -31,8 +33,8 @@ async fn update(Json(update_role): Json) -> impl IntoResponse { } } -async fn delete_(Path(id): Path) -> impl IntoResponse { - match Role::delete(&id).await { +async fn delete_(Path(role_id): Path) -> impl IntoResponse { + match Role::delete(&role_id).await { Ok(role) => (StatusCode::NO_CONTENT, Json(serde_json::json!(role))), Err(err_val) => ( StatusCode::BAD_REQUEST, diff --git a/src/routing/comment.rs b/src/routing/comment.rs index 9ee9649..48c2860 100644 --- a/src/routing/comment.rs +++ b/src/routing/comment.rs @@ -1,41 +1,60 @@ +use std::sync::Arc; + use axum::{ extract::Path, http::StatusCode, response::IntoResponse, routing::{delete, get, patch, post}, - Json, Router, + Extension, Json, Router, }; -use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; -use crate::feature::comment::Comment; +use crate::feature::{comment::Comment, user::User}; + +use super::middleware::by_authorization_token_then_insert; #[derive(Debug, Serialize, Deserialize)] struct CreateComment { - post_creation_time: DateTime, - user_id: i64, + post_id: i64, comment: String, } #[derive(Debug, Serialize, Deserialize)] struct UpdateComment { - creation_time: DateTime, comment: String, } pub fn route() -> Router { 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)) + .route( + "/", + post(create).route_layer(axum::middleware::from_fn( + by_authorization_token_then_insert, + )), + ) + .route("/{comment_id}", get(read)) + .route( + "/{comment_id}", + patch(update).route_layer(axum::middleware::from_fn( + by_authorization_token_then_insert, + )), + ) + .route( + "/{comment_id}", + delete(delete_).route_layer(axum::middleware::from_fn( + by_authorization_token_then_insert, + )), + ) + .route("/posts/{post_id}", get(read_all_for_post)) } -async fn create(Json(create_comment): Json) -> impl IntoResponse { +async fn create( + Extension(user): Extension>, + Json(create_comment): Json, +) -> impl IntoResponse { match Comment::create( - &create_comment.post_creation_time, - &create_comment.user_id, + &user.user_id, + &create_comment.post_id, &create_comment.comment, ) .await @@ -48,8 +67,8 @@ async fn create(Json(create_comment): Json) -> impl IntoResponse } } -async fn read(Path(creation_time): Path>) -> impl IntoResponse { - match Comment::read(&creation_time).await { +async fn read(Path(comment_id): Path) -> impl IntoResponse { + match Comment::read(&comment_id).await { Ok(comment) => (StatusCode::OK, Json(serde_json::json!(comment))), Err(err_val) => ( StatusCode::BAD_REQUEST, @@ -58,8 +77,12 @@ async fn read(Path(creation_time): Path>) -> impl IntoResponse { } } -async fn update(Json(update_comment): Json) -> impl IntoResponse { - match Comment::update(&update_comment.creation_time, &update_comment.comment).await { +async fn update( + Extension(user): Extension>, + Path(comment_id): Path, + Json(update_comment): Json, +) -> impl IntoResponse { + match Comment::update(&comment_id, &user.user_id, &update_comment.comment).await { Ok(comment) => (StatusCode::ACCEPTED, Json(serde_json::json!(comment))), Err(err_val) => ( StatusCode::BAD_REQUEST, @@ -68,8 +91,11 @@ async fn update(Json(update_comment): Json) -> impl IntoResponse } } -async fn delete_(Path(creation_time): Path>) -> impl IntoResponse { - match Comment::delete(&creation_time).await { +async fn delete_( + Extension(user): Extension>, + Path(comment_id): Path, +) -> impl IntoResponse { + match Comment::delete(&comment_id, &user.user_id).await { Ok(comment) => (StatusCode::NO_CONTENT, Json(serde_json::json!(comment))), Err(err_val) => ( StatusCode::BAD_REQUEST, @@ -78,8 +104,8 @@ async fn delete_(Path(creation_time): Path>) -> impl IntoResponse } } -async fn read_all_for_post(Path(post_creation_time): Path>) -> impl IntoResponse { - match Comment::read_all_for_post(&post_creation_time).await { +async fn read_all_for_post(Path(comment_id): Path) -> impl IntoResponse { + match Comment::read_all_for_post(&comment_id).await { Ok(comments) => (StatusCode::OK, Json(serde_json::json!(comments))), Err(err_val) => ( StatusCode::BAD_REQUEST, diff --git a/src/routing/comment_interaction.rs b/src/routing/comment_interaction.rs index 8fcbe3a..7de8ffd 100644 --- a/src/routing/comment_interaction.rs +++ b/src/routing/comment_interaction.rs @@ -1,48 +1,48 @@ +use std::sync::Arc; + use axum::{ extract::Path, http::StatusCode, response::IntoResponse, - routing::{delete, get, patch, post}, - Json, Router, + routing::{delete, get, post}, + Extension, Json, Router, }; -use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; -use crate::feature::comment_interaction::CommentInteraction; +use crate::feature::{comment_interaction::CommentInteraction, user::User}; + +use super::middleware::by_authorization_token_then_insert; #[derive(Debug, Serialize, Deserialize)] struct CreateCommentInteraction { - pub comment_creation_time: DateTime, pub interaction_id: i64, - pub user_id: i64, -} - -#[derive(Debug, Serialize, Deserialize)] -struct UpdateCommentInteraction { - pub interaction_time: DateTime, - pub comment_creation_time: DateTime, - pub interaction_id: i64, - pub user_id: i64, } pub fn route() -> Router { 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), + "/comments/{comment_id}", + post(create).route_layer(axum::middleware::from_fn( + by_authorization_token_then_insert, + )), ) + .route( + "/comments/{comment_id}", + delete(delete_).route_layer(axum::middleware::from_fn( + by_authorization_token_then_insert, + )), + ) + .route("/comments/{comment_id}", get(read_all_for_comment)) } async fn create( + Extension(user): Extension>, + Path(comment_id): Path, Json(create_comment_interaction): Json, ) -> impl IntoResponse { match CommentInteraction::create( - &create_comment_interaction.comment_creation_time, - &create_comment_interaction.user_id, + &comment_id, + &user.user_id, &create_comment_interaction.interaction_id, ) .await @@ -58,38 +58,11 @@ async fn create( } } -async fn read(Path(interaction_time): Path>) -> impl IntoResponse { - match CommentInteraction::read(&interaction_time).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( - Json(update_comment_interaction): Json, +async fn delete_( + Extension(user): Extension>, + Path(comment_id): Path, ) -> impl IntoResponse { - match CommentInteraction::update( - &update_comment_interaction.interaction_time, - &update_comment_interaction.interaction_id, - ) - .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_(Path(interaction_time): Path>) -> impl IntoResponse { - match CommentInteraction::delete(&interaction_time).await { + match CommentInteraction::delete(&comment_id, &user.user_id).await { Ok(comment_interaction) => ( StatusCode::NO_CONTENT, Json(serde_json::json!(comment_interaction)), @@ -101,10 +74,8 @@ async fn delete_(Path(interaction_time): Path>) -> impl IntoRespon } } -async fn read_all_for_comment( - Path(comment_creation_time): Path>, -) -> impl IntoResponse { - match CommentInteraction::read_all_for_comment(&comment_creation_time).await { +async fn read_all_for_comment(Path(comment_id): Path) -> impl IntoResponse { + match CommentInteraction::read_all_for_comment(&comment_id).await { Ok(comment_interactions) => ( StatusCode::OK, Json(serde_json::json!(comment_interactions)), diff --git a/src/routing/post.rs b/src/routing/post.rs index 001ee85..dba8345 100644 --- a/src/routing/post.rs +++ b/src/routing/post.rs @@ -7,7 +7,6 @@ use axum::{ routing::{delete, get, patch, post}, Extension, Json, Router, }; -use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use crate::feature::{post::Post, user::User}; @@ -21,7 +20,6 @@ struct CreatePost { #[derive(Debug, Serialize, Deserialize)] struct UpdatePost { - creation_time: DateTime, post: String, } @@ -33,16 +31,13 @@ pub fn route() -> Router { by_authorization_token_then_insert, )), ) - .route( - "/users/{user_id}/creation_times/{creation_time}", - get(read).route_layer(axum::middleware::from_fn(by_uri_then_insert)), - ) - .route("/", patch(update)) + .route("/{post_id}", get(read)) + .route("/{post_id}", patch(update)) .route_layer(axum::middleware::from_fn( by_authorization_token_then_insert, )) .route( - "/creation_times/{creation_time}", + "/{post_id}", delete(delete_).route_layer(axum::middleware::from_fn( by_authorization_token_then_insert, )), @@ -67,11 +62,8 @@ async fn create( } } -async fn read( - Extension(user): Extension>, - Path(creation_time): Path>, -) -> impl IntoResponse { - match Post::read(&user.user_id, &creation_time).await { +async fn read(Path(post_id): Path) -> impl IntoResponse { + match Post::read(&post_id).await { Ok(post) => (StatusCode::OK, Json(serde_json::json!(post))), Err(err_val) => ( StatusCode::BAD_REQUEST, @@ -82,9 +74,10 @@ async fn read( async fn update( Extension(user): Extension>, - Json(update_role): Json, + Path(post_id): Path, + Json(update_post): Json, ) -> impl IntoResponse { - match Post::update(&user.user_id, &update_role.creation_time, &update_role.post).await { + match Post::update(&post_id, &user.user_id, &update_post.post).await { Ok(post) => (StatusCode::ACCEPTED, Json(serde_json::json!(post))), Err(err_val) => ( StatusCode::BAD_REQUEST, @@ -95,9 +88,9 @@ async fn update( async fn delete_( Extension(user): Extension>, - Path(creation_time): Path>, + Path(post_id): Path, ) -> impl IntoResponse { - match Post::delete(&user.user_id, &creation_time).await { + match Post::delete(&post_id, &user.user_id).await { Ok(post) => (StatusCode::NO_CONTENT, Json(serde_json::json!(post))), Err(err_val) => ( StatusCode::BAD_REQUEST, diff --git a/src/routing/post_interaction.rs b/src/routing/post_interaction.rs index a81d3ea..e4285ca 100644 --- a/src/routing/post_interaction.rs +++ b/src/routing/post_interaction.rs @@ -1,43 +1,48 @@ +use std::sync::Arc; + use axum::{ extract::Path, http::StatusCode, response::IntoResponse, - routing::{delete, get, patch, post}, - Json, Router, + routing::{delete, get, post}, + Extension, Json, Router, }; -use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; -use crate::feature::post_interaction::PostInteraction; +use crate::feature::{post_interaction::PostInteraction, user::User}; + +use super::middleware::by_authorization_token_then_insert; #[derive(Debug, Serialize, Deserialize)] struct CreatePostInteraction { - pub post_creation_time: DateTime, pub interaction_id: i64, - pub user_id: i64, -} - -#[derive(Debug, Serialize, Deserialize)] -struct UpdatePostInteraction { - pub interaction_time: DateTime, - pub post_creation_time: DateTime, - pub interaction_id: i64, - pub user_id: i64, } pub fn route() -> Router { 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)) + .route( + "/posts/{post_id}", + post(create).route_layer(axum::middleware::from_fn( + by_authorization_token_then_insert, + )), + ) + .route( + "/posts/{post_id}", + delete(delete_).route_layer(axum::middleware::from_fn( + by_authorization_token_then_insert, + )), + ) + .route("/posts/{post_id}", get(read_all_for_post)) } -async fn create(Json(create_post_interaction): Json) -> impl IntoResponse { +async fn create( + Extension(user): Extension>, + Path(post_id): Path, + Json(create_post_interaction): Json, +) -> impl IntoResponse { match PostInteraction::create( - &create_post_interaction.post_creation_time, - &create_post_interaction.user_id, + &post_id, + &user.user_id, &create_post_interaction.interaction_id, ) .await @@ -53,36 +58,11 @@ async fn create(Json(create_post_interaction): Json) -> i } } -async fn read(Path(interaction_time): Path>) -> impl IntoResponse { - match PostInteraction::read(&interaction_time).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(Json(update_post_interaction): Json) -> impl IntoResponse { - match PostInteraction::update( - &update_post_interaction.interaction_time, - &update_post_interaction.interaction_id, - ) - .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_(Path(interaction_time): Path>) -> impl IntoResponse { - match PostInteraction::delete(&interaction_time).await { +async fn delete_( + Extension(user): Extension>, + Path(post_id): Path, +) -> impl IntoResponse { + match PostInteraction::delete(&post_id, &user.user_id).await { Ok(post_interaction) => ( StatusCode::NO_CONTENT, Json(serde_json::json!(post_interaction)), @@ -94,8 +74,8 @@ async fn delete_(Path(interaction_time): Path>) -> impl IntoRespon } } -async fn read_all_for_post(Path(post_creation_time): Path>) -> impl IntoResponse { - match PostInteraction::read_all_for_post(&post_creation_time).await { +async fn read_all_for_post(Path(post_id): Path) -> impl IntoResponse { + match PostInteraction::read_all_for_post(&post_id).await { Ok(post_interactions) => (StatusCode::OK, Json(serde_json::json!(post_interactions))), Err(err_val) => ( StatusCode::BAD_REQUEST, diff --git a/src/routing/role.rs b/src/routing/role.rs index e9478a9..cc836f5 100644 --- a/src/routing/role.rs +++ b/src/routing/role.rs @@ -4,12 +4,12 @@ use crate::feature::role::Role; pub fn route() -> Router { Router::new() - .route("/{id}", get(read)) + .route("/{role_id}", get(read)) .route("/", get(read_all)) } -async fn read(Path(id): Path) -> impl IntoResponse { - match Role::read(&id).await { +async fn read(Path(role_id): Path) -> impl IntoResponse { + match Role::read(&role_id).await { Ok(role) => (StatusCode::OK, Json(serde_json::json!(role))), Err(err_val) => ( StatusCode::BAD_REQUEST,