diff --git a/migrations/20241213232937_contact.up.sql b/migrations/20241213232937_contact.up.sql index ec0567d..f7f9ac3 100644 --- a/migrations/20241213232937_contact.up.sql +++ b/migrations/20241213232937_contact.up.sql @@ -1,5 +1,5 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS "contact"( - user_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, - email VARCHAR(255) NOT NULL UNIQUE + id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, + name VARCHAR(255) NOT NULL UNIQUE ); \ No newline at end of file diff --git a/migrations/20241215002127_user_contact.down.sql b/migrations/20241215002127_user_contact.down.sql new file mode 100644 index 0000000..a2b4fd4 --- /dev/null +++ b/migrations/20241215002127_user_contact.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +DROP TABLE IF EXISTS "user_contact"; \ No newline at end of file diff --git a/migrations/20241215002127_user_contact.up.sql b/migrations/20241215002127_user_contact.up.sql new file mode 100644 index 0000000..58b1404 --- /dev/null +++ b/migrations/20241215002127_user_contact.up.sql @@ -0,0 +1,6 @@ +-- Add up migration script here +CREATE TABLE IF NOT EXISTS "user_contact"( + user_id BIGSERIAL NOT NULL REFERENCES "user"(id), + contact_id BIGSERIAL NOT NULL REFERENCES "contact"(id), + PRIMARY KEY (user_id, contact_id) +); \ No newline at end of file diff --git a/src/database.rs b/src/database.rs index 041977d..2546635 100644 --- a/src/database.rs +++ b/src/database.rs @@ -6,7 +6,9 @@ pub mod permission; pub mod post; pub mod post_interaction; pub mod role; +pub mod role_permission; pub mod user; +pub mod user_contact; use std::time::Duration; diff --git a/src/database/comment.rs b/src/database/comment.rs index 53fe03e..3202320 100644 --- a/src/database/comment.rs +++ b/src/database/comment.rs @@ -64,7 +64,7 @@ pub async fn delete( sqlx::query_as!( Comment, r#" - DELETE FROM "comment" where "creation_time" = $1 + DELETE FROM "comment" WHERE "creation_time" = $1 RETURNING * "#, creation_time diff --git a/src/database/comment_interaction.rs b/src/database/comment_interaction.rs index 1a21908..ac5960b 100644 --- a/src/database/comment_interaction.rs +++ b/src/database/comment_interaction.rs @@ -64,7 +64,7 @@ pub async fn delete( sqlx::query_as!( CommentInteraction, r#" - DELETE FROM "comment_interaction" where "interaction_time" = $1 + DELETE FROM "comment_interaction" WHERE "interaction_time" = $1 RETURNING * "#, interaction_time diff --git a/src/database/contact.rs b/src/database/contact.rs index e39fafb..1dd3e0d 100644 --- a/src/database/contact.rs +++ b/src/database/contact.rs @@ -3,66 +3,63 @@ use sqlx::{Pool, Postgres}; use crate::feature::contact::Contact; pub async fn create( - email: &String, + name: &String, database_connection: &Pool, ) -> Result { sqlx::query_as!( Contact, r#" - INSERT INTO "contact"(email) + INSERT INTO "contact"(name) VALUES ($1) RETURNING * "#, - email, + name, ) .fetch_one(database_connection) .await } -pub async fn read( - user_id: &i64, - database_connection: &Pool, -) -> Result { +pub async fn read(id: &i64, database_connection: &Pool) -> Result { sqlx::query_as!( Contact, r#" - SELECT * FROM "contact" WHERE "user_id" = $1 + SELECT * FROM "contact" WHERE "id" = $1 "#, - user_id + id ) .fetch_one(database_connection) .await } pub async fn update( - user_id: &i64, - email: &String, + id: &i64, + name: &String, database_connection: &Pool, ) -> Result { sqlx::query_as!( Contact, r#" - UPDATE "contact" SET "email" = $2 WHERE "user_id" = $1 + UPDATE "contact" SET "name" = $2 WHERE "id" = $1 RETURNING * "#, - user_id, - email, + id, + name, ) .fetch_one(database_connection) .await } pub async fn delete( - user_id: &i64, + id: &i64, database_connection: &Pool, ) -> Result { sqlx::query_as!( Contact, r#" - DELETE FROM "contact" where "user_id" = $1 + DELETE FROM "contact" WHERE "id" = $1 RETURNING * "#, - user_id + id ) .fetch_one(database_connection) .await diff --git a/src/database/interaction.rs b/src/database/interaction.rs index 9300764..76b231e 100644 --- a/src/database/interaction.rs +++ b/src/database/interaction.rs @@ -59,7 +59,7 @@ pub async fn delete( sqlx::query_as!( Interaction, r#" - DELETE FROM "interaction" where "id" = $1 + DELETE FROM "interaction" WHERE "id" = $1 RETURNING * "#, id diff --git a/src/database/permission.rs b/src/database/permission.rs index e7a821c..2c9333c 100644 --- a/src/database/permission.rs +++ b/src/database/permission.rs @@ -63,7 +63,7 @@ pub async fn delete( sqlx::query_as!( Permission, r#" - DELETE FROM "role_permission" where "role_id" = $1 + DELETE FROM "role_permission" WHERE "role_id" = $1 RETURNING * "#, role_id diff --git a/src/database/post.rs b/src/database/post.rs index 9b3f0bf..3ac770f 100644 --- a/src/database/post.rs +++ b/src/database/post.rs @@ -64,7 +64,7 @@ pub async fn delete( sqlx::query_as!( Post, r#" - DELETE FROM "post" where "creation_time" = $1 + DELETE FROM "post" WHERE "creation_time" = $1 RETURNING * "#, creation_time diff --git a/src/database/post_interaction.rs b/src/database/post_interaction.rs index bffd1e4..dab6b49 100644 --- a/src/database/post_interaction.rs +++ b/src/database/post_interaction.rs @@ -64,7 +64,7 @@ pub async fn delete( sqlx::query_as!( PostInteraction, r#" - DELETE FROM "post_interaction" where "interaction_time" = $1 + DELETE FROM "post_interaction" WHERE "interaction_time" = $1 RETURNING * "#, interaction_time diff --git a/src/database/role.rs b/src/database/role.rs index cbbd62e..c12729d 100644 --- a/src/database/role.rs +++ b/src/database/role.rs @@ -53,7 +53,7 @@ pub async fn delete(id: &i64, database_connection: &Pool) -> Result, +) -> Result { + sqlx::query_as!( + RolePermission, + r#" + INSERT INTO "role_permission"(role_id, permission_id) + VALUES ($1, $2) + RETURNING * + "#, + role_id, + permission_id + ) + .fetch_one(database_connection) + .await +} + +pub async fn read( + role_id: &i64, + permission_id: &i64, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + RolePermission, + r#" + SELECT * FROM "role_permission" WHERE "role_id" = $1 AND "permission_id" = $2 + "#, + role_id, + permission_id, + ) + .fetch_one(database_connection) + .await +} + +pub async fn update( + role_id: &i64, + permission_id: &i64, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + RolePermission, + r#" + UPDATE "role_permission" SET "permission_id" = $2 WHERE "role_id" = $1 + RETURNING * + "#, + role_id, + permission_id, + ) + .fetch_one(database_connection) + .await +} + +pub async fn delete( + role_id: &i64, + permission_id: &i64, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + RolePermission, + r#" + DELETE FROM "role_permission" WHERE "role_id" = $1 AND "permission_id" = $2 + RETURNING * + "#, + role_id, + permission_id, + ) + .fetch_one(database_connection) + .await +} + +pub async fn read_all_for_role( + role_id: &i64, + database_connection: &Pool, +) -> Result, sqlx::Error> { + sqlx::query_as!( + RolePermission, + r#" + SELECT * FROM "role_permission" WHERE "role_id" = $1 + "#, + role_id + ) + .fetch_all(database_connection) + .await +} + +pub async fn delete_all_for_role( + role_id: &i64, + database_connection: &Pool, +) -> Result, sqlx::Error> { + sqlx::query_as!( + RolePermission, + r#" + DELETE FROM "role_permission" WHERE "role_id" = $1 + RETURNING * + "#, + role_id, + ) + .fetch_all(database_connection) + .await +} diff --git a/src/database/user.rs b/src/database/user.rs index 9a03bf5..010e927 100644 --- a/src/database/user.rs +++ b/src/database/user.rs @@ -59,7 +59,7 @@ pub async fn delete(id: &i64, database_connection: &Pool) -> Result, +) -> Result { + sqlx::query_as!( + UserContact, + r#" + INSERT INTO "user_contact"(user_id, contact_id) + VALUES ($1, $2) + RETURNING * + "#, + user_id, + contact_id, + ) + .fetch_one(database_connection) + .await +} + +pub async fn read( + user_id: &i64, + contact_id: &i64, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + UserContact, + r#" + SELECT * FROM "user_contact" WHERE "user_id" = $1 AND "contact_id" = $2 + "#, + user_id, + contact_id + ) + .fetch_one(database_connection) + .await +} + +pub async fn update( + user_id: &i64, + contact_id: &i64, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + UserContact, + r#" + UPDATE "user_contact" SET "contact_id" = $2 WHERE "user_id" = $1 + RETURNING * + "#, + user_id, + contact_id, + ) + .fetch_one(database_connection) + .await +} + +pub async fn delete( + user_id: &i64, + contact_id: &i64, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + UserContact, + r#" + DELETE FROM "user_contact" WHERE "user_id" = $1 AND "contact_id" = $2 + RETURNING * + "#, + user_id, + contact_id, + ) + .fetch_one(database_connection) + .await +} + +pub async fn read_all_for_user( + user_id: &i64, + database_connection: &Pool, +) -> Result, sqlx::Error> { + sqlx::query_as!( + UserContact, + r#" + SELECT * FROM "user_contact" WHERE "user_id" = $1 + "#, + user_id, + ) + .fetch_all(database_connection) + .await +} + +pub async fn delete_all_for_user( + user_id: &i64, + database_connection: &Pool, +) -> Result, sqlx::Error> { + sqlx::query_as!( + UserContact, + r#" + DELETE FROM "user_contact" WHERE "user_id" = $1 + RETURNING * + "#, + user_id, + ) + .fetch_all(database_connection) + .await +} diff --git a/src/feature.rs b/src/feature.rs index 58ee13e..77e11d4 100644 --- a/src/feature.rs +++ b/src/feature.rs @@ -6,4 +6,6 @@ pub mod permission; pub mod post; pub mod post_interaction; pub mod role; +pub mod role_permission; pub mod user; +pub mod user_contact; diff --git a/src/feature/contact.rs b/src/feature/contact.rs index cd9544b..e51a239 100644 --- a/src/feature/contact.rs +++ b/src/feature/contact.rs @@ -2,6 +2,6 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct Contact { - pub user_id: i64, - pub email: String, + pub id: i64, + pub name: String, } diff --git a/src/feature/role_permission.rs b/src/feature/role_permission.rs new file mode 100644 index 0000000..22b5ca6 --- /dev/null +++ b/src/feature/role_permission.rs @@ -0,0 +1,4 @@ +pub struct RolePermission { + pub role_id: i64, + pub permission_id: i64, +} diff --git a/src/feature/user_contact.rs b/src/feature/user_contact.rs new file mode 100644 index 0000000..13427e0 --- /dev/null +++ b/src/feature/user_contact.rs @@ -0,0 +1,7 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct UserContact { + pub user_id: i64, + pub contact_id: i64, +}