2023-07-17 21:59:04 +02:00
use derive_more ::{ Display , Error , From } ;
2023-07-17 18:55:17 +02:00
use mysql ::{ params , prelude ::* } ;
2023-07-17 18:31:58 +02:00
2023-07-17 21:59:04 +02:00
use crate ::models ::{
2023-07-17 18:55:17 +02:00
BankDetails , BankResponseData , BranchDetails , BranchResponseData , CustomerDetails ,
CustomerResponseData , ResponseStatus , TellerDetails , TellerResponseData ,
} ;
2023-07-17 18:31:58 +02:00
2023-07-17 18:55:17 +02:00
const ERROR_MESSAGE : & str = " Error occurred during processing, please try again. " ;
2023-07-17 21:59:04 +02:00
#[ derive(Debug, Display, Error, From) ]
pub enum PersistenceError {
EmptyBankName ,
EmptyCountry ,
EmptyBranch ,
EmptyLocation ,
EmptyTellerName ,
EmptyCustomerName ,
MysqlError ( mysql ::Error ) ,
Unknown ,
}
2023-07-17 18:31:58 +02:00
2023-07-17 21:59:04 +02:00
impl actix_web ::ResponseError for PersistenceError { }
2023-07-17 18:31:58 +02:00
2023-07-17 21:59:04 +02:00
pub fn create_bank (
pool : & mysql ::Pool ,
bank_name : String ,
country : String ,
) -> Result < ( ) , PersistenceError > {
2023-07-17 18:55:17 +02:00
if bank_name . replace ( ' ' , " " ) . trim ( ) . is_empty ( ) {
2023-07-17 21:59:04 +02:00
return Err ( PersistenceError ::EmptyBankName ) ;
2023-07-17 18:31:58 +02:00
}
2023-07-17 21:59:04 +02:00
if country . replace ( ' ' , " " ) . trim ( ) . is_empty ( ) {
return Err ( PersistenceError ::EmptyCountry ) ;
2023-07-17 18:31:58 +02:00
}
2023-07-17 21:59:04 +02:00
let mut conn = pool . get_conn ( ) ? ;
2023-07-17 18:31:58 +02:00
2023-07-17 21:59:04 +02:00
let rows_inserted =
insert_bank_data ( & mut conn , bank_name . to_lowercase ( ) , country . to_lowercase ( ) ) ? ;
if rows_inserted > 0 {
return Ok ( ( ) ) ;
} else {
Err ( PersistenceError ::Unknown )
}
2023-07-17 18:31:58 +02:00
}
2023-07-17 21:59:04 +02:00
pub fn create_branch ( pool : & mysql ::Pool , branch_name : String , location : String ) -> ResponseStatus {
2023-07-17 18:31:58 +02:00
let my_status_code : u8 = 1 ;
2023-07-17 18:55:17 +02:00
let my_status_description = ERROR_MESSAGE . to_owned ( ) ;
2023-07-17 18:31:58 +02:00
let mut response_status = ResponseStatus {
status_code : my_status_code ,
status_description : my_status_description ,
} ;
2023-07-17 18:55:17 +02:00
if branch_name . replace ( ' ' , " " ) . trim ( ) . is_empty ( ) {
2023-07-17 18:31:58 +02:00
response_status . status_description = String ::from ( " Branch name is empty! " ) ;
return response_status ;
}
2023-07-17 21:59:04 +02:00
if location . replace ( ' ' , " " ) . trim ( ) . is_empty ( ) {
2023-07-17 18:31:58 +02:00
response_status . status_description = String ::from ( " Location is empty! " ) ;
return response_status ;
}
2023-07-17 19:51:15 +02:00
match pool . get_conn ( ) . and_then ( | mut conn | {
2023-07-17 18:31:58 +02:00
insert_branch_data (
& mut conn ,
branch_name . to_lowercase ( ) ,
2023-07-17 21:59:04 +02:00
location . to_lowercase ( ) ,
2023-07-17 18:31:58 +02:00
)
} ) {
Ok ( x ) = > {
if x > 0 {
response_status . status_code = 0 ;
2023-07-17 19:51:15 +02:00
response_status . status_description = " Successful " . to_owned ( ) ;
2023-07-17 18:31:58 +02:00
}
}
2023-07-17 18:55:17 +02:00
Err ( err ) = > println! ( " Failed to open DB connection. create_branch {err:?} " ) ,
2023-07-17 18:31:58 +02:00
}
response_status
}
pub fn create_teller (
2023-07-17 19:51:15 +02:00
pool : & mysql ::Pool ,
2023-07-17 18:31:58 +02:00
teller_name : String ,
branch_name : String ,
) -> ResponseStatus {
let my_status_code : u8 = 1 ;
2023-07-17 18:55:17 +02:00
let my_status_description = ERROR_MESSAGE . to_owned ( ) ;
2023-07-17 18:31:58 +02:00
let mut response_status = ResponseStatus {
status_code : my_status_code ,
status_description : my_status_description ,
} ;
2023-07-17 18:55:17 +02:00
if teller_name . replace ( ' ' , " " ) . trim ( ) . is_empty ( ) {
2023-07-17 18:31:58 +02:00
response_status . status_description = String ::from ( " Teller name is empty! " ) ;
return response_status ;
}
2023-07-17 18:55:17 +02:00
if branch_name . replace ( ' ' , " " ) . trim ( ) . is_empty ( ) {
2023-07-17 18:31:58 +02:00
response_status . status_description = String ::from ( " Branch name is empty! " ) ;
return response_status ;
}
2023-07-17 19:51:15 +02:00
match pool . get_conn ( ) . and_then ( | mut conn | {
2023-07-17 18:31:58 +02:00
insert_teller_data (
& mut conn ,
teller_name . to_lowercase ( ) ,
branch_name . to_lowercase ( ) ,
)
} ) {
Ok ( x ) = > {
if x > 0 {
response_status . status_code = 0 ;
2023-07-17 19:51:15 +02:00
response_status . status_description = " Successful " . to_owned ( ) ;
2023-07-17 18:31:58 +02:00
}
}
2023-07-17 18:55:17 +02:00
Err ( err ) = > println! ( " Failed to open DB connection. create_teller {err:?} " ) ,
2023-07-17 18:31:58 +02:00
}
response_status
}
pub fn create_customer (
2023-07-17 19:51:15 +02:00
pool : & mysql ::Pool ,
2023-07-17 18:31:58 +02:00
customer_name : String ,
branch_name : String ,
) -> ResponseStatus {
let my_status_code : u8 = 1 ;
2023-07-17 18:55:17 +02:00
let my_status_description = ERROR_MESSAGE . to_owned ( ) ;
2023-07-17 18:31:58 +02:00
let mut response_status = ResponseStatus {
status_code : my_status_code ,
status_description : my_status_description ,
} ;
2023-07-17 18:55:17 +02:00
if customer_name . replace ( ' ' , " " ) . trim ( ) . is_empty ( ) {
2023-07-17 18:31:58 +02:00
response_status . status_description = String ::from ( " Customer name is empty! " ) ;
return response_status ;
}
2023-07-17 18:55:17 +02:00
if branch_name . replace ( ' ' , " " ) . trim ( ) . is_empty ( ) {
2023-07-17 18:31:58 +02:00
response_status . status_description = String ::from ( " Branch name is empty! " ) ;
return response_status ;
}
2023-07-17 19:51:15 +02:00
match pool . get_conn ( ) . and_then ( | mut conn | {
2023-07-17 18:31:58 +02:00
insert_customer_data (
& mut conn ,
customer_name . to_lowercase ( ) ,
branch_name . to_lowercase ( ) ,
)
} ) {
Ok ( x ) = > {
if x > 0 {
response_status . status_code = 0 ;
2023-07-17 19:51:15 +02:00
response_status . status_description = " Successful " . to_owned ( ) ;
2023-07-17 18:31:58 +02:00
}
}
2023-07-17 18:55:17 +02:00
Err ( err ) = > println! ( " Failed to open DB connection. create_customer {err:?} " ) ,
2023-07-17 18:31:58 +02:00
}
response_status
}
2023-07-17 19:51:15 +02:00
pub fn get_bank_data ( pool : & mysql ::Pool ) -> BankResponseData {
2023-07-17 18:31:58 +02:00
let mut vec_bank_data = Vec ::new ( ) ;
2023-07-17 19:51:15 +02:00
let mut my_status_code = 1_ u8 ;
2023-07-17 18:31:58 +02:00
let mut my_status_description : String = String ::from ( " Record not found " ) ;
2023-07-17 19:51:15 +02:00
match pool
2023-07-17 18:31:58 +02:00
. get_conn ( )
. and_then ( | mut conn | select_bank_details ( & mut conn ) )
{
2023-07-17 19:51:15 +02:00
Ok ( data ) = > vec_bank_data = data ,
2023-07-17 18:55:17 +02:00
Err ( err ) = > println! ( " Failed to open DB connection. {err:?} " ) ,
2023-07-17 18:31:58 +02:00
}
2023-07-17 18:55:17 +02:00
if ! vec_bank_data . is_empty ( ) {
2023-07-17 18:31:58 +02:00
my_status_code = 0 ;
2023-07-17 19:51:15 +02:00
my_status_description = " Successful " . to_owned ( ) ;
2023-07-17 18:31:58 +02:00
}
2023-07-17 18:55:17 +02:00
BankResponseData {
2023-07-17 18:31:58 +02:00
status_code : my_status_code ,
status_description : my_status_description ,
bank_data : vec_bank_data ,
2023-07-17 18:55:17 +02:00
}
2023-07-17 18:31:58 +02:00
}
2023-07-17 19:51:15 +02:00
pub fn get_branch_data ( pool : & mysql ::Pool ) -> BranchResponseData {
2023-07-17 18:31:58 +02:00
let mut vec_branch_data = Vec ::new ( ) ;
2023-07-17 19:51:15 +02:00
let mut my_status_code = 1_ u8 ;
2023-07-17 18:31:58 +02:00
let mut my_status_description : String = String ::from ( " Record not found " ) ;
2023-07-17 19:51:15 +02:00
match pool
2023-07-17 18:31:58 +02:00
. get_conn ( )
. and_then ( | mut conn | select_branch_details ( & mut conn ) )
{
2023-07-17 19:51:15 +02:00
Ok ( data ) = > vec_branch_data = data ,
2023-07-17 18:55:17 +02:00
Err ( err ) = > println! ( " Failed to open DB connection. {err:?} " ) ,
2023-07-17 18:31:58 +02:00
}
2023-07-17 18:55:17 +02:00
if ! vec_branch_data . is_empty ( ) {
2023-07-17 18:31:58 +02:00
my_status_code = 0 ;
2023-07-17 19:51:15 +02:00
my_status_description = " Successful " . to_owned ( ) ;
2023-07-17 18:31:58 +02:00
}
2023-07-17 18:55:17 +02:00
BranchResponseData {
2023-07-17 18:31:58 +02:00
status_code : my_status_code ,
status_description : my_status_description ,
branch_data : vec_branch_data ,
2023-07-17 18:55:17 +02:00
}
2023-07-17 18:31:58 +02:00
}
2023-07-17 19:51:15 +02:00
pub fn get_teller_data ( pool : & mysql ::Pool ) -> TellerResponseData {
2023-07-17 18:31:58 +02:00
let mut vec_teller_data = Vec ::new ( ) ;
2023-07-17 19:51:15 +02:00
let mut my_status_code = 1_ u8 ;
2023-07-17 18:31:58 +02:00
let mut my_status_description : String = String ::from ( " Record not found " ) ;
2023-07-17 19:51:15 +02:00
match pool
2023-07-17 18:31:58 +02:00
. get_conn ( )
. and_then ( | mut conn | select_teller_details ( & mut conn ) )
{
2023-07-17 19:51:15 +02:00
Ok ( data ) = > vec_teller_data = data ,
2023-07-17 18:55:17 +02:00
Err ( err ) = > println! ( " Failed to open DB connection. {err:?} " ) ,
2023-07-17 18:31:58 +02:00
}
2023-07-17 18:55:17 +02:00
if ! vec_teller_data . is_empty ( ) {
2023-07-17 18:31:58 +02:00
my_status_code = 0 ;
2023-07-17 19:51:15 +02:00
my_status_description = " Successful " . to_owned ( ) ;
2023-07-17 18:31:58 +02:00
}
2023-07-17 18:55:17 +02:00
TellerResponseData {
2023-07-17 18:31:58 +02:00
status_code : my_status_code ,
status_description : my_status_description ,
teller_data : vec_teller_data ,
2023-07-17 18:55:17 +02:00
}
2023-07-17 18:31:58 +02:00
}
2023-07-17 19:51:15 +02:00
pub fn get_customer_data ( pool : & mysql ::Pool ) -> CustomerResponseData {
2023-07-17 18:31:58 +02:00
let mut vec_customer_data = Vec ::new ( ) ;
2023-07-17 19:51:15 +02:00
let mut my_status_code = 1_ u8 ;
2023-07-17 18:31:58 +02:00
let mut my_status_description : String = String ::from ( " Record not found " ) ;
2023-07-17 19:51:15 +02:00
match pool
2023-07-17 18:31:58 +02:00
. get_conn ( )
. and_then ( | mut conn | select_customer_details ( & mut conn ) )
{
2023-07-17 19:51:15 +02:00
Ok ( data ) = > vec_customer_data = data ,
2023-07-17 18:55:17 +02:00
Err ( err ) = > println! ( " Failed to open DB connection. {err:?} " ) ,
2023-07-17 18:31:58 +02:00
}
2023-07-17 18:55:17 +02:00
if ! vec_customer_data . is_empty ( ) {
2023-07-17 18:31:58 +02:00
my_status_code = 0 ;
2023-07-17 19:51:15 +02:00
my_status_description = " Successful " . to_owned ( ) ;
2023-07-17 18:31:58 +02:00
}
2023-07-17 18:55:17 +02:00
CustomerResponseData {
2023-07-17 18:31:58 +02:00
status_code : my_status_code ,
status_description : my_status_description ,
customer_data : vec_customer_data ,
2023-07-17 18:55:17 +02:00
}
2023-07-17 18:31:58 +02:00
}
fn insert_bank_data (
2023-07-17 18:55:17 +02:00
conn : & mut mysql ::PooledConn ,
2023-07-17 18:31:58 +02:00
my_bank_name : String ,
my_country : String ,
) -> std ::result ::Result < u64 , mysql ::error ::Error > {
// Insert data into the database table bank_details
conn . exec_drop (
" insert into bank_details (bank_name, country) values (:bank_name, :country); " ,
params! {
" bank_name " = > my_bank_name ,
" country " = > my_country ,
} ,
)
2023-07-17 18:55:17 +02:00
. map ( | _ | conn . last_insert_id ( ) )
2023-07-17 18:31:58 +02:00
}
fn insert_branch_data (
2023-07-17 18:55:17 +02:00
conn : & mut mysql ::PooledConn ,
2023-07-17 18:31:58 +02:00
my_branch_name : String ,
my_location : String ,
) -> std ::result ::Result < u64 , mysql ::error ::Error > {
// Insert data into the database table branch_details
conn . exec_drop (
" insert into branch_details (branch_name, location) values (:branch_name, :location); " ,
params! {
" branch_name " = > my_branch_name ,
" location " = > my_location ,
} ,
)
2023-07-17 18:55:17 +02:00
. map ( | _ | conn . last_insert_id ( ) )
2023-07-17 18:31:58 +02:00
}
fn insert_teller_data (
2023-07-17 18:55:17 +02:00
conn : & mut mysql ::PooledConn ,
2023-07-17 18:31:58 +02:00
my_teller_name : String ,
my_branch_name : String ,
) -> std ::result ::Result < u64 , mysql ::error ::Error > {
// Insert data into the database table teller_details
conn . exec_drop (
" insert into teller_details (teller_name, branch_name) values (:teller_name, :branch_name); " ,
params! {
" teller_name " = > my_teller_name ,
" branch_name " = > my_branch_name ,
} ,
2023-07-17 18:55:17 +02:00
) . map ( | _ | conn . last_insert_id ( ) )
2023-07-17 18:31:58 +02:00
}
fn insert_customer_data (
2023-07-17 18:55:17 +02:00
conn : & mut mysql ::PooledConn ,
2023-07-17 18:31:58 +02:00
my_customer_name : String ,
my_branch_name : String ,
) -> std ::result ::Result < u64 , mysql ::error ::Error > {
// Insert data into the database table customer_details
conn . exec_drop (
" insert into customer_details (customer_name, branch_name) values (:customer_name, :branch_name); " ,
params! {
" customer_name " = > my_customer_name ,
" branch_name " = > my_branch_name ,
} ,
2023-07-17 18:55:17 +02:00
) . map ( | _ | conn . last_insert_id ( ) )
2023-07-17 18:31:58 +02:00
}
fn select_bank_details (
2023-07-17 18:55:17 +02:00
conn : & mut mysql ::PooledConn ,
2023-07-17 18:31:58 +02:00
) -> std ::result ::Result < Vec < BankDetails > , mysql ::error ::Error > {
let mut bank_data = Vec ::new ( ) ;
conn . query_map (
" select bank_name, country from bank_details where length(trim(coalesce(bank_name,''))) > 0 and length(trim(coalesce(country,''))) > 0 order by id asc; " ,
| ( my_bank_name , my_country ) | {
let bank_details = BankDetails { bank_name : my_bank_name , country : my_country , } ;
bank_data . push ( bank_details ) ;
} ,
2023-07-17 18:55:17 +02:00
) . map ( | _ | bank_data )
2023-07-17 18:31:58 +02:00
}
fn select_branch_details (
2023-07-17 18:55:17 +02:00
conn : & mut mysql ::PooledConn ,
2023-07-17 18:31:58 +02:00
) -> std ::result ::Result < Vec < BranchDetails > , mysql ::error ::Error > {
let mut branch_data = Vec ::new ( ) ;
conn . query_map (
" select branch_name, location from branch_details where length(trim(coalesce(branch_name,''))) > 0 and length(trim(coalesce(location,''))) > 0 order by id asc; " ,
| ( my_branch_name , my_location ) | {
let branch_details = BranchDetails { branch_name : my_branch_name , location : my_location , } ;
branch_data . push ( branch_details ) ;
} ,
2023-07-17 18:55:17 +02:00
) . map ( | _ | branch_data )
2023-07-17 18:31:58 +02:00
}
fn select_teller_details (
2023-07-17 18:55:17 +02:00
conn : & mut mysql ::PooledConn ,
2023-07-17 18:31:58 +02:00
) -> std ::result ::Result < Vec < TellerDetails > , mysql ::error ::Error > {
let mut teller_data = Vec ::new ( ) ;
conn . query_map (
" select teller_name, branch_name from teller_details where length(trim(coalesce(teller_name,''))) > 0 and length(trim(coalesce(branch_name,''))) > 0 order by id asc; " ,
| ( my_teller_name , my_branch_name ) | {
let teller_details = TellerDetails { teller_name : my_teller_name , branch_name : my_branch_name , } ;
teller_data . push ( teller_details ) ;
} ,
2023-07-17 18:55:17 +02:00
) . map ( | _ | teller_data )
2023-07-17 18:31:58 +02:00
}
fn select_customer_details (
2023-07-17 18:55:17 +02:00
conn : & mut mysql ::PooledConn ,
2023-07-17 18:31:58 +02:00
) -> std ::result ::Result < Vec < CustomerDetails > , mysql ::error ::Error > {
let mut customer_data = Vec ::new ( ) ;
conn . query_map (
" select customer_name, branch_name from customer_details where length(trim(coalesce(customer_name,''))) > 0 and length(trim(coalesce(branch_name,''))) > 0 order by id asc; " ,
| ( my_customer_name , my_branch_name ) | {
let teller_details = CustomerDetails { customer_name : my_customer_name , branch_name : my_branch_name , } ;
customer_data . push ( teller_details ) ;
} ,
2023-07-17 18:55:17 +02:00
) . map ( | _ | customer_data )
2023-07-17 18:31:58 +02:00
}