1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use super::{Account, AccountStatus};
use sqlx::postgres::PgRow;
use sqlx::{Acquire, Row};

use crate::events::*;

pub struct AccountRepository {}

impl AccountRepository {
    pub async fn create(
        account: &account::AccountOpened,
        mut pool: sqlx::pool::PoolConnection<sqlx::Postgres>,
    ) -> Result<Account, sqlx::Error> {
        let mut tx = pool.begin().await?;
        let todo = sqlx::query(
            "INSERT INTO accounts (account_id, name) VALUES ($1, $2) RETURNING account_id, name, balance",
        )
        .bind(account.account_id)
        .bind(&account.name)
        .map(|row: PgRow| Account {
            account_id: row.get(0),
            name: row.get(1),
            status: AccountStatus::Active,
            balance: row.get::<i64, _>(2),
        })
        .fetch_one(&mut tx)
        .await?;

        tx.commit().await?;
        Ok(todo)
    }
}