add: app_data table
This commit is contained in:
parent
f802a1c8ab
commit
5c520f4308
6 changed files with 249 additions and 0 deletions
163
crates/core/src/database/app_data.rs
Normal file
163
crates/core/src/database/app_data.rs
Normal file
|
@ -0,0 +1,163 @@
|
|||
use oiseau::cache::Cache;
|
||||
use crate::model::{apps::AppData, auth::User, permissions::FinePermission, Error, Result};
|
||||
use crate::{auto_method, DataManager};
|
||||
|
||||
use oiseau::PostgresRow;
|
||||
|
||||
use oiseau::{execute, get, query_rows, params};
|
||||
|
||||
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)),
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_app_data_by_id(usize as i64)@get_app_data_from_row -> "SELECT * FROM app_data WHERE id = $1" --name="app_data" --returns=AppData --cache-key-tmpl="atto.app_data:{}");
|
||||
|
||||
/// Get all app_data by app.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the app to fetch app_data for
|
||||
pub async fn get_app_data_by_app(&self, id: usize) -> Result<Vec<AppData>> {
|
||||
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 app = $1 ORDER BY created DESC",
|
||||
&[&(id as i64)],
|
||||
|x| { Self::get_app_data_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("app_data".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Get all app_data by owner.
|
||||
///
|
||||
/// # 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>> {
|
||||
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) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("app_data".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
const MAXIMUM_FREE_APP_DATA: usize = 5;
|
||||
const MAXIMUM_DATA_SIZE: usize = 205_000;
|
||||
|
||||
/// Create a new app_data in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`AppData`] object to insert
|
||||
pub async fn create_app_data(&self, data: AppData) -> Result<AppData> {
|
||||
let app = self.get_app_by_id(data.app).await?;
|
||||
|
||||
// check values
|
||||
if data.key.len() < 2 {
|
||||
return Err(Error::DataTooShort("key".to_string()));
|
||||
} else if data.key.len() > 32 {
|
||||
return Err(Error::DataTooLong("key".to_string()));
|
||||
}
|
||||
|
||||
if data.value.len() < 2 {
|
||||
return Err(Error::DataTooShort("key".to_string()));
|
||||
} else if data.value.len() > Self::MAXIMUM_DATA_SIZE {
|
||||
return Err(Error::DataTooLong("key".to_string()));
|
||||
}
|
||||
|
||||
// check number of app_data
|
||||
let owner = self.get_user_by_id(app.owner).await?;
|
||||
|
||||
if !owner.permissions.check(FinePermission::SUPPORTER) {
|
||||
let app_data = self
|
||||
.get_table_row_count_where("app_data", &format!("app = {}", data.app))
|
||||
.await? as usize;
|
||||
|
||||
if app_data >= Self::MAXIMUM_FREE_APP_DATA {
|
||||
return Err(Error::MiscError(
|
||||
"You already have the maximum number of app_data you can have".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO app_data VALUES ($1, $2, $3, $4, $5)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.owner as i64),
|
||||
&(data.app as i64),
|
||||
&data.key,
|
||||
&data.value
|
||||
]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(&conn, "DELETE FROM app_data WHERE id = $1", &[&(id as i64)]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.0.1.remove(format!("atto.app_data:{}", id)).await;
|
||||
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:{}");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue