use crate::error::Result;

use sqlx::{PgPool, query_scalar};

pub struct AccountRepository<'a> {
    pub pool: &'a PgPool,
}

impl<'a> AccountRepository<'a> {
    pub fn new(pool: &'a PgPool) -> Self {
        Self { pool }
    }

    pub async fn get_access_token(&self, user_id: &str) -> Result<String> {
        query_scalar("SELECT access_token FROM account WHERE user_id = $1")
            .bind(user_id)
            .fetch_one(self.pool)
            .await
            .map_err(|_| crate::error::Error::AccessToken)
    }
}
