add: app data rename method
This commit is contained in:
parent
35b66c94d0
commit
6f2d556c65
10 changed files with 100 additions and 12 deletions
|
@ -1,6 +1,6 @@
|
|||
use crate::{
|
||||
get_app_from_key,
|
||||
routes::api::v1::{InsertAppData, QueryAppData, UpdateAppDataValue},
|
||||
routes::api::v1::{InsertAppData, QueryAppData, UpdateAppDataValue, UpdateAppDataKey},
|
||||
State,
|
||||
};
|
||||
use axum::{extract::Path, http::HeaderMap, response::IntoResponse, Extension, Json};
|
||||
|
@ -94,6 +94,37 @@ pub async fn create_request(
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn update_key_request(
|
||||
headers: HeaderMap,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdateAppDataKey>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let app = match get_app_from_key!(data, headers) {
|
||||
Some(x) => x,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let app_data = match data.get_app_data_by_id(id).await {
|
||||
Ok(x) => x,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
if app_data.app != app.id {
|
||||
return Json(Error::NotAllowed.into());
|
||||
}
|
||||
|
||||
match data.update_app_data_key(id, &req.key).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Data updated".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_value_request(
|
||||
headers: HeaderMap,
|
||||
Extension(data): Extension<State>,
|
||||
|
@ -116,6 +147,10 @@ pub async fn update_value_request(
|
|||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
if app_data.app != app.id {
|
||||
return Json(Error::NotAllowed.into());
|
||||
}
|
||||
|
||||
// check size
|
||||
let size_without = app.data_used - app_data.value.len();
|
||||
let new_size = size_without + req.value.len();
|
||||
|
@ -155,6 +190,10 @@ pub async fn delete_request(
|
|||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
if app_data.app != app.id {
|
||||
return Json(Error::NotAllowed.into());
|
||||
}
|
||||
|
||||
// ...
|
||||
if let Err(e) = data
|
||||
.add_app_data_used(app.id, -(app_data.value.len() as i32))
|
||||
|
|
|
@ -439,6 +439,7 @@ pub fn routes() -> Router {
|
|||
.route("/app_data", post(app_data::create_request))
|
||||
.route("/app_data/app", get(app_data::get_app_request))
|
||||
.route("/app_data/{id}", delete(app_data::delete_request))
|
||||
.route("/app_data/{id}/key", post(app_data::update_key_request))
|
||||
.route("/app_data/{id}/value", post(app_data::update_value_request))
|
||||
.route("/app_data/query", post(app_data::query_request))
|
||||
.route("/app_data/query", delete(app_data::delete_query_request))
|
||||
|
@ -1176,6 +1177,11 @@ pub struct UpdateUploadAlt {
|
|||
pub alt: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateAppDataKey {
|
||||
pub key: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateAppDataValue {
|
||||
pub value: String,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue