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?;