rbac is added
This commit is contained in:
parent
362b8d73ee
commit
34a61cd2c8
4
migrations/202206181200_resource/down.sql
Normal file
4
migrations/202206181200_resource/down.sql
Normal file
|
@ -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;
|
24
migrations/202206181200_resource/up.sql
Normal file
24
migrations/202206181200_resource/up.sql
Normal file
|
@ -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();
|
4
migrations/202206181210_resource_action/down.sql
Normal file
4
migrations/202206181210_resource_action/down.sql
Normal file
|
@ -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;
|
25
migrations/202206181210_resource_action/up.sql
Normal file
25
migrations/202206181210_resource_action/up.sql
Normal file
|
@ -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();
|
4
migrations/202206191200_role/down.sql
Normal file
4
migrations/202206191200_role/down.sql
Normal file
|
@ -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;
|
26
migrations/202206191200_role/up.sql
Normal file
26
migrations/202206191200_role/up.sql
Normal file
|
@ -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();
|
2
migrations/202206191210_role_resource_action/down.sql
Normal file
2
migrations/202206191210_role_resource_action/down.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
DROP INDEX idx_role_resource_actions_role_id;
|
||||||
|
DROP TABLE role_resource_actions;
|
14
migrations/202206191210_role_resource_action/up.sql
Normal file
14
migrations/202206191210_role_resource_action/up.sql
Normal file
|
@ -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);
|
|
@ -1,6 +1,7 @@
|
||||||
CREATE TABLE IF NOT EXISTS member_sites (
|
CREATE TABLE IF NOT EXISTS member_sites (
|
||||||
id UUID DEFAULT uuid_generate_v4(),
|
id UUID DEFAULT uuid_generate_v4(),
|
||||||
url TEXT NOT NULL,
|
url TEXT NOT NULL,
|
||||||
|
use BOOLEAN NOT NULL DEFAULT TRUE,
|
||||||
created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
|
created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
|
||||||
updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
|
updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
|
||||||
deleted_at BIGINT,
|
deleted_at BIGINT,
|
||||||
|
|
3
migrations/202206201400_member_role/down.sql
Normal file
3
migrations/202206201400_member_role/down.sql
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
DROP INDEX idx_member_roles_member_id;
|
||||||
|
DROP INDEX idx_member_roles_role_id;
|
||||||
|
DROP TABLE member_roles;
|
15
migrations/202206201400_member_role/up.sql
Normal file
15
migrations/202206201400_member_role/up.sql
Normal file
|
@ -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);
|
3
migrations/202206201410_member_permission/down.sql
Normal file
3
migrations/202206201410_member_permission/down.sql
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
DROP INDEX idx_member_permissions_member_id;
|
||||||
|
DROP INDEX idx_member_permissions_role_id;
|
||||||
|
DROP TABLE member_permissions;
|
15
migrations/202206201410_member_permission/up.sql
Normal file
15
migrations/202206201410_member_permission/up.sql
Normal file
|
@ -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);
|
9
src/repositories/member_permission/mod.rs
Normal file
9
src/repositories/member_permission/mod.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
|
||||||
|
///
|
||||||
|
pub mod models;
|
||||||
|
///
|
||||||
|
pub mod repository;
|
||||||
|
///
|
||||||
|
pub mod schema;
|
38
src/repositories/member_permission/models.rs
Normal file
38
src/repositories/member_permission/models.rs
Normal file
|
@ -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<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
pub member_id: Option<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
pub pagination: Option<bcr::models::pagination::Pagination>,
|
||||||
|
///
|
||||||
|
pub sorts: Option<Vec<bcr::models::pagination::Sort>>,
|
||||||
|
}
|
181
src/repositories/member_permission/repository.rs
Normal file
181
src/repositories/member_permission/repository.rs
Normal file
|
@ -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<models::MemberPermission, Error> {
|
||||||
|
let inserted = diesel::insert_into(member_permissions::table)
|
||||||
|
.values(new_resource)
|
||||||
|
.get_result::<models::MemberPermission>(conn)?;
|
||||||
|
|
||||||
|
Ok(inserted)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn inserts(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
new_resource_actions: &Vec<models::NewMemberPermission>,
|
||||||
|
) -> Result<Vec<models::MemberPermission>, 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<models::MemberPermission, Error> {
|
||||||
|
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::<models::MemberPermission>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all_count(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<i64, 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
q.count().get_result(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<Vec<models::MemberPermission>, 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::<models::MemberPermission>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn delete(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
member_id: uuid::Uuid,
|
||||||
|
role_resource_action_id: uuid::Uuid,
|
||||||
|
) -> Result<u64, Error> {
|
||||||
|
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<u64, Error> {
|
||||||
|
use member_permissions::dsl;
|
||||||
|
|
||||||
|
diesel::delete(member_permissions::table.filter(dsl::member_id.eq(member_id)))
|
||||||
|
.execute(conn)
|
||||||
|
.map(|c| c as u64)
|
||||||
|
}
|
||||||
|
}
|
14
src/repositories/member_permission/schema.rs
Normal file
14
src/repositories/member_permission/schema.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
|
||||||
|
table! {
|
||||||
|
///
|
||||||
|
member_permissions(member_id, role_resource_action_id) {
|
||||||
|
///
|
||||||
|
member_id -> Uuid,
|
||||||
|
///
|
||||||
|
role_resource_action_id -> Uuid,
|
||||||
|
///
|
||||||
|
created_at -> BigInt,
|
||||||
|
}
|
||||||
|
}
|
9
src/repositories/member_role/mod.rs
Normal file
9
src/repositories/member_role/mod.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
|
||||||
|
///
|
||||||
|
pub mod models;
|
||||||
|
///
|
||||||
|
pub mod repository;
|
||||||
|
///
|
||||||
|
pub mod schema;
|
38
src/repositories/member_role/models.rs
Normal file
38
src/repositories/member_role/models.rs
Normal file
|
@ -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<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
pub member_id: Option<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
pub pagination: Option<bcr::models::pagination::Pagination>,
|
||||||
|
///
|
||||||
|
pub sorts: Option<Vec<bcr::models::pagination::Sort>>,
|
||||||
|
}
|
173
src/repositories/member_role/repository.rs
Normal file
173
src/repositories/member_role/repository.rs
Normal file
|
@ -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<models::MemberRole, Error> {
|
||||||
|
let inserted = diesel::insert_into(member_roles::table)
|
||||||
|
.values(new_resource)
|
||||||
|
.get_result::<models::MemberRole>(conn)?;
|
||||||
|
|
||||||
|
Ok(inserted)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn inserts(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
new_resource_actions: &Vec<models::NewMemberRole>,
|
||||||
|
) -> Result<Vec<models::MemberRole>, 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<models::MemberRole, Error> {
|
||||||
|
use member_roles::dsl;
|
||||||
|
|
||||||
|
member_roles::table
|
||||||
|
.filter(dsl::member_id.eq(member_id).and(dsl::role_id.eq(role_id)))
|
||||||
|
.first::<models::MemberRole>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all_count(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<i64, 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
q.count().get_result(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<Vec<models::MemberRole>, 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::<models::MemberRole>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn delete(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
member_id: uuid::Uuid,
|
||||||
|
role_id: uuid::Uuid,
|
||||||
|
) -> Result<u64, Error> {
|
||||||
|
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<u64, Error> {
|
||||||
|
use member_roles::dsl;
|
||||||
|
|
||||||
|
diesel::delete(member_roles::table.filter(dsl::member_id.eq(member_id)))
|
||||||
|
.execute(conn)
|
||||||
|
.map(|c| c as u64)
|
||||||
|
}
|
||||||
|
}
|
14
src/repositories/member_role/schema.rs
Normal file
14
src/repositories/member_role/schema.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
|
||||||
|
table! {
|
||||||
|
///
|
||||||
|
member_roles(member_id, role_id) {
|
||||||
|
///
|
||||||
|
member_id -> Uuid,
|
||||||
|
///
|
||||||
|
role_id -> Uuid,
|
||||||
|
///
|
||||||
|
created_at -> BigInt,
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,11 @@
|
||||||
pub mod member;
|
pub mod member;
|
||||||
pub mod member_class;
|
pub mod member_class;
|
||||||
pub mod member_level;
|
pub mod member_level;
|
||||||
|
pub mod member_permission;
|
||||||
|
pub mod member_role;
|
||||||
pub mod member_session;
|
pub mod member_session;
|
||||||
pub mod member_site;
|
pub mod member_site;
|
||||||
|
pub mod resource;
|
||||||
|
pub mod resource_action;
|
||||||
|
pub mod role;
|
||||||
|
pub mod role_resource_action;
|
||||||
|
|
9
src/repositories/resource/mod.rs
Normal file
9
src/repositories/resource/mod.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
|
||||||
|
///
|
||||||
|
pub mod models;
|
||||||
|
///
|
||||||
|
pub mod repository;
|
||||||
|
///
|
||||||
|
pub mod schema;
|
83
src/repositories/resource/models.rs
Normal file
83
src/repositories/resource/models.rs
Normal file
|
@ -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<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 = "resources"]
|
||||||
|
pub struct NewResource {
|
||||||
|
///
|
||||||
|
pub parent_id: Option<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
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<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
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<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
pub name_like: Option<String>,
|
||||||
|
///
|
||||||
|
pub key_like: Option<String>,
|
||||||
|
///
|
||||||
|
pub description_like: Option<String>,
|
||||||
|
///
|
||||||
|
pub can_use: Option<bool>,
|
||||||
|
///
|
||||||
|
pub pagination: Option<bcr::models::pagination::Pagination>,
|
||||||
|
///
|
||||||
|
pub sorts: Option<Vec<bcr::models::pagination::Sort>>,
|
||||||
|
}
|
188
src/repositories/resource/repository.rs
Normal file
188
src/repositories/resource/repository.rs
Normal file
|
@ -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<models::Resource, Error> {
|
||||||
|
let inserted = diesel::insert_into(resources::table)
|
||||||
|
.values(new_resource)
|
||||||
|
.get_result::<models::Resource>(conn)?;
|
||||||
|
|
||||||
|
Ok(inserted)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
id: uuid::Uuid,
|
||||||
|
) -> Result<models::Resource, Error> {
|
||||||
|
resources::table
|
||||||
|
.find(id as uuid::Uuid)
|
||||||
|
.first::<models::Resource>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all_count(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<i64, 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
q.count().get_result(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<Vec<models::Resource>, 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::<models::Resource>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn update(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
id: uuid::Uuid,
|
||||||
|
modify: &models::ModifyResource,
|
||||||
|
) -> Result<u64, Error> {
|
||||||
|
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<u64, Error> {
|
||||||
|
use resources::dsl;
|
||||||
|
|
||||||
|
diesel::update(dsl::resources.filter(dsl::id.eq(id)))
|
||||||
|
.set(modify)
|
||||||
|
.execute(conn)
|
||||||
|
.map(|c| c as u64)
|
||||||
|
}
|
||||||
|
}
|
24
src/repositories/resource/schema.rs
Normal file
24
src/repositories/resource/schema.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
|
||||||
|
table! {
|
||||||
|
///
|
||||||
|
resources(id) {
|
||||||
|
///
|
||||||
|
id -> Uuid,
|
||||||
|
///
|
||||||
|
parent_id -> Nullable<Uuid>,
|
||||||
|
///
|
||||||
|
name -> Text,
|
||||||
|
///
|
||||||
|
key -> Text,
|
||||||
|
///
|
||||||
|
description -> Text,
|
||||||
|
///
|
||||||
|
can_use -> Bool,
|
||||||
|
///
|
||||||
|
created_at -> BigInt,
|
||||||
|
///
|
||||||
|
updated_at -> BigInt,
|
||||||
|
}
|
||||||
|
}
|
9
src/repositories/resource_action/mod.rs
Normal file
9
src/repositories/resource_action/mod.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
|
||||||
|
///
|
||||||
|
pub mod models;
|
||||||
|
///
|
||||||
|
pub mod repository;
|
||||||
|
///
|
||||||
|
pub mod schema;
|
83
src/repositories/resource_action/models.rs
Normal file
83
src/repositories/resource_action/models.rs
Normal file
|
@ -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<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
pub name_like: Option<String>,
|
||||||
|
///
|
||||||
|
pub key_like: Option<String>,
|
||||||
|
///
|
||||||
|
pub description_like: Option<String>,
|
||||||
|
///
|
||||||
|
pub can_use: Option<bool>,
|
||||||
|
///
|
||||||
|
pub pagination: Option<bcr::models::pagination::Pagination>,
|
||||||
|
///
|
||||||
|
pub sorts: Option<Vec<bcr::models::pagination::Sort>>,
|
||||||
|
}
|
188
src/repositories/resource_action/repository.rs
Normal file
188
src/repositories/resource_action/repository.rs
Normal file
|
@ -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<models::ResourceAction, Error> {
|
||||||
|
let inserted = diesel::insert_into(resource_actions::table)
|
||||||
|
.values(new_resource)
|
||||||
|
.get_result::<models::ResourceAction>(conn)?;
|
||||||
|
|
||||||
|
Ok(inserted)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
id: uuid::Uuid,
|
||||||
|
) -> Result<models::ResourceAction, Error> {
|
||||||
|
resource_actions::table
|
||||||
|
.find(id as uuid::Uuid)
|
||||||
|
.first::<models::ResourceAction>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all_count(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<i64, 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
q.count().get_result(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<Vec<models::ResourceAction>, 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::<models::ResourceAction>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn update(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
id: uuid::Uuid,
|
||||||
|
modify: &models::ModifyResourceAction,
|
||||||
|
) -> Result<u64, Error> {
|
||||||
|
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<u64, Error> {
|
||||||
|
use resource_actions::dsl;
|
||||||
|
|
||||||
|
diesel::update(dsl::resource_actions.filter(dsl::id.eq(id)))
|
||||||
|
.set(modify)
|
||||||
|
.execute(conn)
|
||||||
|
.map(|c| c as u64)
|
||||||
|
}
|
||||||
|
}
|
24
src/repositories/resource_action/schema.rs
Normal file
24
src/repositories/resource_action/schema.rs
Normal file
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
9
src/repositories/role/mod.rs
Normal file
9
src/repositories/role/mod.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
|
||||||
|
///
|
||||||
|
pub mod models;
|
||||||
|
///
|
||||||
|
pub mod repository;
|
||||||
|
///
|
||||||
|
pub mod schema;
|
83
src/repositories/role/models.rs
Normal file
83
src/repositories/role/models.rs
Normal file
|
@ -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<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 = "roles"]
|
||||||
|
pub struct NewRole {
|
||||||
|
///
|
||||||
|
pub parent_id: Option<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
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<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
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<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
pub name_like: Option<String>,
|
||||||
|
///
|
||||||
|
pub key_like: Option<String>,
|
||||||
|
///
|
||||||
|
pub description_like: Option<String>,
|
||||||
|
///
|
||||||
|
pub can_use: Option<bool>,
|
||||||
|
///
|
||||||
|
pub pagination: Option<bcr::models::pagination::Pagination>,
|
||||||
|
///
|
||||||
|
pub sorts: Option<Vec<bcr::models::pagination::Sort>>,
|
||||||
|
}
|
184
src/repositories/role/repository.rs
Normal file
184
src/repositories/role/repository.rs
Normal file
|
@ -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<models::Role, Error> {
|
||||||
|
let inserted = diesel::insert_into(roles::table)
|
||||||
|
.values(new_resource)
|
||||||
|
.get_result::<models::Role>(conn)?;
|
||||||
|
|
||||||
|
Ok(inserted)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result<models::Role, Error> {
|
||||||
|
roles::table
|
||||||
|
.find(id as uuid::Uuid)
|
||||||
|
.first::<models::Role>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all_count(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<i64, 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
q.count().get_result(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<Vec<models::Role>, 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::<models::Role>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn update(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
id: uuid::Uuid,
|
||||||
|
modify: &models::ModifyRole,
|
||||||
|
) -> Result<u64, Error> {
|
||||||
|
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<u64, Error> {
|
||||||
|
use roles::dsl;
|
||||||
|
|
||||||
|
diesel::update(dsl::roles.filter(dsl::id.eq(id)))
|
||||||
|
.set(modify)
|
||||||
|
.execute(conn)
|
||||||
|
.map(|c| c as u64)
|
||||||
|
}
|
||||||
|
}
|
24
src/repositories/role/schema.rs
Normal file
24
src/repositories/role/schema.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
|
||||||
|
table! {
|
||||||
|
///
|
||||||
|
roles(id) {
|
||||||
|
///
|
||||||
|
id -> Uuid,
|
||||||
|
///
|
||||||
|
parent_id -> Nullable<Uuid>,
|
||||||
|
///
|
||||||
|
name -> Text,
|
||||||
|
///
|
||||||
|
key -> Text,
|
||||||
|
///
|
||||||
|
description -> Text,
|
||||||
|
///
|
||||||
|
can_use -> Bool,
|
||||||
|
///
|
||||||
|
created_at -> BigInt,
|
||||||
|
///
|
||||||
|
updated_at -> BigInt,
|
||||||
|
}
|
||||||
|
}
|
9
src/repositories/role_resource_action/mod.rs
Normal file
9
src/repositories/role_resource_action/mod.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
|
||||||
|
///
|
||||||
|
pub mod models;
|
||||||
|
///
|
||||||
|
pub mod repository;
|
||||||
|
///
|
||||||
|
pub mod schema;
|
38
src/repositories/role_resource_action/models.rs
Normal file
38
src/repositories/role_resource_action/models.rs
Normal file
|
@ -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<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
pub resource_action_id: Option<uuid::Uuid>,
|
||||||
|
///
|
||||||
|
pub pagination: Option<bcr::models::pagination::Pagination>,
|
||||||
|
///
|
||||||
|
pub sorts: Option<Vec<bcr::models::pagination::Sort>>,
|
||||||
|
}
|
182
src/repositories/role_resource_action/repository.rs
Normal file
182
src/repositories/role_resource_action/repository.rs
Normal file
|
@ -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<models::RoleResourceAction, Error> {
|
||||||
|
let inserted = diesel::insert_into(role_resource_actions::table)
|
||||||
|
.values(new_resource)
|
||||||
|
.get_result::<models::RoleResourceAction>(conn)?;
|
||||||
|
|
||||||
|
Ok(inserted)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn inserts(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
new_resource_actions: &Vec<models::NewRoleResourceAction>,
|
||||||
|
) -> Result<Vec<models::RoleResourceAction>, 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<models::RoleResourceAction, Error> {
|
||||||
|
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::<models::RoleResourceAction>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all_count(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<i64, 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
q.count().get_result(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn select_all(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
find_all: models::FindAll,
|
||||||
|
) -> Result<Vec<models::RoleResourceAction>, 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::<models::RoleResourceAction>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn delete(
|
||||||
|
&self,
|
||||||
|
conn: &diesel::PgConnection,
|
||||||
|
role_id: uuid::Uuid,
|
||||||
|
resource_action_id: uuid::Uuid,
|
||||||
|
) -> Result<u64, Error> {
|
||||||
|
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<u64, Error> {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
14
src/repositories/role_resource_action/schema.rs
Normal file
14
src/repositories/role_resource_action/schema.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
|
||||||
|
table! {
|
||||||
|
///
|
||||||
|
role_resource_actions(role_id, resource_action_id) {
|
||||||
|
///
|
||||||
|
role_id -> Uuid,
|
||||||
|
///
|
||||||
|
resource_action_id -> Uuid,
|
||||||
|
///
|
||||||
|
created_at -> BigInt,
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user