JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Fix handling stack

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨c53b026

⁨giterated-daemon/src/backend/settings.rs⁩ - ⁨2390⁩ bytes
Raw
1 use anyhow::Error;
2
3 use giterated_models::repository::Repository;
4 use giterated_models::settings::AnySetting;
5 use giterated_models::user::User;
6
7 use sqlx::PgPool;
8
9 use super::MetadataBackend;
10
11 pub struct DatabaseSettings {
12 pub pg_pool: PgPool,
13 }
14
15 #[async_trait::async_trait]
16 impl MetadataBackend for DatabaseSettings {
17 async fn user_get(&mut self, user: &User, name: &str) -> Result<AnySetting, Error> {
18 let row = sqlx::query_as!(
19 UserSettingRow,
20 "SELECT * FROM user_settings WHERE username = $1 AND name = $2",
21 user.username,
22 name
23 )
24 .fetch_one(&self.pg_pool)
25 .await?;
26
27 let setting = serde_json::from_str(&row.value)?;
28
29 Ok(setting)
30 }
31 async fn user_write(
32 &mut self,
33 user: &User,
34 name: &str,
35 value: AnySetting,
36 ) -> Result<(), Error> {
37 sqlx::query!("INSERT INTO user_settings VALUES ($1, $2, $3) ON CONFLICT (username, name) DO UPDATE SET value = $3",
38 user.username, name, serde_json::to_string(&value)?)
39 .execute(&self.pg_pool).await?;
40
41 Ok(())
42 }
43
44 async fn repository_get(
45 &mut self,
46 repository: &Repository,
47 name: &str,
48 ) -> Result<AnySetting, Error> {
49 let row = sqlx::query_as!(
50 RepositorySettingRow,
51 "SELECT * FROM repository_settings WHERE repository = $1 AND name = $2",
52 repository.to_string(),
53 name
54 )
55 .fetch_one(&self.pg_pool)
56 .await?;
57
58 let setting = serde_json::from_str(&row.value)?;
59
60 Ok(setting)
61 }
62 async fn repository_write(
63 &mut self,
64 repository: &Repository,
65 name: &str,
66 value: AnySetting,
67 ) -> Result<(), Error> {
68 sqlx::query!("INSERT INTO repository_settings VALUES ($1, $2, $3) ON CONFLICT (repository, name) DO UPDATE SET value = $3",
69 format!("{}/{}", repository.owner, repository.name), name, serde_json::to_string(&value)?)
70 .execute(&self.pg_pool).await?;
71
72 Ok(())
73 }
74 }
75
76 #[allow(unused)]
77 #[derive(Debug, sqlx::FromRow)]
78 struct UserSettingRow {
79 pub username: String,
80 pub name: String,
81 pub value: String,
82 }
83
84 #[allow(unused)]
85 #[derive(Debug, sqlx::FromRow)]
86 struct RepositorySettingRow {
87 pub repository: String,
88 pub name: String,
89 pub value: String,
90 }
91