add: journals + notes

This commit is contained in:
trisua 2025-06-19 15:48:04 -04:00
parent c08a26ae8d
commit c1568ad866
26 changed files with 1431 additions and 265 deletions

View file

@ -1,4 +1,4 @@
use oiseau::cache::Cache;
use oiseau::{cache::Cache, query_row};
use crate::{
model::{
auth::User,
@ -24,6 +24,27 @@ impl DataManager {
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 a journal by `owner` and `title`.
pub async fn get_journal_by_owner_title(&self, owner: usize, title: &str) -> Result<Journal> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_row!(
&conn,
"SELECT * FROM journals WHERE owner = $1 AND title = $2",
params![&(owner as i64), &title],
|x| { Ok(Self::get_journal_from_row(x)) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("journal".to_string()));
}
Ok(res.unwrap())
}
/// Get all journals by user.
///
/// # Arguments
@ -54,7 +75,7 @@ impl DataManager {
///
/// # Arguments
/// * `data` - a mock [`Journal`] object to insert
pub async fn create_journal(&self, data: Journal) -> Result<Journal> {
pub async fn create_journal(&self, mut data: Journal) -> Result<Journal> {
// check values
if data.title.len() < 2 {
return Err(Error::DataTooShort("title".to_string()));
@ -62,6 +83,17 @@ impl DataManager {
return Err(Error::DataTooLong("title".to_string()));
}
data.title = data.title.replace(" ", "_");
// make sure this title isn't already in use
if self
.get_journal_by_owner_title(data.owner, &data.title)
.await
.is_ok()
{
return Err(Error::TitleInUse);
}
// check number of journals
let owner = self.get_user_by_id(data.owner).await?;

View file

@ -1,7 +1,7 @@
use oiseau::cache::Cache;
use crate::model::{auth::User, journals::Note, permissions::FinePermission, Error, Result};
use crate::{auto_method, DataManager};
use oiseau::{PostgresRow, execute, get, query_rows, params};
use oiseau::{execute, get, params, query_row, query_rows, PostgresRow};
impl DataManager {
/// Get a [`Note`] from an SQL row.
@ -19,6 +19,27 @@ impl DataManager {
auto_method!(get_note_by_id(usize as i64)@get_note_from_row -> "SELECT * FROM notes WHERE id = $1" --name="note" --returns=Note --cache-key-tmpl="atto.note:{}");
/// Get a note by `journal` and `title`.
pub async fn get_note_by_journal_title(&self, journal: usize, title: &str) -> Result<Note> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_row!(
&conn,
"SELECT * FROM notes WHERE journal = $1 AND title = $2",
params![&(journal as i64), &title],
|x| { Ok(Self::get_note_from_row(x)) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("note".to_string()));
}
Ok(res.unwrap())
}
/// Get all notes by journal.
///
/// # Arguments
@ -31,7 +52,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM notes WHERE journal = $1 ORDER BY edited",
"SELECT * FROM notes WHERE journal = $1 ORDER BY edited DESC",
&[&(id as i64)],
|x| { Self::get_note_from_row(x) }
);
@ -47,7 +68,7 @@ impl DataManager {
///
/// # Arguments
/// * `data` - a mock [`Note`] object to insert
pub async fn create_note(&self, data: Note) -> Result<Note> {
pub async fn create_note(&self, mut data: Note) -> Result<Note> {
// check values
if data.title.len() < 2 {
return Err(Error::DataTooShort("title".to_string()));
@ -61,6 +82,24 @@ impl DataManager {
return Err(Error::DataTooLong("content".to_string()));
}
data.title = data.title.replace(" ", "_");
// make sure this title isn't already in use
if self
.get_note_by_journal_title(data.journal, &data.title)
.await
.is_ok()
{
return Err(Error::TitleInUse);
}
// check permission
let journal = self.get_journal_by_id(data.journal).await?;
if data.owner != journal.owner {
return Err(Error::NotAllowed);
}
// ...
let conn = match self.0.connect().await {
Ok(c) => c,
@ -108,13 +147,6 @@ impl DataManager {
return Err(Error::DatabaseError(e.to_string()));
}
// delete notes
let res = execute!(&conn, "DELETE FROM notes WHERE note = $1", &[&(id as i64)]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
// ...
self.0.1.remove(format!("atto.note:{}", id)).await;
Ok(())
@ -122,4 +154,5 @@ impl DataManager {
auto_method!(update_note_title(&str)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET title = $1 WHERE id = $2" --cache-key-tmpl="atto.note:{}");
auto_method!(update_note_content(&str)@get_note_by_id:MANAGE_NOTES -> "UPDATE notes SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.note:{}");
auto_method!(update_note_edited(i64) -> "UPDATE notes SET edited = $1 WHERE id = $2" --cache-key-tmpl="atto.note:{}");
}