bridge_common/repo/
settings.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use chrono::Utc;
use serde_json::Value;
use sqlx::{query, query_as, Executor, Postgres};

use crate::{settings::Settings, types::Result};

struct SettingsRow {
    value: Value,
}

/// Get settings for a company.
///
/// # Errors
///
/// Returns error if there was a problem while fetching settings.
pub async fn get<'a, E>(executor: E, company_id: i32) -> Result<Settings>
where
    E: Executor<'a, Database = Postgres> + std::marker::Copy,
{
    match query_as!(
        SettingsRow,
        "SELECT value FROM settings WHERE company_id = $1 LIMIT 1",
        company_id,
    )
    .fetch_optional(executor)
    .await?
    {
        Some(row) => Ok(Settings::try_from(row.value)?),
        None => {
            let settings = Settings::default();
            insert(executor, company_id, &settings).await?;

            Ok(settings)
        }
    }
}

/// Insert settings for a company.
///
/// # Errors
///
/// Returns error if there was a problem while inserting settings.
pub async fn insert<'a, E>(executor: E, company_id: i32, settings: &Settings) -> Result<()>
where
    E: Executor<'a, Database = Postgres>,
{
    query!(
        "INSERT INTO settings (company_id, value, created_at, updated_at) VALUES ($1, $2, $3, $4)",
        company_id,
        serde_json::to_value(settings)?,
        Utc::now(),
        Utc::now(),
    )
    .execute(executor)
    .await?;

    Ok(())
}

/// Update settings for a company.
///
/// # Errors
///
/// Returns error if there was a problem while updating settings.
pub async fn update<'a, E>(executor: E, company_id: i32, settings: &Settings) -> Result<()>
where
    E: Executor<'a, Database = Postgres>,
{
    query!(
        "UPDATE settings SET value = $1, updated_at = $2 WHERE company_id = $3",
        serde_json::to_value(settings)?,
        Utc::now(),
        company_id,
    )
    .execute(executor)
    .await?;

    Ok(())
}