use std::error::Error; use sqlx::PgPool; use crate::messages::user::{ UserBioRequest, UserBioResponse, UserDisplayImageRequest, UserDisplayImageResponse, UserDisplayNameRequest, UserDisplayNameResponse, }; use super::{AuthBackend, UserBackend}; pub struct UserAuth { pub pg_pool: PgPool, } impl UserAuth { pub fn new(pool: PgPool) -> Self { Self { pg_pool: pool } } } #[async_trait::async_trait] impl UserBackend for UserAuth { async fn display_name( &mut self, request: UserDisplayNameRequest, ) -> Result> { let db_row = sqlx::query_as!( UserRow, r#"SELECT * FROM users WHERE username = $1"#, request.user.username ) .fetch_one(&self.pg_pool.clone()) .await.unwrap(); Ok(UserDisplayNameResponse { display_name: db_row.display_name, }) } async fn display_image( &mut self, request: UserDisplayImageRequest, ) -> Result> { let db_row = sqlx::query_as!( UserRow, r#"SELECT * FROM users WHERE username = $1"#, request.user.username ) .fetch_one(&self.pg_pool.clone()) .await.unwrap(); Ok(UserDisplayImageResponse { image_url: db_row.image_url, }) } async fn bio( &mut self, request: UserBioRequest, ) -> Result> { let db_row = sqlx::query_as!( UserRow, r#"SELECT * FROM users WHERE username = $1"#, request.user.username ) .fetch_one(&self.pg_pool.clone()) .await.unwrap(); Ok(UserBioResponse { bio: db_row.bio }) } } #[async_trait::async_trait] impl AuthBackend for UserAuth { async fn register(&mut self, request: ()) -> Result<(), Box> { todo!() } async fn login(&mut self, request: ()) -> Result<(), Box> { todo!() } } #[derive(Debug, sqlx::FromRow)] struct UserRow { pub username: String, pub image_url: Option, pub display_name: Option, pub bio: Option, pub email: Option, pub password: String, }