feat: add get_setting_with_prefix method to SettingsService implementation

This commit is contained in:
2026-02-20 10:01:43 +00:00
parent 8f18b8692f
commit 716223e45c

View File

@@ -3,7 +3,9 @@ use std::collections::HashMap;
use async_trait::async_trait;
use chrono::Utc;
use log::warn;
use sea_orm::{ActiveModelTrait, ActiveValue::Set, DatabaseConnection, EntityTrait};
use sea_orm::{
ActiveModelTrait, ActiveValue::Set, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter,
};
use struct_iterable::Iterable;
use crate::{
@@ -20,6 +22,7 @@ pub type SettingModel = settings::Model;
#[async_trait]
pub trait SettingsService: Send + Sync {
async fn get_settings(&self, tx: Option<&ConnectionSource<'_>>) -> CommandResult<Settings>;
async fn get_setting_with_prefix(&self, prefix: &str) -> HashMap<String, String>;
async fn get_setting(&self, key: &str, default: &str) -> String;
async fn update_setting(
&self,
@@ -70,6 +73,20 @@ impl SettingsService for SettingsServiceImpl {
Ok(Settings::from(map))
}
async fn get_setting_with_prefix(&self, prefix: &str) -> HashMap<String, String> {
let settings_list = crate::db::entities::settings::Entity::find()
.filter(settings::Column::Key.starts_with(prefix.to_string()))
.all(&self.db)
.await
.unwrap_or_default();
let mut map = HashMap::new();
for setting in settings_list {
map.insert(setting.key, setting.value.unwrap_or_default());
}
map
}
async fn get_setting(&self, key: &str, default: &str) -> String {
crate::db::entities::settings::Entity::find_by_id(key.to_string())
.one(&self.db)