first steps
parent
33fbe778e7
commit
0e4fcc1601
|
@ -0,0 +1,7 @@
|
|||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Merchant {
|
||||
pub m_id: usize,
|
||||
pub m_name: String,
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
pub mod merchant;
|
48
src/main.rs
48
src/main.rs
|
@ -1,7 +1,10 @@
|
|||
use config::database::Db;
|
||||
use mysql::{prelude::Queryable, PooledConn, params};
|
||||
use database::database::Db;
|
||||
use mysql::PooledConn;
|
||||
use repository::merchant_repo;
|
||||
|
||||
pub mod config;
|
||||
pub mod database;
|
||||
pub mod entity;
|
||||
pub mod repository;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct Payment {
|
||||
|
@ -14,43 +17,10 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||
let db = Db::initialize();
|
||||
|
||||
let mut conn: PooledConn = db.get_connection();
|
||||
conn.query_drop(
|
||||
r"CREATE TEMPORARY TABLE payment (
|
||||
customer_id int not null,
|
||||
amount int not null,
|
||||
account_name text
|
||||
)")?;
|
||||
|
||||
let payments = vec![
|
||||
Payment { customer_id: 1, amount: 2, account_name: None },
|
||||
Payment { customer_id: 3, amount: 4, account_name: Some("foo".into()) },
|
||||
Payment { customer_id: 5, amount: 6, account_name: None },
|
||||
Payment { customer_id: 7, amount: 8, account_name: None },
|
||||
Payment { customer_id: 9, amount: 10, account_name: Some("bar".into()) },
|
||||
];
|
||||
|
||||
// Now let's insert payments to the database
|
||||
conn.exec_batch(
|
||||
r"INSERT INTO payment (customer_id, amount, account_name)
|
||||
VALUES (:customer_id, :amount, :account_name)",
|
||||
payments.iter().map(|p| params! {
|
||||
"customer_id" => p.customer_id,
|
||||
"amount" => p.amount,
|
||||
"account_name" => &p.account_name,
|
||||
})
|
||||
)?;
|
||||
|
||||
// Let's select payments from database. Type inference should do the trick here.
|
||||
let selected_payments = conn
|
||||
.query_map(
|
||||
"SELECT customer_id, amount, account_name from payment",
|
||||
|(customer_id, amount, account_name)| {
|
||||
Payment { customer_id, amount, account_name }
|
||||
},
|
||||
)?;
|
||||
for payment in selected_payments {
|
||||
let selected_payments = merchant_repo::find_by_name_or_id("1", &mut conn);
|
||||
selected_payments.into_iter().for_each(|payment| {
|
||||
println!("{:?}", payment);
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
use mysql::{params, prelude::Queryable, PooledConn};
|
||||
|
||||
use crate::entity::merchant::Merchant;
|
||||
|
||||
pub fn find_by_name_or_id(
|
||||
search: &str,
|
||||
conn: &mut PooledConn,
|
||||
) -> Result<Vec<Merchant>, mysql::Error> {
|
||||
let stat = conn
|
||||
.prep("SELECT m_name, m_id FROM global_data.merchant WHERE m_id = :search OR m_name LIKE :search;")
|
||||
.unwrap();
|
||||
|
||||
conn.exec_map(stat, params! {"search" => search}, |(m_name, m_id)| {
|
||||
Merchant { m_name, m_id }
|
||||
})
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub mod merchant_repo;
|
Loading…
Reference in New Issue