From b9072233cb658414834c50eddd6d7b76c17003dd Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Thu, 11 Aug 2022 16:16:56 +0000 Subject: [PATCH] bank repositories are added --- migrations/202207011000_bank/down.sql | 4 + migrations/202207011000_bank/up.sql | 20 ++ .../202207011100_member_bank_account/down.sql | 3 + .../202207011100_member_bank_account/up.sql | 27 +++ .../202207011110_member_bank_deposit/down.sql | 4 + .../202207011110_member_bank_deposit/up.sql | 35 +++ .../down.sql | 4 + .../202207011120_member_bank_withdraw/up.sql | 38 ++++ src/repositories/bank/mod.rs | 9 + src/repositories/bank/models.rs | 67 ++++++ src/repositories/bank/repository.rs | 167 ++++++++++++++ src/repositories/bank/schema.rs | 22 ++ src/repositories/member_bank_account/mod.rs | 9 + .../member_bank_account/models.rs | 73 +++++++ .../member_bank_account/repository.rs | 168 +++++++++++++++ .../member_bank_account/schema.rs | 24 +++ src/repositories/member_bank_deposit/mod.rs | 9 + .../member_bank_deposit/models.rs | 73 +++++++ .../member_bank_deposit/repository.rs | 174 +++++++++++++++ .../member_bank_deposit/schema.rs | 36 ++++ src/repositories/member_bank_withdraw/mod.rs | 9 + .../member_bank_withdraw/models.rs | 95 ++++++++ .../member_bank_withdraw/repository.rs | 204 ++++++++++++++++++ .../member_bank_withdraw/schema.rs | 42 ++++ src/repositories/mod.rs | 4 + src/repositories/site/repository.rs | 12 +- 26 files changed, 1326 insertions(+), 6 deletions(-) create mode 100644 migrations/202207011000_bank/down.sql create mode 100644 migrations/202207011000_bank/up.sql create mode 100644 migrations/202207011100_member_bank_account/down.sql create mode 100644 migrations/202207011100_member_bank_account/up.sql create mode 100644 migrations/202207011110_member_bank_deposit/down.sql create mode 100644 migrations/202207011110_member_bank_deposit/up.sql create mode 100644 migrations/202207011120_member_bank_withdraw/down.sql create mode 100644 migrations/202207011120_member_bank_withdraw/up.sql create mode 100644 src/repositories/bank/mod.rs create mode 100644 src/repositories/bank/models.rs create mode 100644 src/repositories/bank/repository.rs create mode 100644 src/repositories/bank/schema.rs create mode 100644 src/repositories/member_bank_account/mod.rs create mode 100644 src/repositories/member_bank_account/models.rs create mode 100644 src/repositories/member_bank_account/repository.rs create mode 100644 src/repositories/member_bank_account/schema.rs create mode 100644 src/repositories/member_bank_deposit/mod.rs create mode 100644 src/repositories/member_bank_deposit/models.rs create mode 100644 src/repositories/member_bank_deposit/repository.rs create mode 100644 src/repositories/member_bank_deposit/schema.rs create mode 100644 src/repositories/member_bank_withdraw/mod.rs create mode 100644 src/repositories/member_bank_withdraw/models.rs create mode 100644 src/repositories/member_bank_withdraw/repository.rs create mode 100644 src/repositories/member_bank_withdraw/schema.rs diff --git a/migrations/202207011000_bank/down.sql b/migrations/202207011000_bank/down.sql new file mode 100644 index 0000000..a7767ff --- /dev/null +++ b/migrations/202207011000_bank/down.sql @@ -0,0 +1,4 @@ +DROP UNIQUE INDEX uidx_banks_url; +DROP TRIGGER tg_banks_updated_at; +DROP TABLE banks; + diff --git a/migrations/202207011000_bank/up.sql b/migrations/202207011000_bank/up.sql new file mode 100644 index 0000000..162030b --- /dev/null +++ b/migrations/202207011000_bank/up.sql @@ -0,0 +1,20 @@ +CREATE TABLE IF NOT EXISTS banks ( + id UUID DEFAULT uuid_generate_v4(), + name TEXT, + show BOOLEAN NOT NULL DEFAULT TRUE, + can_use BOOLEAN NOT NULL DEFAULT TRUE, + memo TEXT, + 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 (url) +); + +CREATE UNIQUE INDEX uidx_banks_url ON banks (url); + +-- trigger (updated_at) +CREATE TRIGGER tg_banks_updated_at + BEFORE UPDATE + ON banks + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); diff --git a/migrations/202207011100_member_bank_account/down.sql b/migrations/202207011100_member_bank_account/down.sql new file mode 100644 index 0000000..7eb96ba --- /dev/null +++ b/migrations/202207011100_member_bank_account/down.sql @@ -0,0 +1,3 @@ +DROP INDEX idx_member_bank_accounts_member_id; +DROP TRIGGER tg_member_bank_accounts_updated_at; +DROP TABLE member_bank_accounts; diff --git a/migrations/202207011100_member_bank_account/up.sql b/migrations/202207011100_member_bank_account/up.sql new file mode 100644 index 0000000..c903f6f --- /dev/null +++ b/migrations/202207011100_member_bank_account/up.sql @@ -0,0 +1,27 @@ +CREATE TABLE IF NOT EXISTS member_bank_accounts ( + id UUID DEFAULT uuid_generate_v4(), + member_id UUID NOT NULL, + bank_id UUID NOT NULL, + name TEXT NOT NULL, + account_number TEXT NOT NULL, + memo TEXT, + 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_member_bank_accounts_member_id + FOREIGN KEY(member_id) + REFERENCES members(id), + CONSTRAINT fk_member_bank_accounts_bank_id + FOREIGN KEY(bank_id) + REFERENCES banks(id) + +); + +CREATE INDEX idx_member_bank_accounts_member_id ON member_bank_accounts (member_id); + +-- trigger (updated_at) +CREATE TRIGGER tg_member_bank_accounts_updated_at + BEFORE UPDATE + ON member_bank_accounts + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); diff --git a/migrations/202207011110_member_bank_deposit/down.sql b/migrations/202207011110_member_bank_deposit/down.sql new file mode 100644 index 0000000..e4f439b --- /dev/null +++ b/migrations/202207011110_member_bank_deposit/down.sql @@ -0,0 +1,4 @@ +DROP INDEX idx_member_bank_deposits_member_id; +DROP TRIGGER tg_member_bank_deposits_updated_at; +DROP TRIGGER tg_member_bank_deposits_state_changed_at; +DROP TABLE member_bank_deposits; diff --git a/migrations/202207011110_member_bank_deposit/up.sql b/migrations/202207011110_member_bank_deposit/up.sql new file mode 100644 index 0000000..c020752 --- /dev/null +++ b/migrations/202207011110_member_bank_deposit/up.sql @@ -0,0 +1,35 @@ +CREATE TYPE member_bank_deposit_state AS ENUM ('application', 'complete'); + + +CREATE TABLE IF NOT EXISTS member_bank_deposits ( + id UUID DEFAULT uuid_generate_v4(), + member_id UUID NOT NULL, + name TEXT NOT NULL, + amount INTEGER NOT NULL, + memo TEXT, + state MEMBER_BANK_DEPOSIT_STATE DEFAULT 'application', + state_changed_at BIGINT, + 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_member_bank_deposits_member_id + FOREIGN KEY(member_id) + REFERENCES members(id) + +); + +CREATE INDEX idx_member_bank_deposits_member_id ON member_bank_deposits (member_id); + +-- trigger (updated_at) +CREATE TRIGGER tg_member_bank_deposits_updated_at + BEFORE UPDATE + ON member_bank_deposits + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); + +-- trigger (state_changed_at) +CREATE TRIGGER tg_member_bank_deposits_state_changed_at + BEFORE UPDATE + ON member_bank_deposits + FOR EACH ROW + EXECUTE PROCEDURE update_state_changed_at_column(); diff --git a/migrations/202207011120_member_bank_withdraw/down.sql b/migrations/202207011120_member_bank_withdraw/down.sql new file mode 100644 index 0000000..84b177c --- /dev/null +++ b/migrations/202207011120_member_bank_withdraw/down.sql @@ -0,0 +1,4 @@ +DROP INDEX idx_member_bank_withdraws_member_id; +DROP TRIGGER tg_member_bank_withdraws_updated_at; +DROP TRIGGER tg_member_bank_withdraws_state_changed_at; +DROP TABLE member_bank_withdraws; diff --git a/migrations/202207011120_member_bank_withdraw/up.sql b/migrations/202207011120_member_bank_withdraw/up.sql new file mode 100644 index 0000000..26b828b --- /dev/null +++ b/migrations/202207011120_member_bank_withdraw/up.sql @@ -0,0 +1,38 @@ +CREATE TYPE member_bank_withdraw_state AS ENUM ('application', 'complete'); + + +CREATE TABLE IF NOT EXISTS member_bank_withdraws ( + id UUID DEFAULT uuid_generate_v4(), + member_id UUID NOT NULL, + bank_name TEXT NOT NULL, + name TEXT NOT NULL, + account_number TEXT NOT NULL, + amount INTEGER NOT NULL, + password TEXT NOT NULL, + memo TEXT, + state MEMBER_BANK_WITHDRAW_STATE DEFAULT 'application', + state_changed_at BIGINT, + 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_member_bank_withdraws_member_id + FOREIGN KEY(member_id) + REFERENCES members(id) + +); + +CREATE INDEX idx_member_bank_withdraws_member_id ON member_bank_withdraws (member_id); + +-- trigger (updated_at) +CREATE TRIGGER tg_member_bank_withdraws_updated_at + BEFORE UPDATE + ON member_bank_withdraws + FOR EACH ROW + EXECUTE PROCEDURE update_updated_at_column(); + +-- trigger (state_changed_at) +CREATE TRIGGER tg_member_bank_withdraws_state_changed_at + BEFORE UPDATE + ON member_bank_withdraws + FOR EACH ROW + EXECUTE PROCEDURE update_state_changed_at_column(); diff --git a/src/repositories/bank/mod.rs b/src/repositories/bank/mod.rs new file mode 100644 index 0000000..bef7c9e --- /dev/null +++ b/src/repositories/bank/mod.rs @@ -0,0 +1,9 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/bank/models.rs b/src/repositories/bank/models.rs new file mode 100644 index 0000000..1739b02 --- /dev/null +++ b/src/repositories/bank/models.rs @@ -0,0 +1,67 @@ +use super::schema::banks; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "banks"] +pub struct Bank { + /// + pub id: uuid::Uuid, + /// + pub name: Option, + /// + pub show: bool, + /// + pub can_use: bool, + /// + pub memo: Option, + /// + pub created_at: i64, + /// + pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "banks"] +pub struct NewBank { + /// + pub name: Option, + /// + pub show: bool, + /// + pub can_use: bool, + /// + pub memo: Option, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "banks"] +pub struct ModifyBank { + /// + pub name: Option, + /// + pub show: bool, + /// + pub can_use: bool, + /// + pub memo: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub name_like: Option, + /// + pub show: Option, + /// + pub can_use: Option, + /// + pub memo_like: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/bank/repository.rs b/src/repositories/bank/repository.rs new file mode 100644 index 0000000..2879f2b --- /dev/null +++ b/src/repositories/bank/repository.rs @@ -0,0 +1,167 @@ +//! +//! +use super::{models, schema::banks}; +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 banks").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_member: &models::NewBank, + ) -> Result { + let inserted = diesel::insert_into(banks::table) + .values(new_member) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + ) -> Result, Error> { + match banks::table.find(id).first::(conn) { + Ok(m) => Ok(Some(m)), + Err(e) => match e { + diesel::result::Error::NotFound => Ok(None), + _ => Err(e), + }, + } + } + + /// + pub fn select_all_count( + &self, + conn: &diesel::PgConnection, + find_all: &models::FindAll, + ) -> Result { + let mut q = banks::table.into_boxed(); + + if let Some(sp) = &find_all.name_like { + q = q.filter(banks::dsl::name.like(sp)); + } + if let Some(sp) = find_all.show { + q = q.filter(banks::dsl::show.eq(sp)); + } + if let Some(sp) = find_all.can_use { + q = q.filter(banks::dsl::can_use.eq(sp)); + } + if let Some(sp) = &find_all.memo_like { + q = q.filter(banks::dsl::memo.like(sp)); + } + + q.count().get_result(conn) + } + + /// + pub fn select_all( + &self, + conn: &diesel::PgConnection, + find_all: &models::FindAll, + ) -> Result, Error> { + let mut q = banks::table.into_boxed(); + + if let Some(sp) = &find_all.name_like { + q = q.filter(banks::dsl::name.like(sp)); + } + if let Some(sp) = find_all.show { + q = q.filter(banks::dsl::show.eq(sp)); + } + if let Some(sp) = find_all.can_use { + q = q.filter(banks::dsl::can_use.eq(sp)); + } + if let Some(sp) = &find_all.memo_like { + q = q.filter(banks::dsl::memo.like(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(banks::name.asc()); + } + "created_at" => { + q = q.order_by(banks::created_at.asc()); + } + "updated_at" => { + q = q.order_by(banks::updated_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "name" => { + q = q.order_by(banks::name.desc()); + } + "created_at" => { + q = q.order_by(banks::created_at.desc()); + } + "updated_at" => { + q = q.order_by(banks::updated_at.desc()); + } + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn update( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyBank, + ) -> Result { + use banks::dsl; + + diesel::update(dsl::banks.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } + + /// + pub fn delete(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result { + use banks::dsl; + + diesel::delete(banks::table.filter(dsl::id.eq(id))) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/bank/schema.rs b/src/repositories/bank/schema.rs new file mode 100644 index 0000000..a2fd31d --- /dev/null +++ b/src/repositories/bank/schema.rs @@ -0,0 +1,22 @@ +//! +//! + +table! { + /// + banks(id) { + /// + id -> Uuid, + /// + name -> Nullable, + /// + show -> Bool, + /// + can_use -> Bool, + /// + memo -> Nullable, + /// + created_at -> BigInt, + /// + updated_at -> BigInt, + } +} diff --git a/src/repositories/member_bank_account/mod.rs b/src/repositories/member_bank_account/mod.rs new file mode 100644 index 0000000..bef7c9e --- /dev/null +++ b/src/repositories/member_bank_account/mod.rs @@ -0,0 +1,9 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/member_bank_account/models.rs b/src/repositories/member_bank_account/models.rs new file mode 100644 index 0000000..38ca605 --- /dev/null +++ b/src/repositories/member_bank_account/models.rs @@ -0,0 +1,73 @@ +use super::schema::member_bank_accounts; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "member_bank_accounts"] +pub struct MemberBankAccount { + /// + pub id: uuid::Uuid, + /// + pub member_id: uuid::Uuid, + /// + pub bank_id: uuid::Uuid, + /// + pub name: String, + /// + pub account_number: String, + /// + pub memo: Option, + /// + pub created_at: i64, + /// + pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "member_bank_accounts"] +pub struct NewMemberBankAccount { + /// + pub member_id: uuid::Uuid, + /// + pub bank_id: uuid::Uuid, + /// + pub name: String, + /// + pub account_number: String, + /// + pub memo: Option, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "member_bank_accounts"] +pub struct ModifyMemberBankAccount { + /// + pub bank_id: uuid::Uuid, + /// + pub name: String, + /// + pub account_number: String, + /// + pub memo: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub member_id: Option, + /// + pub bank_id: Option, + /// + pub name_like: Option, + /// + pub account_number_like: Option, + /// + pub memo_like: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/member_bank_account/repository.rs b/src/repositories/member_bank_account/repository.rs new file mode 100644 index 0000000..5cce517 --- /dev/null +++ b/src/repositories/member_bank_account/repository.rs @@ -0,0 +1,168 @@ +//! +//! +use super::{models, schema::member_bank_accounts}; +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_bank_accounts") + .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::NewMemberBankAccount, + ) -> Result { + let inserted = diesel::insert_into(member_bank_accounts::table) + .values(new_resource) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + ) -> Result { + member_bank_accounts::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 = member_bank_accounts::table.into_boxed(); + + if let Some(sp) = find_all.member_id { + q = q.filter(member_bank_accounts::dsl::member_id.eq(sp)); + } + if let Some(sp) = find_all.bank_id { + q = q.filter(member_bank_accounts::dsl::bank_id.eq(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(member_bank_accounts::dsl::name.like(sp)); + } + if let Some(sp) = find_all.account_number_like { + q = q.filter(member_bank_accounts::dsl::account_number.like(sp)); + } + if let Some(sp) = find_all.memo_like { + q = q.filter(member_bank_accounts::dsl::memo.like(sp)); + } + + q.count().get_result(conn) + } + + /// + pub fn select_all( + &self, + conn: &diesel::PgConnection, + find_all: models::FindAll, + ) -> Result, Error> { + let mut q = member_bank_accounts::table.into_boxed(); + + if let Some(sp) = find_all.member_id { + q = q.filter(member_bank_accounts::dsl::member_id.eq(sp)); + } + if let Some(sp) = find_all.bank_id { + q = q.filter(member_bank_accounts::dsl::bank_id.eq(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(member_bank_accounts::dsl::name.like(sp)); + } + if let Some(sp) = find_all.account_number_like { + q = q.filter(member_bank_accounts::dsl::account_number.like(sp)); + } + if let Some(sp) = find_all.memo_like { + q = q.filter(member_bank_accounts::dsl::memo.like(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(member_bank_accounts::name.asc()); + } + "account_number" => { + q = q.order_by(member_bank_accounts::account_number.asc()); + } + "created_at" => { + q = q.order_by(member_bank_accounts::created_at.asc()); + } + "updated_at" => { + q = q.order_by(member_bank_accounts::updated_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "name" => { + q = q.order_by(member_bank_accounts::name.desc()); + } + "account_number" => { + q = q.order_by(member_bank_accounts::account_number.desc()); + } + "created_at" => { + q = q.order_by(member_bank_accounts::created_at.desc()); + } + "updated_at" => { + q = q.order_by(member_bank_accounts::updated_at.desc()); + } + + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn update( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyMemberBankAccount, + ) -> Result { + use member_bank_accounts::dsl; + + diesel::update(dsl::member_bank_accounts.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/member_bank_account/schema.rs b/src/repositories/member_bank_account/schema.rs new file mode 100644 index 0000000..410d4a4 --- /dev/null +++ b/src/repositories/member_bank_account/schema.rs @@ -0,0 +1,24 @@ +//! +//! + +table! { + /// + member_bank_accounts(id) { + /// + id -> Uuid, + /// + member_id -> Uuid, + /// + bank_id -> Uuid, + /// + name -> Text, + /// + account_number -> Text, + /// + memo -> Nullable, + /// + created_at -> BigInt, + /// + updated_at -> BigInt, + } +} diff --git a/src/repositories/member_bank_deposit/mod.rs b/src/repositories/member_bank_deposit/mod.rs new file mode 100644 index 0000000..bef7c9e --- /dev/null +++ b/src/repositories/member_bank_deposit/mod.rs @@ -0,0 +1,9 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/member_bank_deposit/models.rs b/src/repositories/member_bank_deposit/models.rs new file mode 100644 index 0000000..4d3779a --- /dev/null +++ b/src/repositories/member_bank_deposit/models.rs @@ -0,0 +1,73 @@ +use super::schema::{member_bank_deposits, MemberBankDepositState}; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "member_bank_deposits"] +pub struct MemberBankDeposit { + /// + pub id: uuid::Uuid, + /// + pub member_id: uuid::Uuid, + /// + pub name: String, + /// + pub amount: i32, + /// + pub state: MemberBankDepositState, + /// + pub state_changed_at: Option, + /// + pub memo: Option, + /// + pub created_at: i64, + /// + pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "member_bank_deposits"] +pub struct NewMemberBankDeposit { + /// + pub member_id: uuid::Uuid, + /// + pub name: String, + /// + pub amount: i32, + /// + pub memo: Option, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "member_bank_deposits"] +pub struct ModifyMemberBankDeposit { + /// + pub name: String, + /// + pub amount: i32, + /// + pub memo: Option, + /// + pub state: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub member_id: Option, + /// + pub name_like: Option, + /// + pub amount: Option, + /// + pub memo_like: Option, + /// + pub state: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/member_bank_deposit/repository.rs b/src/repositories/member_bank_deposit/repository.rs new file mode 100644 index 0000000..ce882a8 --- /dev/null +++ b/src/repositories/member_bank_deposit/repository.rs @@ -0,0 +1,174 @@ +//! +//! +use super::{models, schema::member_bank_deposits}; +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_bank_deposits") + .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::NewMemberBankDeposit, + ) -> Result { + let inserted = diesel::insert_into(member_bank_deposits::table) + .values(new_resource) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + ) -> Result { + member_bank_deposits::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 = member_bank_deposits::table.into_boxed(); + + if let Some(sp) = find_all.member_id { + q = q.filter(member_bank_deposits::dsl::member_id.eq(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(member_bank_deposits::dsl::name.like(sp)); + } + if let Some(sp) = find_all.amount { + q = q.filter(member_bank_deposits::dsl::amount.eq(sp)); + } + if let Some(sp) = find_all.memo_like { + q = q.filter(member_bank_deposits::dsl::memo.like(sp)); + } + if let Some(sp) = find_all.state { + q = q.filter(member_bank_deposits::dsl::state.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_bank_deposits::table.into_boxed(); + + if let Some(sp) = find_all.member_id { + q = q.filter(member_bank_deposits::dsl::member_id.eq(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(member_bank_deposits::dsl::name.like(sp)); + } + if let Some(sp) = find_all.amount { + q = q.filter(member_bank_deposits::dsl::amount.eq(sp)); + } + if let Some(sp) = find_all.memo_like { + q = q.filter(member_bank_deposits::dsl::memo.like(sp)); + } + if let Some(sp) = find_all.state { + q = q.filter(member_bank_deposits::dsl::state.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(member_bank_deposits::name.asc()); + } + "amount" => { + q = q.order_by(member_bank_deposits::amount.asc()); + } + "state" => { + q = q.order_by(member_bank_deposits::state.asc()); + } + "created_at" => { + q = q.order_by(member_bank_deposits::created_at.asc()); + } + "updated_at" => { + q = q.order_by(member_bank_deposits::updated_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "name" => { + q = q.order_by(member_bank_deposits::name.desc()); + } + "amount" => { + q = q.order_by(member_bank_deposits::amount.desc()); + } + "state" => { + q = q.order_by(member_bank_deposits::state.desc()); + } + "created_at" => { + q = q.order_by(member_bank_deposits::created_at.desc()); + } + "updated_at" => { + q = q.order_by(member_bank_deposits::updated_at.desc()); + } + + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn update( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyMemberBankDeposit, + ) -> Result { + use member_bank_deposits::dsl; + + diesel::update(dsl::member_bank_deposits.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/member_bank_deposit/schema.rs b/src/repositories/member_bank_deposit/schema.rs new file mode 100644 index 0000000..3e21bd6 --- /dev/null +++ b/src/repositories/member_bank_deposit/schema.rs @@ -0,0 +1,36 @@ +//! +//! + +/// +#[derive(Eq, Hash, Debug, Clone, Copy, PartialEq, diesel_derive_enum::DbEnum)] +pub enum MemberBankDepositState { + Application, + Complete, +} + +table! { + use diesel::sql_types::{Uuid, Text, BigInt, Nullable, Integer}; + use super::MemberBankDepositStateMapping; + + /// + member_bank_deposits(id) { + /// + id -> Uuid, + /// + member_id -> Uuid, + /// + name -> Text, + /// + amount -> Integer, + /// + state -> MemberBankDepositStateMapping, + /// + state_changed_at -> Nullable, + /// + memo -> Nullable, + /// + created_at -> BigInt, + /// + updated_at -> BigInt, + } +} diff --git a/src/repositories/member_bank_withdraw/mod.rs b/src/repositories/member_bank_withdraw/mod.rs new file mode 100644 index 0000000..bef7c9e --- /dev/null +++ b/src/repositories/member_bank_withdraw/mod.rs @@ -0,0 +1,9 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; +/// +pub mod schema; diff --git a/src/repositories/member_bank_withdraw/models.rs b/src/repositories/member_bank_withdraw/models.rs new file mode 100644 index 0000000..9d55118 --- /dev/null +++ b/src/repositories/member_bank_withdraw/models.rs @@ -0,0 +1,95 @@ +use super::schema::{member_bank_withdraws, MemberBankWithdrawState}; +use beteran_common_rust as bcr; + +/// +#[derive(Eq, Hash, Identifiable, Queryable, PartialEq, Debug, Clone)] +#[table_name = "member_bank_withdraws"] +pub struct MemberBankWithdraw { + /// + pub id: uuid::Uuid, + /// + pub member_id: uuid::Uuid, + /// + pub bank_name: String, + /// + pub name: String, + /// + pub account_number: String, + /// + pub amount: i32, + /// + pub password: String, + /// + pub state: MemberBankWithdrawState, + /// + pub state_changed_at: Option, + /// + pub memo: Option, + /// + pub created_at: i64, + /// + pub updated_at: i64, +} + +/// +#[derive(Insertable, Debug, Clone)] +#[table_name = "member_bank_withdraws"] +pub struct NewMemberBankWithdraw { + /// + pub member_id: uuid::Uuid, + /// + pub bank_name: String, + /// + pub name: String, + /// + pub account_number: String, + /// + pub amount: i32, + /// + pub password: String, + /// + pub memo: Option, +} + +/// +#[derive(AsChangeset, Debug, Clone)] +#[table_name = "member_bank_withdraws"] +pub struct ModifyMemberBankWithdraw { + /// + pub bank_name: String, + /// + pub name: String, + /// + pub account_number: String, + /// + pub amount: i32, + /// + pub password: String, + /// + pub memo: Option, + /// + pub state: Option, +} + +/// +#[derive(Debug, Clone)] +pub struct FindAll { + /// + pub member_id: Option, + /// + pub bank_name_like: Option, + /// + pub name_like: Option, + /// + pub account_number_like: Option, + /// + pub amount: Option, + /// + pub memo_like: Option, + /// + pub state: Option, + /// + pub pagination: Option, + /// + pub sorts: Option>, +} diff --git a/src/repositories/member_bank_withdraw/repository.rs b/src/repositories/member_bank_withdraw/repository.rs new file mode 100644 index 0000000..fce34b0 --- /dev/null +++ b/src/repositories/member_bank_withdraw/repository.rs @@ -0,0 +1,204 @@ +//! +//! +use super::{models, schema::member_bank_withdraws}; +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_bank_withdraws") + .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::NewMemberBankWithdraw, + ) -> Result { + let inserted = diesel::insert_into(member_bank_withdraws::table) + .values(new_resource) + .get_result::(conn)?; + + Ok(inserted) + } + + /// + pub fn select( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + ) -> Result { + member_bank_withdraws::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 = member_bank_withdraws::table.into_boxed(); + + if let Some(sp) = find_all.member_id { + q = q.filter(member_bank_withdraws::dsl::member_id.eq(sp)); + } + if let Some(sp) = find_all.bank_name_like { + q = q.filter(member_bank_withdraws::dsl::bank_name.like(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(member_bank_withdraws::dsl::name.like(sp)); + } + if let Some(sp) = find_all.account_number_like { + q = q.filter(member_bank_withdraws::dsl::account_number.like(sp)); + } + if let Some(sp) = find_all.amount { + q = q.filter(member_bank_withdraws::dsl::amount.eq(sp)); + } + if let Some(sp) = find_all.memo_like { + q = q.filter(member_bank_withdraws::dsl::memo.like(sp)); + } + if let Some(sp) = find_all.state { + q = q.filter(member_bank_withdraws::dsl::state.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_bank_withdraws::table.into_boxed(); + + if let Some(sp) = find_all.member_id { + q = q.filter(member_bank_withdraws::dsl::member_id.eq(sp)); + } + if let Some(sp) = find_all.bank_name_like { + q = q.filter(member_bank_withdraws::dsl::bank_name.like(sp)); + } + if let Some(sp) = find_all.name_like { + q = q.filter(member_bank_withdraws::dsl::name.like(sp)); + } + if let Some(sp) = find_all.account_number_like { + q = q.filter(member_bank_withdraws::dsl::account_number.like(sp)); + } + if let Some(sp) = find_all.amount { + q = q.filter(member_bank_withdraws::dsl::amount.eq(sp)); + } + if let Some(sp) = find_all.memo_like { + q = q.filter(member_bank_withdraws::dsl::memo.like(sp)); + } + if let Some(sp) = find_all.state { + q = q.filter(member_bank_withdraws::dsl::state.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() { + "member_id" => { + q = q.order_by(member_bank_withdraws::member_id.asc()); + } + "bank_name" => { + q = q.order_by(member_bank_withdraws::bank_name.asc()); + } + "name" => { + q = q.order_by(member_bank_withdraws::name.asc()); + } + "account_number" => { + q = q.order_by(member_bank_withdraws::account_number.asc()); + } + "amount" => { + q = q.order_by(member_bank_withdraws::amount.asc()); + } + "state" => { + q = q.order_by(member_bank_withdraws::state.asc()); + } + "created_at" => { + q = q.order_by(member_bank_withdraws::created_at.asc()); + } + "updated_at" => { + q = q.order_by(member_bank_withdraws::updated_at.asc()); + } + _ => {} + }, + bcr::models::pagination::Sort::DESC(property) => match property.as_str() { + "member_id" => { + q = q.order_by(member_bank_withdraws::member_id.desc()); + } + "bank_name" => { + q = q.order_by(member_bank_withdraws::bank_name.desc()); + } + "name" => { + q = q.order_by(member_bank_withdraws::name.desc()); + } + "account_number" => { + q = q.order_by(member_bank_withdraws::account_number.desc()); + } + "amount" => { + q = q.order_by(member_bank_withdraws::amount.desc()); + } + "state" => { + q = q.order_by(member_bank_withdraws::state.desc()); + } + "created_at" => { + q = q.order_by(member_bank_withdraws::created_at.desc()); + } + "updated_at" => { + q = q.order_by(member_bank_withdraws::updated_at.desc()); + } + + _ => {} + }, + }; + } + } + + q.load::(conn) + } + + /// + pub fn update( + &self, + conn: &diesel::PgConnection, + id: uuid::Uuid, + modify: &models::ModifyMemberBankWithdraw, + ) -> Result { + use member_bank_withdraws::dsl; + + diesel::update(dsl::member_bank_withdraws.filter(dsl::id.eq(id))) + .set(modify) + .execute(conn) + .map(|c| c as u64) + } +} diff --git a/src/repositories/member_bank_withdraw/schema.rs b/src/repositories/member_bank_withdraw/schema.rs new file mode 100644 index 0000000..18672ae --- /dev/null +++ b/src/repositories/member_bank_withdraw/schema.rs @@ -0,0 +1,42 @@ +//! +//! + +/// +#[derive(Eq, Hash, Debug, Clone, Copy, PartialEq, diesel_derive_enum::DbEnum)] +pub enum MemberBankWithdrawState { + Application, + Complete, +} + +table! { + use diesel::sql_types::{Uuid, Text, BigInt, Nullable, Integer}; + use super::MemberBankWithdrawStateMapping; + + /// + member_bank_withdraws(id) { + /// + id -> Uuid, + /// + member_id -> Uuid, + /// + bank_name -> Text, + /// + name -> Text, + /// + account_number -> Text, + /// + amount -> Integer, + /// + password -> Text, + /// + state -> MemberBankWithdrawStateMapping, + /// + state_changed_at -> Nullable, + /// + memo -> Nullable, + /// + created_at -> BigInt, + /// + updated_at -> BigInt, + } +} diff --git a/src/repositories/mod.rs b/src/repositories/mod.rs index 849ee36..a68e53a 100644 --- a/src/repositories/mod.rs +++ b/src/repositories/mod.rs @@ -1,4 +1,8 @@ +pub mod bank; pub mod member; +pub mod member_bank_account; +pub mod member_bank_deposit; +pub mod member_bank_withdraw; pub mod member_class; pub mod member_level; pub mod member_permission; diff --git a/src/repositories/site/repository.rs b/src/repositories/site/repository.rs index 2237628..5e1d170 100644 --- a/src/repositories/site/repository.rs +++ b/src/repositories/site/repository.rs @@ -86,7 +86,7 @@ impl Repository { q = q.filter(sites::dsl::url.like(sp)); } if let Some(sp) = &find_all.name_like { - q = q.filter(sites::dsl::url.like(sp)); + q = q.filter(sites::dsl::name.like(sp)); } if let Some(sp) = &find_all.path_like { q = q.filter(sites::dsl::path.like(sp)); @@ -116,7 +116,7 @@ impl Repository { q = q.filter(sites::dsl::url.like(sp)); } if let Some(sp) = &find_all.name_like { - q = q.filter(sites::dsl::url.like(sp)); + q = q.filter(sites::dsl::name.like(sp)); } if let Some(sp) = &find_all.path_like { q = q.filter(sites::dsl::path.like(sp)); @@ -147,10 +147,10 @@ impl Repository { q = q.order_by(sites::url.asc()); } "name" => { - q = q.order_by(sites::url.asc()); + q = q.order_by(sites::name.asc()); } "path" => { - q = q.order_by(sites::url.asc()); + q = q.order_by(sites::path.asc()); } "expires_at" => { q = q.order_by(sites::expires_at.asc()); @@ -168,10 +168,10 @@ impl Repository { q = q.order_by(sites::url.desc()); } "name" => { - q = q.order_by(sites::url.desc()); + q = q.order_by(sites::name.desc()); } "path" => { - q = q.order_by(sites::url.desc()); + q = q.order_by(sites::path.desc()); } "expires_at" => { q = q.order_by(sites::expires_at.desc());