add: journals/notes database interfaces
This commit is contained in:
parent
0f48a46c40
commit
102ea0ee35
14 changed files with 386 additions and 6 deletions
141
crates/core/src/database/journals.rs
Normal file
141
crates/core/src/database/journals.rs
Normal file
|
@ -0,0 +1,141 @@
|
|||
use oiseau::cache::Cache;
|
||||
use crate::{
|
||||
model::{
|
||||
auth::User,
|
||||
permissions::FinePermission,
|
||||
journals::{Journal, JournalViewPermission},
|
||||
Error, Result,
|
||||
},
|
||||
};
|
||||
use crate::{auto_method, DataManager};
|
||||
use oiseau::{PostgresRow, execute, get, query_rows, params};
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`Journal`] from an SQL row.
|
||||
pub(crate) fn get_journal_from_row(x: &PostgresRow) -> Journal {
|
||||
Journal {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
owner: get!(x->2(i64)) as usize,
|
||||
title: get!(x->3(String)),
|
||||
view: serde_json::from_str(&get!(x->4(String))).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_journal_by_id(usize as i64)@get_journal_from_row -> "SELECT * FROM journals WHERE id = $1" --name="journal" --returns=Journal --cache-key-tmpl="atto.journal:{}");
|
||||
|
||||
/// Get all journals by user.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the user to fetch journals for
|
||||
pub async fn get_journals_by_user(&self, id: usize) -> Result<Vec<Journal>> {
|
||||
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 journals WHERE owner = $1 ORDER BY name ASC",
|
||||
&[&(id as i64)],
|
||||
|x| { Self::get_journal_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("journal".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
const MAXIMUM_FREE_JOURNALS: usize = 15;
|
||||
|
||||
/// Create a new journal in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`Journal`] object to insert
|
||||
pub async fn create_journal(&self, data: Journal) -> Result<Journal> {
|
||||
// check values
|
||||
if data.title.len() < 2 {
|
||||
return Err(Error::DataTooShort("title".to_string()));
|
||||
} else if data.title.len() > 32 {
|
||||
return Err(Error::DataTooLong("title".to_string()));
|
||||
}
|
||||
|
||||
// check number of journals
|
||||
let owner = self.get_user_by_id(data.owner).await?;
|
||||
|
||||
if !owner.permissions.check(FinePermission::SUPPORTER) {
|
||||
let journals = self.get_journals_by_user(data.owner).await?;
|
||||
|
||||
if journals.len() >= Self::MAXIMUM_FREE_JOURNALS {
|
||||
return Err(Error::MiscError(
|
||||
"You already have the maximum number of journals 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 journals VALUES ($1, $2, $3, $4, $5)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
&(data.owner as i64),
|
||||
&data.title,
|
||||
&serde_json::to_string(&data.view).unwrap(),
|
||||
]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub async fn delete_journal(&self, id: usize, user: &User) -> Result<()> {
|
||||
let journal = self.get_journal_by_id(id).await?;
|
||||
|
||||
// check user permission
|
||||
if user.id != journal.owner && !user.permissions.check(FinePermission::MANAGE_JOURNALS) {
|
||||
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 journals WHERE id = $1", &[&(id as i64)]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// delete notes
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"DELETE FROM notes WHERE journal = $1",
|
||||
&[&(id as i64)]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// ...
|
||||
self.0.1.remove(format!("atto.journal:{}", id)).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(update_journal_title(&str)@get_journal_by_id:MANAGE_JOURNALS -> "UPDATE journals SET title = $1 WHERE id = $2" --cache-key-tmpl="atto.journal:{}");
|
||||
auto_method!(update_journal_view(JournalViewPermission)@get_journal_by_id:MANAGE_JOURNALS -> "UPDATE journals SET privacy = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.journal:{}");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue