feat(router): added api for the deleting config key (#3554)

Co-authored-by: Kartikeya Hegde <karthihegde010@gmail.com>
Co-authored-by: dracarys18 <karthikey.hegde@juspay.in>
This commit is contained in:
Tanbir Ali
2024-02-22 16:55:10 +05:30
committed by GitHub
parent 863e380cf2
commit bbb3d3d1e2
8 changed files with 58 additions and 12 deletions

View File

@ -50,8 +50,11 @@ impl Config {
}
#[instrument(skip(conn))]
pub async fn delete_by_key(conn: &PgPooledConn, key: &str) -> StorageResult<bool> {
generics::generic_delete::<<Self as HasTable>::Table, _>(conn, dsl::key.eq(key.to_owned()))
.await
pub async fn delete_by_key(conn: &PgPooledConn, key: &str) -> StorageResult<Self> {
generics::generic_delete_one_with_result::<<Self as HasTable>::Table, _, _>(
conn,
dsl::key.eq(key.to_owned()),
)
.await
}
}

View File

@ -41,3 +41,12 @@ pub async fn update_config(
.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()))
}

View File

@ -51,7 +51,10 @@ pub trait ConfigInterface {
config_update: storage::ConfigUpdate,
) -> CustomResult<storage::Config, errors::StorageError>;
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError>;
async fn delete_config_by_key(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError>;
}
#[async_trait::async_trait]
@ -154,7 +157,10 @@ impl ConfigInterface for Store {
cache::get_or_populate_in_memory(self, key, find_else_unwrap_or, &CONFIG_CACHE).await
}
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError> {
async fn delete_config_by_key(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
let deleted = storage::Config::delete_by_key(&conn, key)
.await
@ -226,15 +232,15 @@ impl ConfigInterface for MockDb {
result
}
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError> {
async fn delete_config_by_key(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError> {
let mut configs = self.configs.lock().await;
let result = configs
.iter()
.position(|c| c.key == key)
.map(|index| {
configs.remove(index);
true
})
.map(|index| configs.remove(index))
.ok_or_else(|| {
errors::StorageError::ValueNotFound("cannot find config to delete".to_string())
.into()

View File

@ -284,7 +284,10 @@ impl ConfigInterface for KafkaStore {
.await
}
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError> {
async fn delete_config_by_key(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError> {
self.diesel_store.delete_config_by_key(key).await
}

View File

@ -813,7 +813,8 @@ impl Configs {
.service(
web::resource("/{key}")
.route(web::get().to(config_key_retrieve))
.route(web::post().to(config_key_update)),
.route(web::post().to(config_key_update))
.route(web::delete().to(config_key_delete)),
)
}
}

View File

@ -71,3 +71,24 @@ pub async fn config_key_update(
)
.await
}
#[instrument(skip_all, fields(flow = ?Flow::ConfigKeyDelete))]
pub async fn config_key_delete(
state: web::Data<AppState>,
req: HttpRequest,
path: web::Path<String>,
) -> impl Responder {
let flow = Flow::ConfigKeyDelete;
let key = path.into_inner();
api::server_wrap(
flow,
state,
&req,
key,
|state, _, key| configs::config_delete(state, key),
&auth::AdminApiAuth,
api_locking::LockAction::NotApplicable,
)
.await
}

View File

@ -74,6 +74,7 @@ impl From<Flow> for ApiIdentifier {
Flow::ConfigKeyCreate
| Flow::ConfigKeyFetch
| Flow::ConfigKeyUpdate
| Flow::ConfigKeyDelete
| Flow::CreateConfigKey => Self::Configs,
Flow::CustomersCreate

View File

@ -82,6 +82,8 @@ pub enum Flow {
ConfigKeyFetch,
/// ConfigKey Update flow.
ConfigKeyUpdate,
/// ConfigKey Delete flow.
ConfigKeyDelete,
/// Customers create flow.
CustomersCreate,
/// Customers retrieve flow.