user_service.rs
1 use sqlx::Pool; 2 use uuid::Uuid; 3 4 use crate::{ 5 db::{account_repository, login_details_repository, new_transaction, DB}, 6 models::{account::Account, login_details::LoginDetails}, 7 util::{accounts_error::AccountsError, uuid::uuid_to_sqlx}, 8 }; 9 10 #[derive(Debug, thiserror::Error)] 11 pub enum UserError { 12 #[error("The account doesn't exist")] 13 AccountNotFound, 14 #[error("Sqlx error")] 15 SqlxError(#[from] sqlx::Error), 16 #[error("Accounts error")] 17 AccountsError(#[from] AccountsError), 18 } 19 20 pub async fn get_me( 21 account_id: Uuid, 22 db_pool: &Pool<DB>, 23 ) -> Result<(Account, Option<LoginDetails>), UserError> { 24 let mut transaction = new_transaction(db_pool).await?; 25 26 let account_id = uuid_to_sqlx(account_id); 27 28 let acc = account_repository::get_account(&mut transaction, account_id) 29 .await? 30 .ok_or(UserError::AccountNotFound)?; 31 32 let login_details = 33 login_details_repository::get_by_account_id(&mut transaction, account_id).await?; 34 35 transaction.commit().await?; 36 37 Ok((acc, login_details)) 38 }