use anyhow::Error; use giterated_models::model::{repository::Repository, settings::AnySetting, user::User}; use sqlx::PgPool; use super::MetadataBackend; pub struct DatabaseSettings { pub pg_pool: PgPool, } #[async_trait::async_trait] impl MetadataBackend for DatabaseSettings { async fn user_get(&mut self, _user: &User, _name: &str) -> Result { todo!() } async fn user_write( &mut self, _user: &User, _name: &str, _value: AnySetting, ) -> Result<(), Error> { // for (name, value) in settings { // sqlx::query!("INSERT INTO user_settings VALUES ($1, $2, $3) ON CONFLICT (username, name) DO UPDATE SET value = $3", // user.username, name, value) // .execute(&self.pg_pool).await?; // } // Ok(()) todo!() } async fn repository_get( &mut self, _repository: &Repository, _name: &str, ) -> Result { // let settings = sqlx::query_as!( // RepositorySettingRow, // r#"SELECT * FROM repository_settings WHERE repository = $1"#, // repository.to_string() // ) // .fetch_many(&self.pg_pool) // .filter_map(|result| async move { // if let Ok(Either::Right(row)) = result { // Some(row) // } else { // None // } // }) // .filter_map(|row| async move { // if let Ok(value) = serde_json::from_str(&row.value) { // Some((row.name, value)) // } else { // None // } // }) // .collect::>() // .await; // Ok(settings) todo!() } async fn repository_write( &mut self, _repository: &Repository, _name: &str, _value: AnySetting, ) -> Result<(), Error> { // for (name, value) in settings { // sqlx::query!("INSERT INTO repository_settings VALUES ($1, $2, $3) ON CONFLICT (repository, name) DO UPDATE SET value = $3", // repository.to_string(), name, value) // .execute(&self.pg_pool).await?; // } // Ok(()) todo!() } } #[allow(unused)] #[derive(Debug, sqlx::FromRow)] struct UserSettingRow { pub username: String, pub name: String, pub value: String, } #[allow(unused)] #[derive(Debug, sqlx::FromRow)] struct RepositorySettingRow { pub repository: String, pub name: String, pub value: String, }