use sqlx::{Pool, Postgres}; use uuid::Uuid; use crate::users::User; use crate::Result; #[derive(Clone)] pub struct Users(Pool); impl Users { pub fn new(pool: Pool) -> Self { Self(pool) } pub async fn create(&self, user: User) -> Result<()> { sqlx::query!( r#"insert into users (id, username, password, email, bio, site, privacy, admin) values ($1, $2, $3, $4, $5, $6, $7, $8)"#, user.id, user.username, user.password, user.email, user.bio, user.site, user.privacy as _, user.admin ) .execute(&self.0) .await?; Ok(()) } pub async fn read(&self, user_id: Uuid) -> Result { Ok( sqlx::query_as!( User, "select id, username, password, email, bio, site, privacy as \"privacy: _\", admin from users where id = $1", user_id ) .fetch_one(&self.0) .await? ) } pub async fn read_username(&self, username: &str) -> Result { Ok( sqlx::query_as!( User, "select id, username, password, email, bio, site, privacy as \"privacy: _\", admin from users where username = $1", username ) .fetch_one(&self.0) .await? ) } pub async fn read_all(&self) -> Result> { Ok(sqlx::query_as("select * from users") .fetch_all(&self.0) .await?) } }