feat: ✨ contact rethinked and user_contact created
This commit is contained in:
parent
aa679b5a05
commit
e167b864ee
19 changed files with 261 additions and 29 deletions
|
@ -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
|
||||
);
|
2
migrations/20241215002127_user_contact.down.sql
Normal file
2
migrations/20241215002127_user_contact.down.sql
Normal file
|
@ -0,0 +1,2 @@
|
|||
-- Add down migration script here
|
||||
DROP TABLE IF EXISTS "user_contact";
|
6
migrations/20241215002127_user_contact.up.sql
Normal file
6
migrations/20241215002127_user_contact.up.sql
Normal file
|
@ -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)
|
||||
);
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -3,66 +3,63 @@ use sqlx::{Pool, Postgres};
|
|||
use crate::feature::contact::Contact;
|
||||
|
||||
pub async fn create(
|
||||
email: &String,
|
||||
name: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Contact, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<Contact, sqlx::Error> {
|
||||
pub async fn read(id: &i64, database_connection: &Pool<Postgres>) -> Result<Contact, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<Contact, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<Contact, sqlx::Error> {
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -53,7 +53,7 @@ pub async fn delete(id: &i64, database_connection: &Pool<Postgres>) -> Result<Ro
|
|||
sqlx::query_as!(
|
||||
Role,
|
||||
r#"
|
||||
DELETE FROM "role" where "id" = $1
|
||||
DELETE FROM "role" WHERE "id" = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
id
|
||||
|
|
106
src/database/role_permission.rs
Normal file
106
src/database/role_permission.rs
Normal file
|
@ -0,0 +1,106 @@
|
|||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::feature::role_permission::RolePermission;
|
||||
|
||||
pub async fn create(
|
||||
role_id: &i64,
|
||||
permission_id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<RolePermission, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<RolePermission, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<RolePermission, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<RolePermission, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<Vec<RolePermission>, 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<Postgres>,
|
||||
) -> Result<Vec<RolePermission>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
RolePermission,
|
||||
r#"
|
||||
DELETE FROM "role_permission" WHERE "role_id" = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
role_id,
|
||||
)
|
||||
.fetch_all(database_connection)
|
||||
.await
|
||||
}
|
|
@ -59,7 +59,7 @@ pub async fn delete(id: &i64, database_connection: &Pool<Postgres>) -> Result<Us
|
|||
sqlx::query_as!(
|
||||
User,
|
||||
r#"
|
||||
DELETE FROM "user" where "id" = $1
|
||||
DELETE FROM "user" WHERE "id" = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
id
|
||||
|
|
106
src/database/user_contact.rs
Normal file
106
src/database/user_contact.rs
Normal file
|
@ -0,0 +1,106 @@
|
|||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::feature::user_contact::UserContact;
|
||||
|
||||
pub async fn create(
|
||||
user_id: &i64,
|
||||
contact_id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<UserContact, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<UserContact, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<UserContact, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<UserContact, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<Vec<UserContact>, 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<Postgres>,
|
||||
) -> Result<Vec<UserContact>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
UserContact,
|
||||
r#"
|
||||
DELETE FROM "user_contact" WHERE "user_id" = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
user_id,
|
||||
)
|
||||
.fetch_all(database_connection)
|
||||
.await
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
4
src/feature/role_permission.rs
Normal file
4
src/feature/role_permission.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
pub struct RolePermission {
|
||||
pub role_id: i64,
|
||||
pub permission_id: i64,
|
||||
}
|
7
src/feature/user_contact.rs
Normal file
7
src/feature/user_contact.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct UserContact {
|
||||
pub user_id: i64,
|
||||
pub contact_id: i64,
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue