cat_gateway/db/event/config/
mod.rs

1//! Configuration query
2
3use key::ConfigKey;
4use serde_json::Value;
5
6use crate::db::event::EventDB;
7
8pub(crate) mod key;
9
10/// Configuration struct
11pub(crate) struct Config {}
12
13/// SQL get configuration.
14const GET_CONFIG: &str = include_str!("sql/get.sql");
15/// SQL update if exist or else insert configuration.
16const UPSERT_CONFIG: &str = include_str!("sql/upsert.sql");
17
18impl Config {
19    /// Retrieve configuration based on the given `ConfigKey`.
20    ///
21    /// # Returns
22    ///
23    /// - A JSON value of the configuration, if not found or error, returns the default
24    ///   value.
25    /// - Error if the query fails.
26    pub(crate) async fn get(id: ConfigKey) -> anyhow::Result<Value> {
27        let (id1, id2, id3) = id.to_id();
28        let row = EventDB::query_one(GET_CONFIG, &[&id1, &id2, &id3]).await?;
29
30        let value = row.get(0);
31        Ok(value)
32    }
33
34    /// Set the configuration for the given `ConfigKey`.
35    ///
36    /// # Returns
37    ///
38    /// - A `BasicOutput` of the validation result, which can be valid or invalid.
39    /// - Error if the query fails.
40    pub(crate) async fn set(
41        id: ConfigKey,
42        value: Value,
43    ) -> anyhow::Result<()> {
44        let (id1, id2, id3) = id.to_id();
45        EventDB::query(UPSERT_CONFIG, &[&id1, &id2, &id3, &value]).await?;
46        Ok(())
47    }
48}