mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
refactor mysql models
This commit is contained in:
parent
a86471501d
commit
da976e0415
@ -24,12 +24,6 @@ pub struct CustomerData {
|
|||||||
pub branch_name: String,
|
pub branch_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
|
||||||
pub struct ResponseStatus {
|
|
||||||
pub status_code: u8,
|
|
||||||
pub status_description: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct BankDetails {
|
pub struct BankDetails {
|
||||||
pub bank_name: String,
|
pub bank_name: String,
|
||||||
@ -38,8 +32,6 @@ pub struct BankDetails {
|
|||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct BankResponseData {
|
pub struct BankResponseData {
|
||||||
pub status_code: u8,
|
|
||||||
pub status_description: String,
|
|
||||||
pub bank_data: Vec<BankDetails>,
|
pub bank_data: Vec<BankDetails>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,8 +43,6 @@ pub struct BranchDetails {
|
|||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct BranchResponseData {
|
pub struct BranchResponseData {
|
||||||
pub status_code: u8,
|
|
||||||
pub status_description: String,
|
|
||||||
pub branch_data: Vec<BranchDetails>,
|
pub branch_data: Vec<BranchDetails>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,8 +54,6 @@ pub struct TellerDetails {
|
|||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct TellerResponseData {
|
pub struct TellerResponseData {
|
||||||
pub status_code: u8,
|
|
||||||
pub status_description: String,
|
|
||||||
pub teller_data: Vec<TellerDetails>,
|
pub teller_data: Vec<TellerDetails>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +65,5 @@ pub struct CustomerDetails {
|
|||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct CustomerResponseData {
|
pub struct CustomerResponseData {
|
||||||
pub status_code: u8,
|
|
||||||
pub status_description: String,
|
|
||||||
pub customer_data: Vec<CustomerDetails>,
|
pub customer_data: Vec<CustomerDetails>,
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
|
use actix_web::http::StatusCode;
|
||||||
use derive_more::{Display, Error, From};
|
use derive_more::{Display, Error, From};
|
||||||
use mysql::{params, prelude::*};
|
use mysql::{params, prelude::*};
|
||||||
|
|
||||||
use crate::models::{
|
use crate::models::{
|
||||||
BankDetails, BankResponseData, BranchDetails, BranchResponseData, CustomerDetails,
|
BankDetails, BankResponseData, BranchDetails, BranchResponseData, CustomerDetails,
|
||||||
CustomerResponseData, ResponseStatus, TellerDetails, TellerResponseData,
|
CustomerResponseData, TellerDetails, TellerResponseData,
|
||||||
};
|
};
|
||||||
|
|
||||||
const ERROR_MESSAGE: &str = "Error occurred during processing, please try again.";
|
|
||||||
|
|
||||||
#[derive(Debug, Display, Error, From)]
|
#[derive(Debug, Display, Error, From)]
|
||||||
pub enum PersistenceError {
|
pub enum PersistenceError {
|
||||||
EmptyBankName,
|
EmptyBankName,
|
||||||
@ -16,12 +15,28 @@ pub enum PersistenceError {
|
|||||||
EmptyLocation,
|
EmptyLocation,
|
||||||
EmptyTellerName,
|
EmptyTellerName,
|
||||||
EmptyCustomerName,
|
EmptyCustomerName,
|
||||||
|
|
||||||
MysqlError(mysql::Error),
|
MysqlError(mysql::Error),
|
||||||
|
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl actix_web::ResponseError for PersistenceError {}
|
impl actix_web::ResponseError for PersistenceError {
|
||||||
|
fn status_code(&self) -> StatusCode {
|
||||||
|
match self {
|
||||||
|
PersistenceError::EmptyBankName
|
||||||
|
| PersistenceError::EmptyCountry
|
||||||
|
| PersistenceError::EmptyBranch
|
||||||
|
| PersistenceError::EmptyLocation
|
||||||
|
| PersistenceError::EmptyTellerName
|
||||||
|
| PersistenceError::EmptyCustomerName => StatusCode::BAD_REQUEST,
|
||||||
|
|
||||||
|
PersistenceError::MysqlError(_) | PersistenceError::Unknown => {
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create_bank(
|
pub fn create_bank(
|
||||||
pool: &mysql::Pool,
|
pool: &mysql::Pool,
|
||||||
@ -38,246 +53,143 @@ pub fn create_bank(
|
|||||||
|
|
||||||
let mut conn = pool.get_conn()?;
|
let mut conn = pool.get_conn()?;
|
||||||
|
|
||||||
let rows_inserted =
|
let last_insert_id =
|
||||||
insert_bank_data(&mut conn, bank_name.to_lowercase(), country.to_lowercase())?;
|
insert_bank_data(&mut conn, bank_name.to_lowercase(), country.to_lowercase())?;
|
||||||
|
|
||||||
if rows_inserted > 0 {
|
if last_insert_id > 0 {
|
||||||
return Ok(());
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(PersistenceError::Unknown)
|
Err(PersistenceError::Unknown)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_branch(pool: &mysql::Pool, branch_name: String, location: String) -> ResponseStatus {
|
pub fn create_branch(
|
||||||
let my_status_code: u8 = 1;
|
pool: &mysql::Pool,
|
||||||
let my_status_description = ERROR_MESSAGE.to_owned();
|
branch_name: String,
|
||||||
|
location: String,
|
||||||
let mut response_status = ResponseStatus {
|
) -> Result<(), PersistenceError> {
|
||||||
status_code: my_status_code,
|
|
||||||
status_description: my_status_description,
|
|
||||||
};
|
|
||||||
|
|
||||||
if branch_name.replace(' ', "").trim().is_empty() {
|
if branch_name.replace(' ', "").trim().is_empty() {
|
||||||
response_status.status_description = String::from("Branch name is empty!");
|
return Err(PersistenceError::EmptyBranch);
|
||||||
return response_status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if location.replace(' ', "").trim().is_empty() {
|
if location.replace(' ', "").trim().is_empty() {
|
||||||
response_status.status_description = String::from("Location is empty!");
|
return Err(PersistenceError::EmptyLocation);
|
||||||
return response_status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
match pool.get_conn().and_then(|mut conn| {
|
let mut conn = pool.get_conn()?;
|
||||||
insert_branch_data(
|
|
||||||
|
let last_insert_id = insert_branch_data(
|
||||||
&mut conn,
|
&mut conn,
|
||||||
branch_name.to_lowercase(),
|
branch_name.to_lowercase(),
|
||||||
location.to_lowercase(),
|
location.to_lowercase(),
|
||||||
)
|
)?;
|
||||||
}) {
|
|
||||||
Ok(x) => {
|
|
||||||
if x > 0 {
|
|
||||||
response_status.status_code = 0;
|
|
||||||
response_status.status_description = "Successful".to_owned();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => println!("Failed to open DB connection. create_branch {err:?}"),
|
|
||||||
}
|
|
||||||
|
|
||||||
response_status
|
if last_insert_id > 0 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(PersistenceError::Unknown)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_teller(
|
pub fn create_teller(
|
||||||
pool: &mysql::Pool,
|
pool: &mysql::Pool,
|
||||||
teller_name: String,
|
teller_name: String,
|
||||||
branch_name: String,
|
branch_name: String,
|
||||||
) -> ResponseStatus {
|
) -> Result<(), PersistenceError> {
|
||||||
let my_status_code: u8 = 1;
|
|
||||||
let my_status_description = ERROR_MESSAGE.to_owned();
|
|
||||||
|
|
||||||
let mut response_status = ResponseStatus {
|
|
||||||
status_code: my_status_code,
|
|
||||||
status_description: my_status_description,
|
|
||||||
};
|
|
||||||
|
|
||||||
if teller_name.replace(' ', "").trim().is_empty() {
|
if teller_name.replace(' ', "").trim().is_empty() {
|
||||||
response_status.status_description = String::from("Teller name is empty!");
|
return Err(PersistenceError::EmptyTellerName);
|
||||||
return response_status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if branch_name.replace(' ', "").trim().is_empty() {
|
if branch_name.replace(' ', "").trim().is_empty() {
|
||||||
response_status.status_description = String::from("Branch name is empty!");
|
return Err(PersistenceError::EmptyBranch);
|
||||||
return response_status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
match pool.get_conn().and_then(|mut conn| {
|
let mut conn = pool.get_conn()?;
|
||||||
insert_teller_data(
|
|
||||||
|
let last_insert_id = insert_teller_data(
|
||||||
&mut conn,
|
&mut conn,
|
||||||
teller_name.to_lowercase(),
|
teller_name.to_lowercase(),
|
||||||
branch_name.to_lowercase(),
|
branch_name.to_lowercase(),
|
||||||
)
|
)?;
|
||||||
}) {
|
|
||||||
Ok(x) => {
|
|
||||||
if x > 0 {
|
|
||||||
response_status.status_code = 0;
|
|
||||||
response_status.status_description = "Successful".to_owned();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => println!("Failed to open DB connection. create_teller {err:?}"),
|
|
||||||
}
|
|
||||||
|
|
||||||
response_status
|
if last_insert_id > 0 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(PersistenceError::Unknown)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_customer(
|
pub fn create_customer(
|
||||||
pool: &mysql::Pool,
|
pool: &mysql::Pool,
|
||||||
customer_name: String,
|
customer_name: String,
|
||||||
branch_name: String,
|
branch_name: String,
|
||||||
) -> ResponseStatus {
|
) -> Result<(), PersistenceError> {
|
||||||
let my_status_code: u8 = 1;
|
|
||||||
let my_status_description = ERROR_MESSAGE.to_owned();
|
|
||||||
|
|
||||||
let mut response_status = ResponseStatus {
|
|
||||||
status_code: my_status_code,
|
|
||||||
status_description: my_status_description,
|
|
||||||
};
|
|
||||||
|
|
||||||
if customer_name.replace(' ', "").trim().is_empty() {
|
if customer_name.replace(' ', "").trim().is_empty() {
|
||||||
response_status.status_description = String::from("Customer name is empty!");
|
return Err(PersistenceError::EmptyCustomerName);
|
||||||
return response_status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if branch_name.replace(' ', "").trim().is_empty() {
|
if branch_name.replace(' ', "").trim().is_empty() {
|
||||||
response_status.status_description = String::from("Branch name is empty!");
|
return Err(PersistenceError::EmptyBranch);
|
||||||
return response_status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
match pool.get_conn().and_then(|mut conn| {
|
let mut conn = pool.get_conn()?;
|
||||||
insert_customer_data(
|
|
||||||
|
let last_insert_id = insert_customer_data(
|
||||||
&mut conn,
|
&mut conn,
|
||||||
customer_name.to_lowercase(),
|
customer_name.to_lowercase(),
|
||||||
branch_name.to_lowercase(),
|
branch_name.to_lowercase(),
|
||||||
)
|
)?;
|
||||||
}) {
|
|
||||||
Ok(x) => {
|
|
||||||
if x > 0 {
|
|
||||||
response_status.status_code = 0;
|
|
||||||
response_status.status_description = "Successful".to_owned();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => println!("Failed to open DB connection. create_customer {err:?}"),
|
|
||||||
}
|
|
||||||
|
|
||||||
response_status
|
if last_insert_id > 0 {
|
||||||
}
|
Ok(())
|
||||||
|
} else {
|
||||||
pub fn get_bank_data(pool: &mysql::Pool) -> BankResponseData {
|
Err(PersistenceError::Unknown)
|
||||||
let mut vec_bank_data = Vec::new();
|
|
||||||
let mut my_status_code = 1_u8;
|
|
||||||
let mut my_status_description: String = String::from("Record not found");
|
|
||||||
|
|
||||||
match pool
|
|
||||||
.get_conn()
|
|
||||||
.and_then(|mut conn| select_bank_details(&mut conn))
|
|
||||||
{
|
|
||||||
Ok(data) => vec_bank_data = data,
|
|
||||||
Err(err) => println!("Failed to open DB connection. {err:?}"),
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vec_bank_data.is_empty() {
|
|
||||||
my_status_code = 0;
|
|
||||||
my_status_description = "Successful".to_owned();
|
|
||||||
}
|
|
||||||
|
|
||||||
BankResponseData {
|
|
||||||
status_code: my_status_code,
|
|
||||||
status_description: my_status_description,
|
|
||||||
bank_data: vec_bank_data,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_branch_data(pool: &mysql::Pool) -> BranchResponseData {
|
pub fn get_bank_data(pool: &mysql::Pool) -> Result<BankResponseData, PersistenceError> {
|
||||||
let mut vec_branch_data = Vec::new();
|
let mut conn = pool.get_conn()?;
|
||||||
let mut my_status_code = 1_u8;
|
|
||||||
let mut my_status_description: String = String::from("Record not found");
|
|
||||||
|
|
||||||
match pool
|
Ok(BankResponseData {
|
||||||
.get_conn()
|
bank_data: select_bank_details(&mut conn)?,
|
||||||
.and_then(|mut conn| select_branch_details(&mut conn))
|
})
|
||||||
{
|
|
||||||
Ok(data) => vec_branch_data = data,
|
|
||||||
Err(err) => println!("Failed to open DB connection. {err:?}"),
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vec_branch_data.is_empty() {
|
|
||||||
my_status_code = 0;
|
|
||||||
my_status_description = "Successful".to_owned();
|
|
||||||
}
|
|
||||||
|
|
||||||
BranchResponseData {
|
|
||||||
status_code: my_status_code,
|
|
||||||
status_description: my_status_description,
|
|
||||||
branch_data: vec_branch_data,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_teller_data(pool: &mysql::Pool) -> TellerResponseData {
|
pub fn get_branch_data(pool: &mysql::Pool) -> Result<BranchResponseData, PersistenceError> {
|
||||||
let mut vec_teller_data = Vec::new();
|
let mut conn = pool.get_conn()?;
|
||||||
let mut my_status_code = 1_u8;
|
|
||||||
let mut my_status_description: String = String::from("Record not found");
|
|
||||||
|
|
||||||
match pool
|
Ok(BranchResponseData {
|
||||||
.get_conn()
|
branch_data: select_branch_details(&mut conn)?,
|
||||||
.and_then(|mut conn| select_teller_details(&mut conn))
|
})
|
||||||
{
|
|
||||||
Ok(data) => vec_teller_data = data,
|
|
||||||
Err(err) => println!("Failed to open DB connection. {err:?}"),
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vec_teller_data.is_empty() {
|
|
||||||
my_status_code = 0;
|
|
||||||
my_status_description = "Successful".to_owned();
|
|
||||||
}
|
|
||||||
|
|
||||||
TellerResponseData {
|
|
||||||
status_code: my_status_code,
|
|
||||||
status_description: my_status_description,
|
|
||||||
teller_data: vec_teller_data,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_customer_data(pool: &mysql::Pool) -> CustomerResponseData {
|
pub fn get_teller_data(pool: &mysql::Pool) -> Result<TellerResponseData, PersistenceError> {
|
||||||
let mut vec_customer_data = Vec::new();
|
let mut conn = pool.get_conn()?;
|
||||||
let mut my_status_code = 1_u8;
|
|
||||||
let mut my_status_description: String = String::from("Record not found");
|
|
||||||
|
|
||||||
match pool
|
Ok(TellerResponseData {
|
||||||
.get_conn()
|
teller_data: select_teller_details(&mut conn)?,
|
||||||
.and_then(|mut conn| select_customer_details(&mut conn))
|
})
|
||||||
{
|
|
||||||
Ok(data) => vec_customer_data = data,
|
|
||||||
Err(err) => println!("Failed to open DB connection. {err:?}"),
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vec_customer_data.is_empty() {
|
|
||||||
my_status_code = 0;
|
|
||||||
my_status_description = "Successful".to_owned();
|
|
||||||
}
|
|
||||||
|
|
||||||
CustomerResponseData {
|
|
||||||
status_code: my_status_code,
|
|
||||||
status_description: my_status_description,
|
|
||||||
customer_data: vec_customer_data,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_customer_data(pool: &mysql::Pool) -> Result<CustomerResponseData, PersistenceError> {
|
||||||
|
let mut conn = pool.get_conn()?;
|
||||||
|
|
||||||
|
Ok(CustomerResponseData {
|
||||||
|
customer_data: select_customer_details(&mut conn)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Insert data into the database table `bank_details`.
|
||||||
fn insert_bank_data(
|
fn insert_bank_data(
|
||||||
conn: &mut mysql::PooledConn,
|
conn: &mut mysql::PooledConn,
|
||||||
my_bank_name: String,
|
my_bank_name: String,
|
||||||
my_country: String,
|
my_country: String,
|
||||||
) -> std::result::Result<u64, mysql::error::Error> {
|
) -> mysql::error::Result<u64> {
|
||||||
// Insert data into the database table bank_details
|
|
||||||
conn.exec_drop(
|
conn.exec_drop(
|
||||||
"insert into bank_details (bank_name, country) values (:bank_name, :country);",
|
"
|
||||||
|
INSERT INTO bank_details (bank_name, country)
|
||||||
|
VALUES (:bank_name, :country)
|
||||||
|
",
|
||||||
params! {
|
params! {
|
||||||
"bank_name" => my_bank_name,
|
"bank_name" => my_bank_name,
|
||||||
"country" => my_country,
|
"country" => my_country,
|
||||||
@ -286,14 +198,17 @@ fn insert_bank_data(
|
|||||||
.map(|_| conn.last_insert_id())
|
.map(|_| conn.last_insert_id())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Insert data into the database table `branch_details`.
|
||||||
fn insert_branch_data(
|
fn insert_branch_data(
|
||||||
conn: &mut mysql::PooledConn,
|
conn: &mut mysql::PooledConn,
|
||||||
my_branch_name: String,
|
my_branch_name: String,
|
||||||
my_location: String,
|
my_location: String,
|
||||||
) -> std::result::Result<u64, mysql::error::Error> {
|
) -> mysql::error::Result<u64> {
|
||||||
// Insert data into the database table branch_details
|
|
||||||
conn.exec_drop(
|
conn.exec_drop(
|
||||||
"insert into branch_details (branch_name, location) values (:branch_name, :location);",
|
"
|
||||||
|
INSERT INTO branch_details (branch_name, location)
|
||||||
|
VALUES (:branch_name, :location)
|
||||||
|
",
|
||||||
params! {
|
params! {
|
||||||
"branch_name" => my_branch_name,
|
"branch_name" => my_branch_name,
|
||||||
"location" => my_location,
|
"location" => my_location,
|
||||||
@ -302,88 +217,106 @@ fn insert_branch_data(
|
|||||||
.map(|_| conn.last_insert_id())
|
.map(|_| conn.last_insert_id())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Insert data into the database table `teller_details`.
|
||||||
fn insert_teller_data(
|
fn insert_teller_data(
|
||||||
conn: &mut mysql::PooledConn,
|
conn: &mut mysql::PooledConn,
|
||||||
my_teller_name: String,
|
my_teller_name: String,
|
||||||
my_branch_name: String,
|
my_branch_name: String,
|
||||||
) -> std::result::Result<u64, mysql::error::Error> {
|
) -> mysql::error::Result<u64> {
|
||||||
// Insert data into the database table teller_details
|
|
||||||
conn.exec_drop(
|
conn.exec_drop(
|
||||||
"insert into teller_details (teller_name, branch_name) values (:teller_name, :branch_name);",
|
"
|
||||||
|
INSERT INTO teller_details (teller_name, branch_name)
|
||||||
|
VALUES (:teller_name, :branch_name)
|
||||||
|
",
|
||||||
params! {
|
params! {
|
||||||
"teller_name" => my_teller_name,
|
"teller_name" => my_teller_name,
|
||||||
"branch_name" => my_branch_name,
|
"branch_name" => my_branch_name,
|
||||||
},
|
},
|
||||||
).map(|_| conn.last_insert_id())
|
)
|
||||||
|
.map(|_| conn.last_insert_id())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Insert data into the database table `customer_details`.
|
||||||
fn insert_customer_data(
|
fn insert_customer_data(
|
||||||
conn: &mut mysql::PooledConn,
|
conn: &mut mysql::PooledConn,
|
||||||
my_customer_name: String,
|
my_customer_name: String,
|
||||||
my_branch_name: String,
|
my_branch_name: String,
|
||||||
) -> std::result::Result<u64, mysql::error::Error> {
|
) -> mysql::error::Result<u64> {
|
||||||
// Insert data into the database table customer_details
|
|
||||||
conn.exec_drop(
|
conn.exec_drop(
|
||||||
"insert into customer_details (customer_name, branch_name) values (:customer_name, :branch_name);",
|
r"
|
||||||
|
INSERT INTO customer_details (customer_name, branch_name)
|
||||||
|
VALUES (:customer_name, :branch_name)
|
||||||
|
",
|
||||||
params! {
|
params! {
|
||||||
"customer_name" => my_customer_name,
|
"customer_name" => my_customer_name,
|
||||||
"branch_name" => my_branch_name,
|
"branch_name" => my_branch_name,
|
||||||
},
|
},
|
||||||
).map(|_| conn.last_insert_id())
|
)
|
||||||
|
.map(|_| conn.last_insert_id())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn select_bank_details(
|
/// Lists all banks' details.
|
||||||
conn: &mut mysql::PooledConn,
|
fn select_bank_details(conn: &mut mysql::PooledConn) -> mysql::error::Result<Vec<BankDetails>> {
|
||||||
) -> std::result::Result<Vec<BankDetails>, mysql::error::Error> {
|
|
||||||
let mut bank_data = Vec::new();
|
|
||||||
|
|
||||||
conn.query_map(
|
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;",
|
r"
|
||||||
|(my_bank_name, my_country)| {
|
SELECT bank_name, country FROM bank_details
|
||||||
let bank_details = BankDetails { bank_name: my_bank_name, country: my_country, };
|
WHERE LENGTH(TRIM(COALESCE(bank_name, ''))) > 0
|
||||||
bank_data.push(bank_details);
|
AND LENGTH(TRIM(COALESCE(country, ''))) > 0
|
||||||
|
ORDER BY id ASC
|
||||||
|
",
|
||||||
|
|(my_bank_name, my_country)| BankDetails {
|
||||||
|
bank_name: my_bank_name,
|
||||||
|
country: my_country,
|
||||||
},
|
},
|
||||||
).map(|_| bank_data)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn select_branch_details(
|
/// Lists all branches' details.
|
||||||
conn: &mut mysql::PooledConn,
|
fn select_branch_details(conn: &mut mysql::PooledConn) -> mysql::error::Result<Vec<BranchDetails>> {
|
||||||
) -> std::result::Result<Vec<BranchDetails>, mysql::error::Error> {
|
|
||||||
let mut branch_data = Vec::new();
|
|
||||||
|
|
||||||
conn.query_map(
|
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;",
|
r"
|
||||||
|(my_branch_name, my_location)| {
|
SELECT branch_name, location FROM branch_details
|
||||||
let branch_details = BranchDetails { branch_name: my_branch_name, location: my_location, };
|
WHERE LENGTH(TRIM(COALESCE(branch_name, ''))) > 0
|
||||||
branch_data.push(branch_details);
|
AND LENGTH(TRIM(COALESCE(location, ''))) > 0
|
||||||
|
ORDER BY id ASC
|
||||||
|
",
|
||||||
|
|(my_branch_name, my_location)| BranchDetails {
|
||||||
|
branch_name: my_branch_name,
|
||||||
|
location: my_location,
|
||||||
},
|
},
|
||||||
).map(|_| branch_data)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn select_teller_details(
|
/// Lists all tellers' details.
|
||||||
conn: &mut mysql::PooledConn,
|
fn select_teller_details(conn: &mut mysql::PooledConn) -> mysql::error::Result<Vec<TellerDetails>> {
|
||||||
) -> std::result::Result<Vec<TellerDetails>, mysql::error::Error> {
|
|
||||||
let mut teller_data = Vec::new();
|
|
||||||
|
|
||||||
conn.query_map(
|
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;",
|
r"
|
||||||
|(my_teller_name, my_branch_name)| {
|
SELECT teller_name, branch_name FROM teller_details
|
||||||
let teller_details = TellerDetails { teller_name: my_teller_name, branch_name: my_branch_name, };
|
WHERE LENGTH(TRIM(COALESCE(teller_name, ''))) > 0
|
||||||
teller_data.push(teller_details);
|
AND LENGTH(TRIM(COALESCE(branch_name, ''))) > 0
|
||||||
|
ORDER BY id ASC
|
||||||
|
",
|
||||||
|
|(my_teller_name, my_branch_name)| TellerDetails {
|
||||||
|
teller_name: my_teller_name,
|
||||||
|
branch_name: my_branch_name,
|
||||||
},
|
},
|
||||||
).map(|_| teller_data)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Lists all customers' details.
|
||||||
fn select_customer_details(
|
fn select_customer_details(
|
||||||
conn: &mut mysql::PooledConn,
|
conn: &mut mysql::PooledConn,
|
||||||
) -> std::result::Result<Vec<CustomerDetails>, mysql::error::Error> {
|
) -> mysql::error::Result<Vec<CustomerDetails>> {
|
||||||
let mut customer_data = Vec::new();
|
|
||||||
|
|
||||||
conn.query_map(
|
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;",
|
r"
|
||||||
|(my_customer_name, my_branch_name)| {
|
SELECT customer_name, branch_name FROM customer_details
|
||||||
let teller_details = CustomerDetails { customer_name: my_customer_name, branch_name: my_branch_name, };
|
WHERE LENGTH(TRIM(COALESCE(customer_name, ''))) > 0
|
||||||
customer_data.push(teller_details);
|
AND LENGTH(TRIM(COALESCE(branch_name, ''))) > 0
|
||||||
|
ORDER BY id ASC
|
||||||
|
",
|
||||||
|
|(my_customer_name, my_branch_name)| CustomerDetails {
|
||||||
|
customer_name: my_customer_name,
|
||||||
|
branch_name: my_branch_name,
|
||||||
},
|
},
|
||||||
).map(|_| customer_data)
|
)
|
||||||
}
|
}
|
||||||
|
@ -19,9 +19,9 @@ pub(crate) async fn add_bank(
|
|||||||
data: web::Data<mysql::Pool>,
|
data: web::Data<mysql::Pool>,
|
||||||
) -> actix_web::Result<impl Responder> {
|
) -> actix_web::Result<impl Responder> {
|
||||||
let bank_name = bank_data.bank_name;
|
let bank_name = bank_data.bank_name;
|
||||||
let _country = bank_data.country;
|
let country = bank_data.country;
|
||||||
|
|
||||||
create_bank(&data, bank_name, _country)?;
|
web::block(move || create_bank(&data, bank_name, country)).await??;
|
||||||
|
|
||||||
Ok(HttpResponse::NoContent())
|
Ok(HttpResponse::NoContent())
|
||||||
}
|
}
|
||||||
@ -32,11 +32,11 @@ pub(crate) async fn add_branch(
|
|||||||
data: web::Data<mysql::Pool>,
|
data: web::Data<mysql::Pool>,
|
||||||
) -> actix_web::Result<impl Responder> {
|
) -> actix_web::Result<impl Responder> {
|
||||||
let branch_name = branch_data.branch_name;
|
let branch_name = branch_data.branch_name;
|
||||||
let _location = branch_data.location;
|
let location = branch_data.location;
|
||||||
|
|
||||||
let response_data = create_branch(&data, branch_name, _location);
|
web::block(move || create_branch(&data, branch_name, location)).await??;
|
||||||
|
|
||||||
Ok(web::Json(response_data))
|
Ok(HttpResponse::NoContent())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/teller")]
|
#[post("/teller")]
|
||||||
@ -47,9 +47,9 @@ pub(crate) async fn add_teller(
|
|||||||
let teller_name = teller_data.teller_name;
|
let teller_name = teller_data.teller_name;
|
||||||
let branch_name = teller_data.branch_name;
|
let branch_name = teller_data.branch_name;
|
||||||
|
|
||||||
let response_data = create_teller(&data, teller_name, branch_name);
|
web::block(move || create_teller(&data, teller_name, branch_name)).await??;
|
||||||
|
|
||||||
Ok(web::Json(response_data))
|
Ok(HttpResponse::NoContent())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/customer")]
|
#[post("/customer")]
|
||||||
@ -60,26 +60,26 @@ pub(crate) async fn add_customer(
|
|||||||
let customer_name = customer_data.customer_name;
|
let customer_name = customer_data.customer_name;
|
||||||
let branch_name = customer_data.branch_name;
|
let branch_name = customer_data.branch_name;
|
||||||
|
|
||||||
let response_data = create_customer(&data, customer_name, branch_name);
|
web::block(move || create_customer(&data, customer_name, branch_name)).await??;
|
||||||
|
|
||||||
Ok(web::Json(response_data))
|
Ok(HttpResponse::NoContent())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/bank")]
|
#[get("/bank")]
|
||||||
pub(crate) async fn get_bank(data: web::Data<mysql::Pool>) -> actix_web::Result<impl Responder> {
|
pub(crate) async fn get_bank(data: web::Data<mysql::Pool>) -> actix_web::Result<impl Responder> {
|
||||||
let bank_response_data = get_bank_data(&data);
|
let bank_response_data = web::block(move || get_bank_data(&data)).await??;
|
||||||
Ok(web::Json(bank_response_data))
|
Ok(web::Json(bank_response_data))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/branch")]
|
#[get("/branch")]
|
||||||
pub(crate) async fn get_branch(data: web::Data<mysql::Pool>) -> actix_web::Result<impl Responder> {
|
pub(crate) async fn get_branch(data: web::Data<mysql::Pool>) -> actix_web::Result<impl Responder> {
|
||||||
let branch_response_data = get_branch_data(&data);
|
let branch_response_data = web::block(move || get_branch_data(&data)).await??;
|
||||||
Ok(web::Json(branch_response_data))
|
Ok(web::Json(branch_response_data))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/teller")]
|
#[get("/teller")]
|
||||||
pub(crate) async fn get_teller(data: web::Data<mysql::Pool>) -> actix_web::Result<impl Responder> {
|
pub(crate) async fn get_teller(data: web::Data<mysql::Pool>) -> actix_web::Result<impl Responder> {
|
||||||
let teller_response_data = get_teller_data(&data);
|
let teller_response_data = web::block(move || get_teller_data(&data)).await??;
|
||||||
Ok(web::Json(teller_response_data))
|
Ok(web::Json(teller_response_data))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,6 +87,6 @@ pub(crate) async fn get_teller(data: web::Data<mysql::Pool>) -> actix_web::Resul
|
|||||||
pub(crate) async fn get_customer(
|
pub(crate) async fn get_customer(
|
||||||
data: web::Data<mysql::Pool>,
|
data: web::Data<mysql::Pool>,
|
||||||
) -> actix_web::Result<impl Responder> {
|
) -> actix_web::Result<impl Responder> {
|
||||||
let customer_response_data = get_customer_data(&data);
|
let customer_response_data = web::block(move || get_customer_data(&data)).await??;
|
||||||
Ok(web::Json(customer_response_data))
|
Ok(web::Json(customer_response_data))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user