1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

deref to pool through data

This commit is contained in:
Rob Ede 2023-07-17 18:51:15 +01:00
parent dfd925d4c1
commit 43fdd8ad62
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933

View File

@ -1,4 +1,3 @@
use actix_web::web;
use mysql::{params, prelude::*}; use mysql::{params, prelude::*};
use crate::{ use crate::{
@ -8,11 +7,7 @@ use crate::{
const ERROR_MESSAGE: &str = "Error occurred during processing, please try again."; const ERROR_MESSAGE: &str = "Error occurred during processing, please try again.";
pub fn create_bank( pub fn create_bank(pool: &mysql::Pool, bank_name: String, _country: String) -> ResponseStatus {
data: &web::Data<mysql::Pool>,
bank_name: String,
_country: String,
) -> ResponseStatus {
let my_status_code: u8 = 1; let my_status_code: u8 = 1;
let my_status_description = ERROR_MESSAGE.to_owned(); let my_status_description = ERROR_MESSAGE.to_owned();
@ -31,13 +26,13 @@ pub fn create_bank(
return response_status; return response_status;
} }
match data.get_conn().and_then(|mut conn| { match pool.get_conn().and_then(|mut conn| {
insert_bank_data(&mut conn, bank_name.to_lowercase(), _country.to_lowercase()) insert_bank_data(&mut conn, bank_name.to_lowercase(), _country.to_lowercase())
}) { }) {
Ok(x) => { Ok(x) => {
if x > 0 { if x > 0 {
response_status.status_code = 0; response_status.status_code = 0;
response_status.status_description = String::from("Successful"); response_status.status_description = "Successful".to_owned();
} }
} }
Err(err) => println!("Failed to open DB connection. create_bank {err:?}"), Err(err) => println!("Failed to open DB connection. create_bank {err:?}"),
@ -46,11 +41,7 @@ pub fn create_bank(
response_status response_status
} }
pub fn create_branch( pub fn create_branch(pool: &mysql::Pool, branch_name: String, _location: String) -> ResponseStatus {
data: &web::Data<mysql::Pool>,
branch_name: String,
_location: String,
) -> ResponseStatus {
let my_status_code: u8 = 1; let my_status_code: u8 = 1;
let my_status_description = ERROR_MESSAGE.to_owned(); let my_status_description = ERROR_MESSAGE.to_owned();
@ -69,7 +60,7 @@ pub fn create_branch(
return response_status; return response_status;
} }
match data.get_conn().and_then(|mut conn| { match pool.get_conn().and_then(|mut conn| {
insert_branch_data( insert_branch_data(
&mut conn, &mut conn,
branch_name.to_lowercase(), branch_name.to_lowercase(),
@ -79,7 +70,7 @@ pub fn create_branch(
Ok(x) => { Ok(x) => {
if x > 0 { if x > 0 {
response_status.status_code = 0; response_status.status_code = 0;
response_status.status_description = String::from("Successful"); response_status.status_description = "Successful".to_owned();
} }
} }
Err(err) => println!("Failed to open DB connection. create_branch {err:?}"), Err(err) => println!("Failed to open DB connection. create_branch {err:?}"),
@ -89,7 +80,7 @@ pub fn create_branch(
} }
pub fn create_teller( pub fn create_teller(
data: &web::Data<mysql::Pool>, pool: &mysql::Pool,
teller_name: String, teller_name: String,
branch_name: String, branch_name: String,
) -> ResponseStatus { ) -> ResponseStatus {
@ -111,7 +102,7 @@ pub fn create_teller(
return response_status; return response_status;
} }
match data.get_conn().and_then(|mut conn| { match pool.get_conn().and_then(|mut conn| {
insert_teller_data( insert_teller_data(
&mut conn, &mut conn,
teller_name.to_lowercase(), teller_name.to_lowercase(),
@ -121,7 +112,7 @@ pub fn create_teller(
Ok(x) => { Ok(x) => {
if x > 0 { if x > 0 {
response_status.status_code = 0; response_status.status_code = 0;
response_status.status_description = String::from("Successful"); response_status.status_description = "Successful".to_owned();
} }
} }
Err(err) => println!("Failed to open DB connection. create_teller {err:?}"), Err(err) => println!("Failed to open DB connection. create_teller {err:?}"),
@ -131,7 +122,7 @@ pub fn create_teller(
} }
pub fn create_customer( pub fn create_customer(
data: &web::Data<mysql::Pool>, pool: &mysql::Pool,
customer_name: String, customer_name: String,
branch_name: String, branch_name: String,
) -> ResponseStatus { ) -> ResponseStatus {
@ -153,7 +144,7 @@ pub fn create_customer(
return response_status; return response_status;
} }
match data.get_conn().and_then(|mut conn| { match pool.get_conn().and_then(|mut conn| {
insert_customer_data( insert_customer_data(
&mut conn, &mut conn,
customer_name.to_lowercase(), customer_name.to_lowercase(),
@ -163,7 +154,7 @@ pub fn create_customer(
Ok(x) => { Ok(x) => {
if x > 0 { if x > 0 {
response_status.status_code = 0; response_status.status_code = 0;
response_status.status_description = String::from("Successful"); response_status.status_description = "Successful".to_owned();
} }
} }
Err(err) => println!("Failed to open DB connection. create_customer {err:?}"), Err(err) => println!("Failed to open DB connection. create_customer {err:?}"),
@ -172,28 +163,24 @@ pub fn create_customer(
response_status response_status
} }
pub fn get_bank_data(data: &web::Data<mysql::Pool>) -> BankResponseData { pub fn get_bank_data(pool: &mysql::Pool) -> BankResponseData {
let mut vec_bank_data = Vec::new(); let mut vec_bank_data = Vec::new();
let mut my_status_code: u8 = 1; let mut my_status_code = 1_u8;
let mut my_status_description: String = String::from("Record not found"); let mut my_status_description: String = String::from("Record not found");
match data match pool
.get_conn() .get_conn()
.and_then(|mut conn| select_bank_details(&mut conn)) .and_then(|mut conn| select_bank_details(&mut conn))
{ {
Ok(s) => { Ok(data) => vec_bank_data = data,
vec_bank_data = s;
}
Err(err) => println!("Failed to open DB connection. {err:?}"), Err(err) => println!("Failed to open DB connection. {err:?}"),
} }
if !vec_bank_data.is_empty() { if !vec_bank_data.is_empty() {
my_status_code = 0; my_status_code = 0;
my_status_description = String::from("Successful"); my_status_description = "Successful".to_owned();
} }
//Assign values to struct variable
BankResponseData { BankResponseData {
status_code: my_status_code, status_code: my_status_code,
status_description: my_status_description, status_description: my_status_description,
@ -201,24 +188,22 @@ pub fn get_bank_data(data: &web::Data<mysql::Pool>) -> BankResponseData {
} }
} }
pub fn get_branch_data(data: &web::Data<mysql::Pool>) -> BranchResponseData { pub fn get_branch_data(pool: &mysql::Pool) -> BranchResponseData {
let mut vec_branch_data = Vec::new(); let mut vec_branch_data = Vec::new();
let mut my_status_code: u8 = 1; let mut my_status_code = 1_u8;
let mut my_status_description: String = String::from("Record not found"); let mut my_status_description: String = String::from("Record not found");
match data match pool
.get_conn() .get_conn()
.and_then(|mut conn| select_branch_details(&mut conn)) .and_then(|mut conn| select_branch_details(&mut conn))
{ {
Ok(s) => { Ok(data) => vec_branch_data = data,
vec_branch_data = s;
}
Err(err) => println!("Failed to open DB connection. {err:?}"), Err(err) => println!("Failed to open DB connection. {err:?}"),
} }
if !vec_branch_data.is_empty() { if !vec_branch_data.is_empty() {
my_status_code = 0; my_status_code = 0;
my_status_description = String::from("Successful"); my_status_description = "Successful".to_owned();
} }
//Assign values to struct variable //Assign values to struct variable
@ -230,27 +215,24 @@ pub fn get_branch_data(data: &web::Data<mysql::Pool>) -> BranchResponseData {
} }
} }
pub fn get_teller_data(data: &web::Data<mysql::Pool>) -> TellerResponseData { pub fn get_teller_data(pool: &mysql::Pool) -> TellerResponseData {
let mut vec_teller_data = Vec::new(); let mut vec_teller_data = Vec::new();
let mut my_status_code: u8 = 1; let mut my_status_code = 1_u8;
let mut my_status_description: String = String::from("Record not found"); let mut my_status_description: String = String::from("Record not found");
match data match pool
.get_conn() .get_conn()
.and_then(|mut conn| select_teller_details(&mut conn)) .and_then(|mut conn| select_teller_details(&mut conn))
{ {
Ok(s) => { Ok(data) => vec_teller_data = data,
vec_teller_data = s;
}
Err(err) => println!("Failed to open DB connection. {err:?}"), Err(err) => println!("Failed to open DB connection. {err:?}"),
} }
if !vec_teller_data.is_empty() { if !vec_teller_data.is_empty() {
my_status_code = 0; my_status_code = 0;
my_status_description = String::from("Successful"); my_status_description = "Successful".to_owned();
} }
// assign values to struct variable
TellerResponseData { TellerResponseData {
status_code: my_status_code, status_code: my_status_code,
status_description: my_status_description, status_description: my_status_description,
@ -258,27 +240,24 @@ pub fn get_teller_data(data: &web::Data<mysql::Pool>) -> TellerResponseData {
} }
} }
pub fn get_customer_data(data: &web::Data<mysql::Pool>) -> CustomerResponseData { pub fn get_customer_data(pool: &mysql::Pool) -> CustomerResponseData {
let mut vec_customer_data = Vec::new(); let mut vec_customer_data = Vec::new();
let mut my_status_code: u8 = 1; let mut my_status_code = 1_u8;
let mut my_status_description: String = String::from("Record not found"); let mut my_status_description: String = String::from("Record not found");
match data match pool
.get_conn() .get_conn()
.and_then(|mut conn| select_customer_details(&mut conn)) .and_then(|mut conn| select_customer_details(&mut conn))
{ {
Ok(s) => { Ok(data) => vec_customer_data = data,
vec_customer_data = s;
}
Err(err) => println!("Failed to open DB connection. {err:?}"), Err(err) => println!("Failed to open DB connection. {err:?}"),
} }
if !vec_customer_data.is_empty() { if !vec_customer_data.is_empty() {
my_status_code = 0; my_status_code = 0;
my_status_description = String::from("Successful"); my_status_description = "Successful".to_owned();
} }
// assign values to struct variable
CustomerResponseData { CustomerResponseData {
status_code: my_status_code, status_code: my_status_code,
status_description: my_status_description, status_description: my_status_description,