add: app data rename method
This commit is contained in:
parent
35b66c94d0
commit
6f2d556c65
10 changed files with 100 additions and 12 deletions
|
@ -131,7 +131,7 @@ async fn main() {
|
|||
let client = Client::new();
|
||||
let mut app = Router::new();
|
||||
|
||||
// cretae stripe client
|
||||
// create stripe client
|
||||
let stripe_client = if let Some(ref stripe) = config.stripe {
|
||||
Some(StripeClient::new(stripe.secret.clone()))
|
||||
} else {
|
||||
|
|
|
@ -140,6 +140,29 @@ export default function tetratto({
|
|||
);
|
||||
}
|
||||
|
||||
async function rename(id, key) {
|
||||
if (!api_key) {
|
||||
throw Error("No API key provided.");
|
||||
}
|
||||
|
||||
return api_promise(
|
||||
json_parse(
|
||||
await (
|
||||
await fetch(`${host}/api/v1/app_data/${id}/key`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Atto-Secret-Key": api_key,
|
||||
},
|
||||
body: json_stringify({
|
||||
key,
|
||||
}),
|
||||
})
|
||||
).text(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async function remove(id) {
|
||||
if (!api_key) {
|
||||
throw Error("No API key provided.");
|
||||
|
@ -265,6 +288,7 @@ export default function tetratto({
|
|||
query,
|
||||
insert,
|
||||
update,
|
||||
rename,
|
||||
remove,
|
||||
remove_query,
|
||||
// user connection
|
||||
|
|
|
@ -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