134 lines
4.6 KiB
Rust
134 lines
4.6 KiB
Rust
use crate::model::{
|
|
auth::User,
|
|
littleweb::{Service, ServiceFsEntry},
|
|
permissions::{FinePermission, SecondaryPermission},
|
|
Error, Result,
|
|
};
|
|
use crate::{auto_method, DataManager};
|
|
use oiseau::{cache::Cache, execute, get, params, query_rows, PostgresRow};
|
|
|
|
impl DataManager {
|
|
/// Get a [`Service`] from an SQL row.
|
|
pub(crate) fn get_service_from_row(x: &PostgresRow) -> Service {
|
|
Service {
|
|
id: get!(x->0(i64)) as usize,
|
|
created: get!(x->1(i64)) as usize,
|
|
owner: get!(x->2(i64)) as usize,
|
|
name: get!(x->3(String)),
|
|
files: serde_json::from_str(&get!(x->4(String))).unwrap(),
|
|
revision: get!(x->5(i64)) as usize,
|
|
}
|
|
}
|
|
|
|
auto_method!(get_service_by_id(usize as i64)@get_service_from_row -> "SELECT * FROM services WHERE id = $1" --name="service" --returns=Service --cache-key-tmpl="atto.service:{}");
|
|
|
|
/// Get all services by user.
|
|
///
|
|
/// # Arguments
|
|
/// * `id` - the ID of the user to fetch services for
|
|
pub async fn get_services_by_user(&self, id: usize) -> Result<Vec<Service>> {
|
|
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 services WHERE owner = $1 ORDER BY created DESC",
|
|
&[&(id as i64)],
|
|
|x| { Self::get_service_from_row(x) }
|
|
);
|
|
|
|
if res.is_err() {
|
|
return Err(Error::GeneralNotFound("service".to_string()));
|
|
}
|
|
|
|
Ok(res.unwrap())
|
|
}
|
|
|
|
const MAXIMUM_FREE_SERVICES: usize = 10;
|
|
|
|
/// Create a new service in the database.
|
|
///
|
|
/// # Arguments
|
|
/// * `data` - a mock [`Service`] object to insert
|
|
pub async fn create_service(&self, data: Service) -> Result<Service> {
|
|
// check values
|
|
if data.name.trim().len() < 2 {
|
|
return Err(Error::DataTooShort("name".to_string()));
|
|
} else if data.name.len() > 128 {
|
|
return Err(Error::DataTooLong("name".to_string()));
|
|
}
|
|
|
|
// check number of services
|
|
let owner = self.get_user_by_id(data.owner).await?;
|
|
|
|
if !owner.permissions.check(FinePermission::SUPPORTER) {
|
|
let services = self.get_services_by_user(data.owner).await?;
|
|
|
|
if services.len() >= Self::MAXIMUM_FREE_SERVICES {
|
|
return Err(Error::MiscError(
|
|
"You already have the maximum number of services 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 services VALUES ($1, $2, $3, $4, $5, $6)",
|
|
params![
|
|
&(data.id as i64),
|
|
&(data.created as i64),
|
|
&(data.owner as i64),
|
|
&data.name,
|
|
&serde_json::to_string(&data.files).unwrap(),
|
|
&(data.created as i64)
|
|
]
|
|
);
|
|
|
|
if let Err(e) = res {
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
}
|
|
|
|
Ok(data)
|
|
}
|
|
|
|
pub async fn delete_service(&self, id: usize, user: &User) -> Result<()> {
|
|
let service = self.get_service_by_id(id).await?;
|
|
|
|
// check user permission
|
|
if user.id != service.owner
|
|
&& !user
|
|
.secondary_permissions
|
|
.check(SecondaryPermission::MANAGE_SERVICES)
|
|
{
|
|
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 services WHERE id = $1", &[&(id as i64)]);
|
|
|
|
if let Err(e) = res {
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
}
|
|
|
|
// ...
|
|
self.0.1.remove(format!("atto.service:{}", id)).await;
|
|
Ok(())
|
|
}
|
|
|
|
auto_method!(update_service_name(&str)@get_service_by_id:FinePermission::MANAGE_USERS; -> "UPDATE services SET name = $1 WHERE id = $2" --cache-key-tmpl="atto.service:{}");
|
|
auto_method!(update_service_files(Vec<ServiceFsEntry>)@get_service_by_id:FinePermission::MANAGE_USERS; -> "UPDATE services SET files = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.service:{}");
|
|
auto_method!(update_service_revision(i64) -> "UPDATE services SET revision = $1 WHERE id = $2" --cache-key-tmpl="atto.service:{}");
|
|
}
|