-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
configs.rs
52 lines (46 loc) · 1.76 KB
/
configs.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
use error_stack::ResultExt;
use crate::{
core::errors::{self, utils::StorageErrorExt, RouterResponse},
routes::AppState,
services::ApplicationResponse,
types::{api, transformers::ForeignInto},
};
pub async fn set_config(state: AppState, config: api::Config) -> RouterResponse<api::Config> {
let store = state.store.as_ref();
let config = store
.insert_config(diesel_models::configs::ConfigNew {
key: config.key,
config: config.value,
})
.await
.to_duplicate_response(errors::ApiErrorResponse::DuplicateConfig)
.attach_printable("Unknown error, while setting config key")?;
Ok(ApplicationResponse::Json(config.foreign_into()))
}
pub async fn read_config(state: AppState, key: &str) -> RouterResponse<api::Config> {
let store = state.store.as_ref();
let config = store
.find_config_by_key(key)
.await
.to_not_found_response(errors::ApiErrorResponse::ConfigNotFound)?;
Ok(ApplicationResponse::Json(config.foreign_into()))
}
pub async fn update_config(
state: AppState,
config_update: &api::ConfigUpdate,
) -> RouterResponse<api::Config> {
let store = state.store.as_ref();
let config = store
.update_config_by_key(&config_update.key, config_update.foreign_into())
.await
.to_not_found_response(errors::ApiErrorResponse::ConfigNotFound)?;
Ok(ApplicationResponse::Json(config.foreign_into()))
}
pub async fn config_delete(state: AppState, key: String) -> RouterResponse<api::Config> {
let store = state.store.as_ref();
let config = store
.delete_config_by_key(&key)
.await
.to_not_found_response(errors::ApiErrorResponse::ConfigNotFound)?;
Ok(ApplicationResponse::Json(config.foreign_into()))
}