add: app_data api
This commit is contained in:
parent
5c520f4308
commit
f423daf2fc
38 changed files with 410 additions and 91 deletions
|
@ -1,17 +1,17 @@
|
|||
use oiseau::cache::Cache;
|
||||
use crate::model::{apps::AppData, auth::User, permissions::FinePermission, Error, Result};
|
||||
use crate::model::apps::{AppDataQuery, AppDataQueryResult, AppDataSelectMode, AppDataSelectQuery};
|
||||
use crate::model::{apps::AppData, permissions::FinePermission, Error, Result};
|
||||
use crate::{auto_method, DataManager};
|
||||
use oiseau::{PostgresRow, execute, get, query_row, query_rows, params};
|
||||
|
||||
use oiseau::PostgresRow;
|
||||
|
||||
use oiseau::{execute, get, query_rows, params};
|
||||
pub const FREE_DATA_LIMIT: usize = 512_000;
|
||||
pub const PASS_DATA_LIMIT: usize = 5_242_880;
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`AppData`] from an SQL row.
|
||||
pub(crate) fn get_app_data_from_row(x: &PostgresRow) -> AppData {
|
||||
AppData {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
owner: get!(x->1(i64)) as usize,
|
||||
app: get!(x->2(i64)) as usize,
|
||||
key: get!(x->3(String)),
|
||||
value: get!(x->4(String)),
|
||||
|
@ -48,24 +48,39 @@ impl DataManager {
|
|||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the user to fetch app_data for
|
||||
pub async fn get_app_data_by_owner(&self, id: usize) -> Result<Vec<AppData>> {
|
||||
pub async fn query_app_data(&self, query: AppDataQuery) -> Result<AppDataQueryResult> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM app_data WHERE owner = $1 ORDER BY created DESC",
|
||||
&[&(id as i64)],
|
||||
|x| { Self::get_app_data_from_row(x) }
|
||||
let query_str = query.to_string().replace(
|
||||
"%q%",
|
||||
&match query.query {
|
||||
AppDataSelectQuery::Like(_, _) => format!("v LIKE $1"),
|
||||
},
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("app_data".to_string()));
|
||||
}
|
||||
let res = match query.mode {
|
||||
AppDataSelectMode::One => AppDataQueryResult::One(
|
||||
match query_row!(&conn, &query_str, params![&query.query.to_string()], |x| {
|
||||
Ok(Self::get_app_data_from_row(x))
|
||||
}) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return Err(Error::GeneralNotFound("app_data".to_string())),
|
||||
},
|
||||
),
|
||||
AppDataSelectMode::Many(_, _, _) => AppDataQueryResult::Many(
|
||||
match query_rows!(&conn, &query_str, params![&query.query.to_string()], |x| {
|
||||
Self::get_app_data_from_row(x)
|
||||
}) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return Err(Error::GeneralNotFound("app_data".to_string())),
|
||||
},
|
||||
),
|
||||
};
|
||||
|
||||
Ok(res.unwrap())
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
const MAXIMUM_FREE_APP_DATA: usize = 5;
|
||||
|
@ -114,10 +129,9 @@ impl DataManager {
|
|||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO app_data VALUES ($1, $2, $3, $4, $5)",
|
||||
"INSERT INTO app_data VALUES ($1, $2, $3, $4)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.owner as i64),
|
||||
&(data.app as i64),
|
||||
&data.key,
|
||||
&data.value
|
||||
|
@ -131,18 +145,7 @@ impl DataManager {
|
|||
Ok(data)
|
||||
}
|
||||
|
||||
pub async fn delete_app_data(&self, id: usize, user: &User) -> Result<()> {
|
||||
let app_data = self.get_app_data_by_id(id).await?;
|
||||
let app = self.get_app_by_id(app_data.app).await?;
|
||||
|
||||
// check user permission
|
||||
if ((user.id != app.owner) | (user.id != app_data.owner))
|
||||
&& !user.permissions.check(FinePermission::MANAGE_APPS)
|
||||
{
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
|
||||
// ...
|
||||
pub async fn delete_app_data(&self, id: usize) -> Result<()> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
|
@ -158,6 +161,6 @@ impl DataManager {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(update_app_data_key(&str)@get_app_data_by_id:FinePermission::MANAGE_APPS; -> "UPDATE app_data SET k = $1 WHERE id = $2" --cache-key-tmpl="atto.app_data:{}");
|
||||
auto_method!(update_app_data_value(&str)@get_app_data_by_id:FinePermission::MANAGE_APPS; -> "UPDATE app_data SET v = $1 WHERE id = $2" --cache-key-tmpl="atto.app_data:{}");
|
||||
auto_method!(update_app_data_key(&str) -> "UPDATE app_data SET k = $1 WHERE id = $2" --cache-key-tmpl="atto.app_data:{}");
|
||||
auto_method!(update_app_data_value(&str) -> "UPDATE app_data SET v = $1 WHERE id = $2" --cache-key-tmpl="atto.app_data:{}");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue