add postgres data source for accounts

This commit is contained in:
anna 2022-12-05 20:36:30 +01:00
parent e0358c2c67
commit 38baa25561
Signed by: fef
GPG key ID: EC22E476DC2D3D84
3 changed files with 31 additions and 0 deletions

29
src/data/account.rs Normal file
View 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
View file

@ -0,0 +1 @@
pub mod account;

View file

@ -5,6 +5,7 @@ use sqlx::{migrate, query, PgPool, Pool};
mod conf;
mod core;
mod data;
mod model;
mod route;
mod state;