add postgres data source for accounts
This commit is contained in:
parent
e0358c2c67
commit
38baa25561
3 changed files with 31 additions and 0 deletions
29
src/data/account.rs
Normal file
29
src/data/account.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
use log::*;
|
||||
use serde;
|
||||
use sqlx::{Executor, PgPool};
|
||||
|
||||
use crate::core::{Id, Result};
|
||||
use crate::model::account::Account;
|
||||
|
||||
pub struct PgAccountDataSource {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PgAccountDataSource {
|
||||
pub fn new(pool: PgPool) -> PgAccountDataSource {
|
||||
PgAccountDataSource { pool }
|
||||
}
|
||||
|
||||
pub async fn by_id<I>(&self, id: I) -> Result<Account>
|
||||
where
|
||||
I: Into<Id> + Send,
|
||||
{
|
||||
let id: Id = id.into();
|
||||
debug!(target: "db", "Query accounts by id {}", id);
|
||||
let account: Account = sqlx::query_as("SELECT * FROM accounts WHERE id = $1")
|
||||
.bind(id)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
Ok(account)
|
||||
}
|
||||
}
|
1
src/data/mod.rs
Normal file
1
src/data/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod account;
|
|
@ -5,6 +5,7 @@ use sqlx::{migrate, query, PgPool, Pool};
|
|||
|
||||
mod conf;
|
||||
mod core;
|
||||
mod data;
|
||||
mod model;
|
||||
mod route;
|
||||
mod state;
|
||||
|
|
Loading…
Reference in a new issue