diff --git a/migrations/202206201200_initialize/down.sql b/migrations/202206171200_initialize/down.sql similarity index 100% rename from migrations/202206201200_initialize/down.sql rename to migrations/202206171200_initialize/down.sql diff --git a/migrations/202206201200_initialize/up.sql b/migrations/202206171200_initialize/up.sql similarity index 100% rename from migrations/202206201200_initialize/up.sql rename to migrations/202206171200_initialize/up.sql diff --git a/migrations/202206181200_resource/down.sql b/migrations/202206181200_resource/down.sql new file mode 100644 index 0000000..ed609f8 --- /dev/null +++ b/migrations/202206181200_resource/down.sql @@ -0,0 +1,4 @@ +DROP INDEX idx_resources_parent_id; +CREATE UNIQUE INDEX uidx_resources_key; +DROP TRIGGER tg_resources_updated_at; +DROP TABLE resources; diff --git a/migrations/202206181200_resource/up.sql b/migrations/202206181200_resource/up.sql new file mode 100644 index 0000000..38ae681 --- /dev/null +++ b/migrations/202206181200_resource/up.sql @@ -0,0 +1,24 @@ +CREATE TABLE IF NOT EXISTS resources ( + id UUID DEFAULT uuid_generate_v4(), + parent_id UUID, + name TEXT NOT NULL, + key TEXT NOT NULL, + description TEXT, + can_use BOOLEAN NOT NULL DEFAULT TRUE, + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + PRIMARY KEY (id), + CONSTRAINT fk_resources_parent_id + FOREIGN KEY(parent_id) + REFERENCES resources(id) +); + +CREATE UNIQUE INDEX uidx_resources_key ON resources (key); +CREATE INDEX idx_resources_parent_id ON resources (parent_id); + +-- trigger (updated_at) +CREATE TRIGGER tg_resources_updated_at + BEFORE UPDATE + ON resources + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); diff --git a/migrations/202206181210_resource_action/down.sql b/migrations/202206181210_resource_action/down.sql new file mode 100644 index 0000000..decb207 --- /dev/null +++ b/migrations/202206181210_resource_action/down.sql @@ -0,0 +1,4 @@ +DROP UNIQUE INDEX uidx_resource_actions_key; +DROP INDEX idx_resource_actions_resource_id; +DROP TRIGGER tg_resource_actions_updated_at; +DROP TABLE resource_actions; diff --git a/migrations/202206181210_resource_action/up.sql b/migrations/202206181210_resource_action/up.sql new file mode 100644 index 0000000..0078e03 --- /dev/null +++ b/migrations/202206181210_resource_action/up.sql @@ -0,0 +1,25 @@ +CREATE TABLE IF NOT EXISTS resource_actions ( + id UUID DEFAULT uuid_generate_v4(), + resource_id UUID NOT NULL, + name TEXT NOT NULL, + key TEXT NOT NULL, + description TEXT, + can_use BOOLEAN NOT NULL DEFAULT TRUE, + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + PRIMARY KEY (id), + CONSTRAINT fk_resource_actions_resource_id + FOREIGN KEY(resource_id) + REFERENCES resources(id) + +); + +CREATE UNIQUE INDEX uidx_resource_actions_key ON resource_actions (key); +CREATE INDEX idx_resource_actions_resource_id ON resource_actions (resource_id); + +-- trigger (updated_at) +CREATE TRIGGER tg_resource_actions_updated_at + BEFORE UPDATE + ON resource_actions + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); diff --git a/migrations/202206191200_role/down.sql b/migrations/202206191200_role/down.sql new file mode 100644 index 0000000..558be9e --- /dev/null +++ b/migrations/202206191200_role/down.sql @@ -0,0 +1,4 @@ +DROP UNIQUE INDEX uidx_roles_key; +DROP INDEX idx_roles_parent_id; +DROP TRIGGER tg_roles_updated_at; +DROP TABLE roles; diff --git a/migrations/202206191200_role/up.sql b/migrations/202206191200_role/up.sql new file mode 100644 index 0000000..fa3c3ad --- /dev/null +++ b/migrations/202206191200_role/up.sql @@ -0,0 +1,26 @@ +CREATE TABLE IF NOT EXISTS roles ( + id UUID DEFAULT uuid_generate_v4(), + parent_id UUID NOT NULL, + name TEXT NOT NULL, + key TEXT NOT NULL, + description TEXT, + can_use BOOLEAN NOT NULL DEFAULT TRUE, + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + PRIMARY KEY (id), + UNIQUE (key), + CONSTRAINT fk_roles_parent_id + FOREIGN KEY(parent_id) + REFERENCES roles(id) +); + +CREATE UNIQUE INDEX uidx_roles_key ON roles (key); +CREATE INDEX idx_roles_parent_id ON roles (parent_id); + + +-- trigger (updated_at) +CREATE TRIGGER tg_roles_updated_at + BEFORE UPDATE + ON roles + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); diff --git a/migrations/202206191210_role_resource_action/down.sql b/migrations/202206191210_role_resource_action/down.sql new file mode 100644 index 0000000..ea00365 --- /dev/null +++ b/migrations/202206191210_role_resource_action/down.sql @@ -0,0 +1,2 @@ +DROP INDEX idx_role_resource_actions_role_id; +DROP TABLE role_resource_actions; diff --git a/migrations/202206191210_role_resource_action/up.sql b/migrations/202206191210_role_resource_action/up.sql new file mode 100644 index 0000000..bbaff5f --- /dev/null +++ b/migrations/202206191210_role_resource_action/up.sql @@ -0,0 +1,14 @@ +CREATE TABLE IF NOT EXISTS role_resource_actions ( + role_id UUID NOT NULL, + resource_action_id UUID NOT NULL, + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + PRIMARY KEY (role_id, resource_action_id), + CONSTRAINT fk_role_actions_role_id + FOREIGN KEY(role_id) + REFERENCES roles(id), + CONSTRAINT fk_role_actions_resource_action_id + FOREIGN KEY(resource_action_id) + REFERENCES resource_actions(id) +); + +CREATE INDEX idx_role_resource_actions_role_id ON role_resource_actions (role_id); diff --git a/migrations/202206201210_member_site/up.sql b/migrations/202206201210_member_site/up.sql index f802a53..ae2091c 100644 --- a/migrations/202206201210_member_site/up.sql +++ b/migrations/202206201210_member_site/up.sql @@ -1,6 +1,7 @@ CREATE TABLE IF NOT EXISTS member_sites ( id UUID DEFAULT uuid_generate_v4(), url TEXT NOT NULL, + use BOOLEAN NOT NULL DEFAULT TRUE, created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), deleted_at BIGINT, diff --git a/migrations/202206201400_member_role/down.sql b/migrations/202206201400_member_role/down.sql new file mode 100644 index 0000000..18bdf35 --- /dev/null +++ b/migrations/202206201400_member_role/down.sql @@ -0,0 +1,3 @@ +DROP INDEX idx_member_roles_member_id; +DROP INDEX idx_member_roles_role_id; +DROP TABLE member_roles; diff --git a/migrations/202206201400_member_role/up.sql b/migrations/202206201400_member_role/up.sql new file mode 100644 index 0000000..f72a93d --- /dev/null +++ b/migrations/202206201400_member_role/up.sql @@ -0,0 +1,15 @@ +CREATE TABLE IF NOT EXISTS member_roles ( + member_id UUID NOT NULL, + role_id UUID NOT NULL, + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + PRIMARY KEY (member_id, role_id), + CONSTRAINT fk_member_roles_role_id + FOREIGN KEY(role_id) + REFERENCES roles(id), + CONSTRAINT fk_member_roles_member_id + FOREIGN KEY(member_id) + REFERENCES members(id) +); + +CREATE INDEX idx_member_roles_role_id ON member_roles (role_id); +CREATE INDEX idx_member_roles_member_id ON member_roles (member_id); diff --git a/migrations/202206201410_member_permission/down.sql b/migrations/202206201410_member_permission/down.sql new file mode 100644 index 0000000..a9da987 --- /dev/null +++ b/migrations/202206201410_member_permission/down.sql @@ -0,0 +1,3 @@ +DROP INDEX idx_member_permissions_member_id; +DROP INDEX idx_member_permissions_role_id; +DROP TABLE member_permissions; diff --git a/migrations/202206201410_member_permission/up.sql b/migrations/202206201410_member_permission/up.sql new file mode 100644 index 0000000..19428b7 --- /dev/null +++ b/migrations/202206201410_member_permission/up.sql @@ -0,0 +1,15 @@ +CREATE TABLE IF NOT EXISTS member_permissions ( + member_id UUID NOT NULL, + role_resource_action_id UUID NOT NULL, + created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000), + PRIMARY KEY (member_id, role_resource_action_id), + CONSTRAINT fk_member_permissions_member_id + FOREIGN KEY(member_id) + REFERENCES members(id), + CONSTRAINT fk_member_permissions_role_id + FOREIGN KEY(role_id) + REFERENCES roles(id) +); + +CREATE INDEX idx_member_permissions_role_id ON member_permissions (role_id); +CREATE INDEX idx_member_permissions_member_id ON member_permissions (member_id); diff --git a/migrations/202206201310_member_session/down.sql b/migrations/202206301310_member_session/down.sql similarity index 100% rename from migrations/202206201310_member_session/down.sql rename to migrations/202206301310_member_session/down.sql diff --git a/migrations/202206201310_member_session/up.sql b/migrations/202206301310_member_session/up.sql similarity index 100% rename from migrations/202206201310_member_session/up.sql rename to migrations/202206301310_member_session/up.sql diff --git a/src/repositories/member_permission/mod.rs b/src/repositories/member_permission/mod.rs new file mode 100644 index 0000000..bef7c9e --- /dev/null +++ b/src/repositories/member_permission/mod.rs @@ -0,0 +1,9 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/member_permission/models.rs b/src/repositories/member_permission/models.rs new file mode 100644 index 0000000..8998f1d --- /dev/null +++ b/src/repositories/member_permission/models.rs @@ -0,0 +1,38 @@ +use super::schema::member_permissions; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "member_permissions"] +#[primary_key(member_id, role_resource_action_id)] +pub struct MemberPermission { + /// + pub member_id: uuid::Uuid, + /// + pub role_resource_action_id: uuid::Uuid, + /// + pub created_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "member_permissions"] +pub struct NewMemberPermission { + /// + pub member_id: uuid::Uuid, + /// + pub role_resource_action_id: uuid::Uuid, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub role_resource_action_id: Option, + /// + pub member_id: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/member_permission/repository.rs b/src/repositories/member_permission/repository.rs new file mode 100644 index 0000000..347d28b --- /dev/null +++ b/src/repositories/member_permission/repository.rs @@ -0,0 +1,181 @@ +//! +//! +use super::{models, schema::member_permissions}; +use beteran_common_rust as bcr; +use diesel::prelude::*; +use diesel::result::Error; + +/// +pub struct Repository {} + +impl std::fmt::Debug for Repository { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Repository of member_permissions").finish() + } +} + +impl Default for Repository { + fn default() -> Self { + Self::new() + } +} + +impl Repository { + /// + pub fn new() -> Repository { + Repository {} + } + + /// + pub fn insert( + &self, + conn: &diesel::PgConnection, + new_resource: &models::NewMemberPermission, + ) -> Result { + let inserted = diesel::insert_into(member_permissions::table) + .values(new_resource) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn inserts( + &self, + conn: &diesel::PgConnection, + new_resource_actions: &Vec, + ) -> Result, Error> { + let inserted = diesel::insert_into(member_permissions::table) + .values(new_resource_actions) + .get_results(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + member_id: uuid::Uuid, + role_resource_action_id: uuid::Uuid, + ) -> Result { + use member_permissions::dsl; + + member_permissions::table + .filter( + dsl::member_id + .eq(member_id) + .and(dsl::role_resource_action_id.eq(role_resource_action_id)), + ) + .first::(conn) + } + + /// + pub fn select_all_count( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result { + let mut q = member_permissions::table.into_boxed(); + + if let Some(sp) = find_all.member_id { + q = q.filter(member_permissions::dsl::member_id.eq(sp)); + } + if let Some(sp) = find_all.role_resource_action_id { + q = q.filter(member_permissions::dsl::role_resource_action_id.eq(sp)); + } + + q.count().get_result(conn) + } + + /// + pub fn select_all( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result, Error> { + let mut q = member_permissions::table.into_boxed(); + + if let Some(sp) = find_all.member_id { + q = q.filter(member_permissions::dsl::member_id.eq(sp)); + } + if let Some(sp) = find_all.role_resource_action_id { + q = q.filter(member_permissions::dsl::role_resource_action_id.eq(sp)); + } + + if let Some(p) = find_all.pagination { + let page = p.page.unwrap_or(1); + + if let Some(page_size) = p.page_size { + q = q.offset(((page - 1) * page_size) as i64); + q = q.limit(page_size as i64); + } + } + if let Some(orderbys) = find_all.sorts { + for s in orderbys { + match s { + bcr::models::pagination::Sort::ASC(property) => match property.as_str() { + "role_resource_action_id" => { + q = q.order_by(member_permissions::role_resource_action_id.asc()); + } + "member_id" => { + q = q.order_by(member_permissions::member_id.asc()); + } + "created_at" => { + q = q.order_by(member_permissions::created_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "role_resource_action_id" => { + q = q.order_by(member_permissions::role_resource_action_id.desc()); + } + "member_id" => { + q = q.order_by(member_permissions::member_id.desc()); + } + "created_at" => { + q = q.order_by(member_permissions::created_at.desc()); + } + + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn delete( + &self, + conn: &diesel::PgConnection, + member_id: uuid::Uuid, + role_resource_action_id: uuid::Uuid, + ) -> Result { + use member_permissions::dsl; + + diesel::delete( + member_permissions::table.filter( + dsl::member_id + .eq(member_id) + .and(dsl::role_resource_action_id.eq(role_resource_action_id)), + ), + ) + .execute(conn) + .map(|c| c as u64) + } + + /// + pub fn delete_by_member_id( + &self, + conn: &diesel::PgConnection, + member_id: uuid::Uuid, + ) -> Result { + use member_permissions::dsl; + + diesel::delete(member_permissions::table.filter(dsl::member_id.eq(member_id))) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/member_permission/schema.rs b/src/repositories/member_permission/schema.rs new file mode 100644 index 0000000..e65b86a --- /dev/null +++ b/src/repositories/member_permission/schema.rs @@ -0,0 +1,14 @@ +//! +//! + +table! { + /// + member_permissions(member_id, role_resource_action_id) { + /// + member_id -> Uuid, + /// + role_resource_action_id -> Uuid, + /// + created_at -> BigInt, + } +} diff --git a/src/repositories/member_role/mod.rs b/src/repositories/member_role/mod.rs new file mode 100644 index 0000000..bef7c9e --- /dev/null +++ b/src/repositories/member_role/mod.rs @@ -0,0 +1,9 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/member_role/models.rs b/src/repositories/member_role/models.rs new file mode 100644 index 0000000..1d65f0c --- /dev/null +++ b/src/repositories/member_role/models.rs @@ -0,0 +1,38 @@ +use super::schema::member_roles; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "member_roles"] +#[primary_key(role_id, member_id)] +pub struct MemberRole { + /// + pub member_id: uuid::Uuid, + /// + pub role_id: uuid::Uuid, + /// + pub created_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "member_roles"] +pub struct NewMemberRole { + /// + pub member_id: uuid::Uuid, + /// + pub role_id: uuid::Uuid, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub role_id: Option, + /// + pub member_id: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/member_role/repository.rs b/src/repositories/member_role/repository.rs new file mode 100644 index 0000000..8aaf129 --- /dev/null +++ b/src/repositories/member_role/repository.rs @@ -0,0 +1,173 @@ +//! +//! +use super::{models, schema::member_roles}; +use beteran_common_rust as bcr; +use diesel::prelude::*; +use diesel::result::Error; + +/// +pub struct Repository {} + +impl std::fmt::Debug for Repository { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Repository of member_roles").finish() + } +} + +impl Default for Repository { + fn default() -> Self { + Self::new() + } +} + +impl Repository { + /// + pub fn new() -> Repository { + Repository {} + } + + /// + pub fn insert( + &self, + conn: &diesel::PgConnection, + new_resource: &models::NewMemberRole, + ) -> Result { + let inserted = diesel::insert_into(member_roles::table) + .values(new_resource) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn inserts( + &self, + conn: &diesel::PgConnection, + new_resource_actions: &Vec, + ) -> Result, Error> { + let inserted = diesel::insert_into(member_roles::table) + .values(new_resource_actions) + .get_results(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + member_id: uuid::Uuid, + role_id: uuid::Uuid, + ) -> Result { + use member_roles::dsl; + + member_roles::table + .filter(dsl::member_id.eq(member_id).and(dsl::role_id.eq(role_id))) + .first::(conn) + } + + /// + pub fn select_all_count( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result { + let mut q = member_roles::table.into_boxed(); + + if let Some(sp) = find_all.role_id { + q = q.filter(member_roles::dsl::role_id.eq(sp)); + } + if let Some(sp) = find_all.member_id { + q = q.filter(member_roles::dsl::member_id.eq(sp)); + } + + q.count().get_result(conn) + } + + /// + pub fn select_all( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result, Error> { + let mut q = member_roles::table.into_boxed(); + + if let Some(sp) = find_all.role_id { + q = q.filter(member_roles::dsl::role_id.eq(sp)); + } + if let Some(sp) = find_all.member_id { + q = q.filter(member_roles::dsl::member_id.eq(sp)); + } + + if let Some(p) = find_all.pagination { + let page = p.page.unwrap_or(1); + + if let Some(page_size) = p.page_size { + q = q.offset(((page - 1) * page_size) as i64); + q = q.limit(page_size as i64); + } + } + if let Some(orderbys) = find_all.sorts { + for s in orderbys { + match s { + bcr::models::pagination::Sort::ASC(property) => match property.as_str() { + "role_id" => { + q = q.order_by(member_roles::role_id.asc()); + } + "member_id" => { + q = q.order_by(member_roles::member_id.asc()); + } + "created_at" => { + q = q.order_by(member_roles::created_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "role_id" => { + q = q.order_by(member_roles::role_id.desc()); + } + "member_id" => { + q = q.order_by(member_roles::member_id.desc()); + } + "created_at" => { + q = q.order_by(member_roles::created_at.desc()); + } + + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn delete( + &self, + conn: &diesel::PgConnection, + member_id: uuid::Uuid, + role_id: uuid::Uuid, + ) -> Result { + use member_roles::dsl; + + diesel::delete( + member_roles::table.filter(dsl::member_id.eq(member_id).and(dsl::role_id.eq(role_id))), + ) + .execute(conn) + .map(|c| c as u64) + } + + /// + pub fn delete_by_member_id( + &self, + conn: &diesel::PgConnection, + member_id: uuid::Uuid, + ) -> Result { + use member_roles::dsl; + + diesel::delete(member_roles::table.filter(dsl::member_id.eq(member_id))) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/member_role/schema.rs b/src/repositories/member_role/schema.rs new file mode 100644 index 0000000..b43fa75 --- /dev/null +++ b/src/repositories/member_role/schema.rs @@ -0,0 +1,14 @@ +//! +//! + +table! { + /// + member_roles(member_id, role_id) { + /// + member_id -> Uuid, + /// + role_id -> Uuid, + /// + created_at -> BigInt, + } +} diff --git a/src/repositories/mod.rs b/src/repositories/mod.rs index 90f5fbe..f9f9829 100644 --- a/src/repositories/mod.rs +++ b/src/repositories/mod.rs @@ -1,5 +1,11 @@ pub mod member; pub mod member_class; pub mod member_level; +pub mod member_permission; +pub mod member_role; pub mod member_session; pub mod member_site; +pub mod resource; +pub mod resource_action; +pub mod role; +pub mod role_resource_action; diff --git a/src/repositories/resource/mod.rs b/src/repositories/resource/mod.rs new file mode 100644 index 0000000..bef7c9e --- /dev/null +++ b/src/repositories/resource/mod.rs @@ -0,0 +1,9 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/resource/models.rs b/src/repositories/resource/models.rs new file mode 100644 index 0000000..2896e3f --- /dev/null +++ b/src/repositories/resource/models.rs @@ -0,0 +1,83 @@ +use super::schema::resources; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "resources"] +pub struct Resource { + /// + pub id: uuid::Uuid, + /// + pub parent_id: Option, + /// + pub name: String, + /// + pub key: String, + /// + pub description: String, + /// + pub can_use: bool, + /// + pub created_at: i64, + /// + pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "resources"] +pub struct NewResource { + /// + pub parent_id: Option, + /// + pub name: String, + /// + pub key: String, + /// + pub description: String, + /// + pub can_use: bool, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "resources"] +pub struct ModifyResource { + /// + pub parent_id: Option, + /// + pub name: String, + /// + pub key: String, + /// + pub description: String, + /// + pub can_use: bool, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "resources"] +pub struct ModifyResource4CanUse { + /// + pub can_use: bool, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub parent_id: Option, + /// + pub name_like: Option, + /// + pub key_like: Option, + /// + pub description_like: Option, + /// + pub can_use: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/resource/repository.rs b/src/repositories/resource/repository.rs new file mode 100644 index 0000000..e2fe17d --- /dev/null +++ b/src/repositories/resource/repository.rs @@ -0,0 +1,188 @@ +//! +//! +use super::{models, schema::resources}; +use beteran_common_rust as bcr; +use diesel::prelude::*; +use diesel::result::Error; + +/// +pub struct Repository {} + +impl std::fmt::Debug for Repository { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Repository of resources").finish() + } +} + +impl Default for Repository { + fn default() -> Self { + Self::new() + } +} + +impl Repository { + /// + pub fn new() -> Repository { + Repository {} + } + + /// + pub fn insert( + &self, + conn: &diesel::PgConnection, + new_resource: &models::NewResource, + ) -> Result { + let inserted = diesel::insert_into(resources::table) + .values(new_resource) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + ) -> Result { + resources::table + .find(id as uuid::Uuid) + .first::(conn) + } + + /// + pub fn select_all_count( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result { + let mut q = resources::table.into_boxed(); + + if let Some(sp) = find_all.parent_id { + q = q.filter(resources::dsl::parent_id.eq(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(resources::dsl::name.like(sp)); + } + if let Some(sp) = find_all.key_like { + q = q.filter(resources::dsl::key.like(sp)); + } + if let Some(sp) = find_all.description_like { + q = q.filter(resources::dsl::description.like(sp)); + } + if let Some(sp) = find_all.can_use { + q = q.filter(resources::dsl::can_use.eq(sp)); + } + + q.count().get_result(conn) + } + + /// + pub fn select_all( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result, Error> { + let mut q = resources::table.into_boxed(); + + if let Some(sp) = find_all.parent_id { + q = q.filter(resources::dsl::parent_id.eq(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(resources::dsl::name.like(sp)); + } + if let Some(sp) = find_all.key_like { + q = q.filter(resources::dsl::key.like(sp)); + } + if let Some(sp) = find_all.description_like { + q = q.filter(resources::dsl::description.like(sp)); + } + if let Some(sp) = find_all.can_use { + q = q.filter(resources::dsl::can_use.eq(sp)); + } + + if let Some(p) = find_all.pagination { + let page = p.page.unwrap_or(1); + + if let Some(page_size) = p.page_size { + q = q.offset(((page - 1) * page_size) as i64); + q = q.limit(page_size as i64); + } + } + if let Some(orderbys) = find_all.sorts { + for s in orderbys { + match s { + bcr::models::pagination::Sort::ASC(property) => match property.as_str() { + "name" => { + q = q.order_by(resources::name.asc()); + } + "key" => { + q = q.order_by(resources::key.asc()); + } + "can_use" => { + q = q.order_by(resources::can_use.asc()); + } + "created_at" => { + q = q.order_by(resources::created_at.asc()); + } + "updated_at" => { + q = q.order_by(resources::updated_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "name" => { + q = q.order_by(resources::name.desc()); + } + "key" => { + q = q.order_by(resources::key.desc()); + } + "can_use" => { + q = q.order_by(resources::can_use.desc()); + } + "created_at" => { + q = q.order_by(resources::created_at.desc()); + } + "updated_at" => { + q = q.order_by(resources::updated_at.desc()); + } + + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn update( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyResource, + ) -> Result { + use resources::dsl; + + diesel::update(dsl::resources.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } + + /// + pub fn update_can_use( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyResource4CanUse, + ) -> Result { + use resources::dsl; + + diesel::update(dsl::resources.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/resource/schema.rs b/src/repositories/resource/schema.rs new file mode 100644 index 0000000..6d43416 --- /dev/null +++ b/src/repositories/resource/schema.rs @@ -0,0 +1,24 @@ +//! +//! + +table! { + /// + resources(id) { + /// + id -> Uuid, + /// + parent_id -> Nullable, + /// + name -> Text, + /// + key -> Text, + /// + description -> Text, + /// + can_use -> Bool, + /// + created_at -> BigInt, + /// + updated_at -> BigInt, + } +} diff --git a/src/repositories/resource_action/mod.rs b/src/repositories/resource_action/mod.rs new file mode 100644 index 0000000..bef7c9e --- /dev/null +++ b/src/repositories/resource_action/mod.rs @@ -0,0 +1,9 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/resource_action/models.rs b/src/repositories/resource_action/models.rs new file mode 100644 index 0000000..cd965c6 --- /dev/null +++ b/src/repositories/resource_action/models.rs @@ -0,0 +1,83 @@ +use super::schema::resource_actions; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "resource_actions"] +pub struct ResourceAction { + /// + pub id: uuid::Uuid, + /// + pub resource_id: uuid::Uuid, + /// + pub name: String, + /// + pub key: String, + /// + pub description: String, + /// + pub can_use: bool, + /// + pub created_at: i64, + /// + pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "resource_actions"] +pub struct NewResourceAction { + /// + pub resource_id: uuid::Uuid, + /// + pub name: String, + /// + pub key: String, + /// + pub description: String, + /// + pub can_use: bool, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "resource_actions"] +pub struct ModifyResourceAction { + /// + pub resource_id: uuid::Uuid, + /// + pub name: String, + /// + pub key: String, + /// + pub description: String, + /// + pub can_use: bool, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "resource_actions"] +pub struct ModifyResourceAction4CanUse { + /// + pub can_use: bool, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub resource_id: Option, + /// + pub name_like: Option, + /// + pub key_like: Option, + /// + pub description_like: Option, + /// + pub can_use: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/resource_action/repository.rs b/src/repositories/resource_action/repository.rs new file mode 100644 index 0000000..aed23dc --- /dev/null +++ b/src/repositories/resource_action/repository.rs @@ -0,0 +1,188 @@ +//! +//! +use super::{models, schema::resource_actions}; +use beteran_common_rust as bcr; +use diesel::prelude::*; +use diesel::result::Error; + +/// +pub struct Repository {} + +impl std::fmt::Debug for Repository { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Repository of resource_actions").finish() + } +} + +impl Default for Repository { + fn default() -> Self { + Self::new() + } +} + +impl Repository { + /// + pub fn new() -> Repository { + Repository {} + } + + /// + pub fn insert( + &self, + conn: &diesel::PgConnection, + new_resource: &models::NewResourceAction, + ) -> Result { + let inserted = diesel::insert_into(resource_actions::table) + .values(new_resource) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + ) -> Result { + resource_actions::table + .find(id as uuid::Uuid) + .first::(conn) + } + + /// + pub fn select_all_count( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result { + let mut q = resource_actions::table.into_boxed(); + + if let Some(sp) = find_all.resource_id { + q = q.filter(resource_actions::dsl::resource_id.eq(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(resource_actions::dsl::name.like(sp)); + } + if let Some(sp) = find_all.key_like { + q = q.filter(resource_actions::dsl::key.like(sp)); + } + if let Some(sp) = find_all.description_like { + q = q.filter(resource_actions::dsl::description.like(sp)); + } + if let Some(sp) = find_all.can_use { + q = q.filter(resource_actions::dsl::can_use.eq(sp)); + } + + q.count().get_result(conn) + } + + /// + pub fn select_all( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result, Error> { + let mut q = resource_actions::table.into_boxed(); + + if let Some(sp) = find_all.resource_id { + q = q.filter(resource_actions::dsl::resource_id.eq(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(resource_actions::dsl::name.like(sp)); + } + if let Some(sp) = find_all.key_like { + q = q.filter(resource_actions::dsl::key.like(sp)); + } + if let Some(sp) = find_all.description_like { + q = q.filter(resource_actions::dsl::description.like(sp)); + } + if let Some(sp) = find_all.can_use { + q = q.filter(resource_actions::dsl::can_use.eq(sp)); + } + + if let Some(p) = find_all.pagination { + let page = p.page.unwrap_or(1); + + if let Some(page_size) = p.page_size { + q = q.offset(((page - 1) * page_size) as i64); + q = q.limit(page_size as i64); + } + } + if let Some(orderbys) = find_all.sorts { + for s in orderbys { + match s { + bcr::models::pagination::Sort::ASC(property) => match property.as_str() { + "name" => { + q = q.order_by(resource_actions::name.asc()); + } + "key" => { + q = q.order_by(resource_actions::key.asc()); + } + "can_use" => { + q = q.order_by(resource_actions::can_use.asc()); + } + "created_at" => { + q = q.order_by(resource_actions::created_at.asc()); + } + "updated_at" => { + q = q.order_by(resource_actions::updated_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "name" => { + q = q.order_by(resource_actions::name.desc()); + } + "key" => { + q = q.order_by(resource_actions::key.desc()); + } + "can_use" => { + q = q.order_by(resource_actions::can_use.desc()); + } + "created_at" => { + q = q.order_by(resource_actions::created_at.desc()); + } + "updated_at" => { + q = q.order_by(resource_actions::updated_at.desc()); + } + + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn update( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyResourceAction, + ) -> Result { + use resource_actions::dsl; + + diesel::update(dsl::resource_actions.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } + + /// + pub fn update_can_use( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyResourceAction4CanUse, + ) -> Result { + use resource_actions::dsl; + + diesel::update(dsl::resource_actions.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/resource_action/schema.rs b/src/repositories/resource_action/schema.rs new file mode 100644 index 0000000..2ab6d9c --- /dev/null +++ b/src/repositories/resource_action/schema.rs @@ -0,0 +1,24 @@ +//! +//! + +table! { + /// + resource_actions(id) { + /// + id -> Uuid, + /// + resource_id -> Uuid, + /// + name -> Text, + /// + key -> Text, + /// + description -> Text, + /// + can_use -> Bool, + /// + created_at -> BigInt, + /// + updated_at -> BigInt, + } +} diff --git a/src/repositories/role/mod.rs b/src/repositories/role/mod.rs new file mode 100644 index 0000000..bef7c9e --- /dev/null +++ b/src/repositories/role/mod.rs @@ -0,0 +1,9 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/role/models.rs b/src/repositories/role/models.rs new file mode 100644 index 0000000..809d8e9 --- /dev/null +++ b/src/repositories/role/models.rs @@ -0,0 +1,83 @@ +use super::schema::roles; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "roles"] +pub struct Role { + /// + pub id: uuid::Uuid, + /// + pub parent_id: Option, + /// + pub name: String, + /// + pub key: String, + /// + pub description: String, + /// + pub can_use: bool, + /// + pub created_at: i64, + /// + pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "roles"] +pub struct NewRole { + /// + pub parent_id: Option, + /// + pub name: String, + /// + pub key: String, + /// + pub description: String, + /// + pub can_use: bool, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "roles"] +pub struct ModifyRole { + /// + pub parent_id: Option, + /// + pub name: String, + /// + pub key: String, + /// + pub description: String, + /// + pub can_use: bool, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "roles"] +pub struct ModifyRole4CanUse { + /// + pub can_use: bool, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub parent_id: Option, + /// + pub name_like: Option, + /// + pub key_like: Option, + /// + pub description_like: Option, + /// + pub can_use: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/role/repository.rs b/src/repositories/role/repository.rs new file mode 100644 index 0000000..0dc1c20 --- /dev/null +++ b/src/repositories/role/repository.rs @@ -0,0 +1,184 @@ +//! +//! +use super::{models, schema::roles}; +use beteran_common_rust as bcr; +use diesel::prelude::*; +use diesel::result::Error; + +/// +pub struct Repository {} + +impl std::fmt::Debug for Repository { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Repository of roles").finish() + } +} + +impl Default for Repository { + fn default() -> Self { + Self::new() + } +} + +impl Repository { + /// + pub fn new() -> Repository { + Repository {} + } + + /// + pub fn insert( + &self, + conn: &diesel::PgConnection, + new_resource: &models::NewRole, + ) -> Result { + let inserted = diesel::insert_into(roles::table) + .values(new_resource) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn select(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result { + roles::table + .find(id as uuid::Uuid) + .first::(conn) + } + + /// + pub fn select_all_count( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result { + let mut q = roles::table.into_boxed(); + + if let Some(sp) = find_all.parent_id { + q = q.filter(roles::dsl::parent_id.eq(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(roles::dsl::name.like(sp)); + } + if let Some(sp) = find_all.key_like { + q = q.filter(roles::dsl::key.like(sp)); + } + if let Some(sp) = find_all.description_like { + q = q.filter(roles::dsl::description.like(sp)); + } + if let Some(sp) = find_all.can_use { + q = q.filter(roles::dsl::can_use.eq(sp)); + } + + q.count().get_result(conn) + } + + /// + pub fn select_all( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result, Error> { + let mut q = roles::table.into_boxed(); + + if let Some(sp) = find_all.parent_id { + q = q.filter(roles::dsl::parent_id.eq(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(roles::dsl::name.like(sp)); + } + if let Some(sp) = find_all.key_like { + q = q.filter(roles::dsl::key.like(sp)); + } + if let Some(sp) = find_all.description_like { + q = q.filter(roles::dsl::description.like(sp)); + } + if let Some(sp) = find_all.can_use { + q = q.filter(roles::dsl::can_use.eq(sp)); + } + + if let Some(p) = find_all.pagination { + let page = p.page.unwrap_or(1); + + if let Some(page_size) = p.page_size { + q = q.offset(((page - 1) * page_size) as i64); + q = q.limit(page_size as i64); + } + } + if let Some(orderbys) = find_all.sorts { + for s in orderbys { + match s { + bcr::models::pagination::Sort::ASC(property) => match property.as_str() { + "name" => { + q = q.order_by(roles::name.asc()); + } + "key" => { + q = q.order_by(roles::key.asc()); + } + "can_use" => { + q = q.order_by(roles::can_use.asc()); + } + "created_at" => { + q = q.order_by(roles::created_at.asc()); + } + "updated_at" => { + q = q.order_by(roles::updated_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "name" => { + q = q.order_by(roles::name.desc()); + } + "key" => { + q = q.order_by(roles::key.desc()); + } + "can_use" => { + q = q.order_by(roles::can_use.desc()); + } + "created_at" => { + q = q.order_by(roles::created_at.desc()); + } + "updated_at" => { + q = q.order_by(roles::updated_at.desc()); + } + + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn update( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyRole, + ) -> Result { + use roles::dsl; + + diesel::update(dsl::roles.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } + + /// + pub fn update_can_use( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyRole4CanUse, + ) -> Result { + use roles::dsl; + + diesel::update(dsl::roles.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/role/schema.rs b/src/repositories/role/schema.rs new file mode 100644 index 0000000..a763b60 --- /dev/null +++ b/src/repositories/role/schema.rs @@ -0,0 +1,24 @@ +//! +//! + +table! { + /// + roles(id) { + /// + id -> Uuid, + /// + parent_id -> Nullable, + /// + name -> Text, + /// + key -> Text, + /// + description -> Text, + /// + can_use -> Bool, + /// + created_at -> BigInt, + /// + updated_at -> BigInt, + } +} diff --git a/src/repositories/role_resource_action/mod.rs b/src/repositories/role_resource_action/mod.rs new file mode 100644 index 0000000..bef7c9e --- /dev/null +++ b/src/repositories/role_resource_action/mod.rs @@ -0,0 +1,9 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/role_resource_action/models.rs b/src/repositories/role_resource_action/models.rs new file mode 100644 index 0000000..5f4a898 --- /dev/null +++ b/src/repositories/role_resource_action/models.rs @@ -0,0 +1,38 @@ +use super::schema::role_resource_actions; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "role_resource_actions"] +#[primary_key(role_id, resource_action_id)] +pub struct RoleResourceAction { + /// + pub role_id: uuid::Uuid, + /// + pub resource_action_id: uuid::Uuid, + /// + pub created_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "role_resource_actions"] +pub struct NewRoleResourceAction { + /// + pub role_id: uuid::Uuid, + /// + pub resource_action_id: uuid::Uuid, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub role_id: Option, + /// + pub resource_action_id: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/role_resource_action/repository.rs b/src/repositories/role_resource_action/repository.rs new file mode 100644 index 0000000..b234641 --- /dev/null +++ b/src/repositories/role_resource_action/repository.rs @@ -0,0 +1,182 @@ +//! +//! +use super::{models, schema::role_resource_actions}; +use beteran_common_rust as bcr; +use diesel::prelude::*; +use diesel::result::Error; + +/// +pub struct Repository {} + +impl std::fmt::Debug for Repository { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Repository of role_resource_actions") + .finish() + } +} + +impl Default for Repository { + fn default() -> Self { + Self::new() + } +} + +impl Repository { + /// + pub fn new() -> Repository { + Repository {} + } + + /// + pub fn insert( + &self, + conn: &diesel::PgConnection, + new_resource: &models::NewRoleResourceAction, + ) -> Result { + let inserted = diesel::insert_into(role_resource_actions::table) + .values(new_resource) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn inserts( + &self, + conn: &diesel::PgConnection, + new_resource_actions: &Vec, + ) -> Result, Error> { + let inserted = diesel::insert_into(role_resource_actions::table) + .values(new_resource_actions) + .get_results(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + role_id: uuid::Uuid, + resource_action_id: uuid::Uuid, + ) -> Result { + use role_resource_actions::dsl; + + role_resource_actions::table + .filter( + dsl::role_id + .eq(role_id) + .and(dsl::resource_action_id.eq(resource_action_id)), + ) + .first::(conn) + } + + /// + pub fn select_all_count( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result { + let mut q = role_resource_actions::table.into_boxed(); + + if let Some(sp) = find_all.role_id { + q = q.filter(role_resource_actions::dsl::role_id.eq(sp)); + } + if let Some(sp) = find_all.resource_action_id { + q = q.filter(role_resource_actions::dsl::resource_action_id.eq(sp)); + } + + q.count().get_result(conn) + } + + /// + pub fn select_all( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result, Error> { + let mut q = role_resource_actions::table.into_boxed(); + + if let Some(sp) = find_all.role_id { + q = q.filter(role_resource_actions::dsl::role_id.eq(sp)); + } + if let Some(sp) = find_all.resource_action_id { + q = q.filter(role_resource_actions::dsl::resource_action_id.eq(sp)); + } + + if let Some(p) = find_all.pagination { + let page = p.page.unwrap_or(1); + + if let Some(page_size) = p.page_size { + q = q.offset(((page - 1) * page_size) as i64); + q = q.limit(page_size as i64); + } + } + if let Some(orderbys) = find_all.sorts { + for s in orderbys { + match s { + bcr::models::pagination::Sort::ASC(property) => match property.as_str() { + "role_id" => { + q = q.order_by(role_resource_actions::role_id.asc()); + } + "resource_action_id" => { + q = q.order_by(role_resource_actions::resource_action_id.asc()); + } + "created_at" => { + q = q.order_by(role_resource_actions::created_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "role_id" => { + q = q.order_by(role_resource_actions::role_id.desc()); + } + "resource_action_id" => { + q = q.order_by(role_resource_actions::resource_action_id.desc()); + } + "created_at" => { + q = q.order_by(role_resource_actions::created_at.desc()); + } + + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn delete( + &self, + conn: &diesel::PgConnection, + role_id: uuid::Uuid, + resource_action_id: uuid::Uuid, + ) -> Result { + use role_resource_actions::dsl; + + diesel::delete( + role_resource_actions::table.filter( + dsl::role_id + .eq(role_id) + .and(dsl::resource_action_id.eq(resource_action_id)), + ), + ) + .execute(conn) + .map(|c| c as u64) + } + + /// + pub fn delete_by_role_id( + &self, + conn: &diesel::PgConnection, + role_id: uuid::Uuid, + ) -> Result { + use role_resource_actions::dsl; + + diesel::delete(role_resource_actions::table.filter(dsl::role_id.eq(role_id))) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/role_resource_action/schema.rs b/src/repositories/role_resource_action/schema.rs new file mode 100644 index 0000000..f051242 --- /dev/null +++ b/src/repositories/role_resource_action/schema.rs @@ -0,0 +1,14 @@ +//! +//! + +table! { + /// + role_resource_actions(role_id, resource_action_id) { + /// + role_id -> Uuid, + /// + resource_action_id -> Uuid, + /// + created_at -> BigInt, + } +}