add: pages api

add: entries base
add: entries api
This commit is contained in:
trisua 2025-03-24 19:55:08 -04:00
parent 38dbf10130
commit daa223d529
8 changed files with 389 additions and 8 deletions

View file

@ -0,0 +1,69 @@
use super::*;
use crate::cache::Cache;
use crate::model::auth::User;
use crate::model::{Error, Result, journal::JournalEntry, permissions::FinePermission};
use crate::{auto_method, execute, get, query_row};
#[cfg(feature = "sqlite")]
use rusqlite::Row;
#[cfg(feature = "postgres")]
use tokio_postgres::Row;
impl DataManager {
/// Get a [`JournalEntry`] from an SQL row.
pub(crate) fn get_entry_from_row(
#[cfg(feature = "sqlite")] x: &Row<'_>,
#[cfg(feature = "postgres")] x: &Row,
) -> JournalEntry {
JournalEntry {
id: get!(x->0(u64)) as usize,
created: get!(x->1(u64)) as usize,
content: get!(x->2(String)),
owner: get!(x->3(u64)) as usize,
journal: get!(x->4(u64)) as usize,
}
}
auto_method!(get_entry_by_id()@get_entry_from_row -> "SELECT * FROM entries WHERE id = $1" --name="journal entry" --returns=JournalEntry --cache-key-tmpl="atto.entry:{}");
/// Create a new journal entry in the database.
///
/// # Arguments
/// * `data` - a mock [`JournalEntry`] object to insert
pub async fn create_entry(&self, data: JournalEntry) -> Result<()> {
// check values
if data.content.len() < 2 {
return Err(Error::DataTooShort("content".to_string()));
} else if data.content.len() > 4096 {
return Err(Error::DataTooLong("username".to_string()));
}
// ...
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"INSERT INTO entries VALUES ($1, $2, $3, $4, $5",
&[
&data.id.to_string().as_str(),
&data.created.to_string().as_str(),
&data.content.as_str(),
&data.owner.to_string().as_str(),
&data.journal.to_string().as_str(),
]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
Ok(())
}
auto_method!(delete_entry()@get_entry_by_id:MANAGE_JOURNAL_ENTRIES -> "DELETE FROM entries WHERE id = $1" --cache-key-tmpl="atto.entry:{}");
auto_method!(update_entry_content(String)@get_entry_by_id:MANAGE_JOURNAL_ENTRIES -> "UPDATE entries SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.entry:{}");
}

View file

@ -1,6 +1,7 @@
mod auth;
mod common;
mod drivers;
mod entries;
mod pages;
#[cfg(feature = "sqlite")]

View file

@ -1,6 +1,7 @@
use super::*;
use crate::cache::Cache;
use crate::model::auth::User;
use crate::model::journal::{JournalPageReadAccess, JournalPageWriteAccess};
use crate::model::{Error, Result, journal::JournalPage, permissions::FinePermission};
use crate::{auto_method, execute, get, query_row};
@ -34,15 +35,11 @@ impl DataManager {
/// # Arguments
/// * `data` - a mock [`JournalPage`] object to insert
pub async fn create_page(&self, data: JournalPage) -> Result<()> {
if self.0.security.registration_enabled == false {
return Err(Error::RegistrationDisabled);
}
// check values
if data.title.len() < 2 {
return Err(Error::DataTooShort("title".to_string()));
} else if data.title.len() > 32 {
return Err(Error::DataTooLong("username".to_string()));
return Err(Error::DataTooLong("title".to_string()));
}
if data.prompt.len() < 2 {
@ -81,6 +78,6 @@ impl DataManager {
auto_method!(delete_page()@get_page_by_id:MANAGE_JOURNAL_PAGES -> "DELETE FROM pages WHERE id = $1" --cache-key-tmpl="atto.page:{}");
auto_method!(update_page_title(String)@get_page_by_id:MANAGE_JOURNAL_PAGES -> "UPDATE pages SET title = $1 WHERE id = $2" --cache-key-tmpl="atto.page:{}");
auto_method!(update_page_prompt(String)@get_page_by_id:MANAGE_JOURNAL_PAGES -> "UPDATE pages SET prompt = $1 WHERE id = $2" --cache-key-tmpl="atto.page:{}");
auto_method!(update_page_read_access(String)@get_page_by_id:MANAGE_JOURNAL_PAGES -> "UPDATE pages SET read_access = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.page:{}");
auto_method!(update_page_write_access(String)@get_page_by_id:MANAGE_JOURNAL_PAGES -> "UPDATE pages SET write_access = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.page:{}");
auto_method!(update_page_read_access(JournalPageReadAccess)@get_page_by_id:MANAGE_JOURNAL_PAGES -> "UPDATE pages SET read_access = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.page:{}");
auto_method!(update_page_write_access(JournalPageWriteAccess)@get_page_by_id:MANAGE_JOURNAL_PAGES -> "UPDATE pages SET write_access = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.page:{}");
}

View file

@ -69,3 +69,30 @@ impl Default for JournalPageWriteAccess {
Self::Authenticated
}
}
#[derive(Serialize, Deserialize)]
pub struct JournalEntry {
pub id: usize,
pub created: usize,
pub content: String,
/// The ID of the owner of this entry.
pub owner: usize,
/// The ID of the [`JournalPage`] this entry belongs to.
pub journal: usize,
}
impl JournalEntry {
/// Create a new [`JournalEntry`].
pub fn new(content: String, journal: usize, owner: usize) -> Self {
Self {
id: AlmostSnowflake::new(1234567890)
.to_string()
.parse::<usize>()
.unwrap(),
created: unix_epoch_timestamp() as usize,
content,
owner,
journal,
}
}
}