add: app data rename method
This commit is contained in:
parent
35b66c94d0
commit
6f2d556c65
10 changed files with 100 additions and 12 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -3269,7 +3269,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tetratto"
|
name = "tetratto"
|
||||||
version = "11.0.0"
|
version = "12.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ammonia",
|
"ammonia",
|
||||||
"async-stripe",
|
"async-stripe",
|
||||||
|
@ -3301,7 +3301,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tetratto-core"
|
name = "tetratto-core"
|
||||||
version = "11.0.0"
|
version = "12.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-recursion",
|
"async-recursion",
|
||||||
"base16ct",
|
"base16ct",
|
||||||
|
@ -3325,7 +3325,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tetratto-l10n"
|
name = "tetratto-l10n"
|
||||||
version = "11.0.0"
|
version = "12.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"pathbufd",
|
"pathbufd",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -3334,7 +3334,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tetratto-shared"
|
name = "tetratto-shared"
|
||||||
version = "11.0.0"
|
version = "12.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ammonia",
|
"ammonia",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tetratto"
|
name = "tetratto"
|
||||||
version = "11.0.0"
|
version = "12.0.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
|
|
@ -131,7 +131,7 @@ async fn main() {
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
let mut app = Router::new();
|
let mut app = Router::new();
|
||||||
|
|
||||||
// cretae stripe client
|
// create stripe client
|
||||||
let stripe_client = if let Some(ref stripe) = config.stripe {
|
let stripe_client = if let Some(ref stripe) = config.stripe {
|
||||||
Some(StripeClient::new(stripe.secret.clone()))
|
Some(StripeClient::new(stripe.secret.clone()))
|
||||||
} else {
|
} 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) {
|
async function remove(id) {
|
||||||
if (!api_key) {
|
if (!api_key) {
|
||||||
throw Error("No API key provided.");
|
throw Error("No API key provided.");
|
||||||
|
@ -265,6 +288,7 @@ export default function tetratto({
|
||||||
query,
|
query,
|
||||||
insert,
|
insert,
|
||||||
update,
|
update,
|
||||||
|
rename,
|
||||||
remove,
|
remove,
|
||||||
remove_query,
|
remove_query,
|
||||||
// user connection
|
// user connection
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
get_app_from_key,
|
get_app_from_key,
|
||||||
routes::api::v1::{InsertAppData, QueryAppData, UpdateAppDataValue},
|
routes::api::v1::{InsertAppData, QueryAppData, UpdateAppDataValue, UpdateAppDataKey},
|
||||||
State,
|
State,
|
||||||
};
|
};
|
||||||
use axum::{extract::Path, http::HeaderMap, response::IntoResponse, Extension, Json};
|
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(
|
pub async fn update_value_request(
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
Extension(data): Extension<State>,
|
Extension(data): Extension<State>,
|
||||||
|
@ -116,6 +147,10 @@ pub async fn update_value_request(
|
||||||
Err(e) => return Json(e.into()),
|
Err(e) => return Json(e.into()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if app_data.app != app.id {
|
||||||
|
return Json(Error::NotAllowed.into());
|
||||||
|
}
|
||||||
|
|
||||||
// check size
|
// check size
|
||||||
let size_without = app.data_used - app_data.value.len();
|
let size_without = app.data_used - app_data.value.len();
|
||||||
let new_size = size_without + req.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()),
|
Err(e) => return Json(e.into()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if app_data.app != app.id {
|
||||||
|
return Json(Error::NotAllowed.into());
|
||||||
|
}
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
if let Err(e) = data
|
if let Err(e) = data
|
||||||
.add_app_data_used(app.id, -(app_data.value.len() as i32))
|
.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", post(app_data::create_request))
|
||||||
.route("/app_data/app", get(app_data::get_app_request))
|
.route("/app_data/app", get(app_data::get_app_request))
|
||||||
.route("/app_data/{id}", delete(app_data::delete_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/{id}/value", post(app_data::update_value_request))
|
||||||
.route("/app_data/query", post(app_data::query_request))
|
.route("/app_data/query", post(app_data::query_request))
|
||||||
.route("/app_data/query", delete(app_data::delete_query_request))
|
.route("/app_data/query", delete(app_data::delete_query_request))
|
||||||
|
@ -1176,6 +1177,11 @@ pub struct UpdateUploadAlt {
|
||||||
pub alt: String,
|
pub alt: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct UpdateAppDataKey {
|
||||||
|
pub key: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct UpdateAppDataValue {
|
pub struct UpdateAppDataValue {
|
||||||
pub value: String,
|
pub value: String,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tetratto-core"
|
name = "tetratto-core"
|
||||||
description = "The core behind Tetratto"
|
description = "The core behind Tetratto"
|
||||||
version = "11.0.0"
|
version = "12.0.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
@ -18,8 +18,8 @@ default = ["database", "types", "sdk"]
|
||||||
pathbufd = "0.1.4"
|
pathbufd = "0.1.4"
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
toml = "0.9.2"
|
toml = "0.9.2"
|
||||||
tetratto-shared = { version = "11.0.0", path = "../shared" }
|
tetratto-shared = { version = "12.0.0", path = "../shared" }
|
||||||
tetratto-l10n = { version = "11.0.0", path = "../l10n" }
|
tetratto-l10n = { version = "12.0.0", path = "../l10n" }
|
||||||
serde_json = "1.0.141"
|
serde_json = "1.0.141"
|
||||||
totp-rs = { version = "5.7.0", features = ["qr", "gen_secret"], optional = true }
|
totp-rs = { version = "5.7.0", features = ["qr", "gen_secret"], optional = true }
|
||||||
reqwest = { version = "0.12.22", features = ["json", "multipart"], optional = true }
|
reqwest = { version = "0.12.22", features = ["json", "multipart"], optional = true }
|
||||||
|
|
|
@ -129,6 +129,25 @@ impl DataClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update a record's key given its ID and the new key.
|
||||||
|
pub async fn rename(&self, id: usize, key: String) -> Result<()> {
|
||||||
|
match self
|
||||||
|
.http
|
||||||
|
.post(format!("{}/api/v1/app_data/{id}/key", self.host))
|
||||||
|
.header("Atto-Secret-Key", &self.api_key)
|
||||||
|
.json(&serde_json::Value::Object({
|
||||||
|
let mut map = serde_json::Map::new();
|
||||||
|
map.insert("key".to_string(), serde_json::Value::String(key));
|
||||||
|
map
|
||||||
|
}))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(x) => api_return_ok!((), x),
|
||||||
|
Err(e) => Err(Error::MiscError(e.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Delete a row from the app's data by its `id`.
|
/// Delete a row from the app's data by its `id`.
|
||||||
pub async fn remove(&self, id: usize) -> Result<()> {
|
pub async fn remove(&self, id: usize) -> Result<()> {
|
||||||
match self
|
match self
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tetratto-l10n"
|
name = "tetratto-l10n"
|
||||||
description = "Localization for Tetratto"
|
description = "Localization for Tetratto"
|
||||||
version = "11.0.0"
|
version = "12.0.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tetratto-shared"
|
name = "tetratto-shared"
|
||||||
description = "Shared stuff for Tetratto"
|
description = "Shared stuff for Tetratto"
|
||||||
version = "11.0.0"
|
version = "12.0.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue