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
//! Contains the implementation of [`ApplicationSetting`].

use diesel::pg::Pg;
use diesel::prelude::*;
use diesel::{debug_query, QueryResult};
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use log::debug;

use crate::model::dto::ApplicationSettingDto;
use crate::model::entity::ApplicationSetting;
use crate::schema::application_settings::{self, key as key_column};

impl ApplicationSetting {
    /// Fetch application setting by key from the database.
    ///
    /// # Errors
    /// * Unknown, diesel doesn't say why it might error.
    pub async fn find_by_key(
        key: &str,
        conn: &mut AsyncPgConnection,
    ) -> QueryResult<ApplicationSettingDto> {
        let query = application_settings::table.filter(key_column.eq(key));
        debug!("{}", debug_query::<Pg, _>(&query));
        query.first::<Self>(conn).await.map(Into::into)
    }
}