cleanup code
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
use argon2::{
|
||||
password_hash::SaltString, Argon2, PasswordHash, PasswordHasher, PasswordVerifier,
|
||||
use crate::{
|
||||
database::{Database, DatabaseKind},
|
||||
queries,
|
||||
};
|
||||
use argon2::{Argon2, PasswordHash, PasswordHasher, PasswordVerifier, password_hash::SaltString};
|
||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use rand_core::{OsRng, RngCore};
|
||||
use serde::Serialize;
|
||||
use sqlx::FromRow;
|
||||
use crate::{database::{Database, DatabaseKind}, queries};
|
||||
use sqlx::{Any, Transaction};
|
||||
|
||||
|
||||
async fn inserted_id(kind: DatabaseKind, tx: &mut Transaction<'_, Any>, table: &str) -> Result<i64, sqlx::Error> {
|
||||
async fn inserted_id(
|
||||
kind: DatabaseKind,
|
||||
tx: &mut Transaction<'_, Any>,
|
||||
table: &str,
|
||||
) -> Result<i64, sqlx::Error> {
|
||||
let query = match kind {
|
||||
DatabaseKind::Sqlite => queries::SQLITE_LAST_INSERT_ID,
|
||||
DatabaseKind::MySql => queries::MYSQL_LAST_INSERT_ID,
|
||||
@@ -92,9 +96,9 @@ pub struct Revision {
|
||||
|
||||
pub async fn find_workspace(pool: &Database, slug: &str) -> Result<Option<Workspace>, sqlx::Error> {
|
||||
sqlx::query_as::<_, Workspace>(queries::get(pool.kind(), queries::Q001))
|
||||
.bind(slug)
|
||||
.fetch_optional(pool.pool())
|
||||
.await
|
||||
.bind(slug)
|
||||
.fetch_optional(pool.pool())
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn create_workspace(
|
||||
@@ -103,22 +107,27 @@ pub async fn create_workspace(
|
||||
title: &str,
|
||||
password: Option<&str>,
|
||||
) -> Result<Workspace, sqlx::Error> {
|
||||
let password_hash = password.filter(|value| !value.is_empty()).map(hash_password);
|
||||
let password_hash = password
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(hash_password);
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q002))
|
||||
.bind(slug)
|
||||
.bind(title)
|
||||
.bind(password_hash)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
.bind(slug)
|
||||
.bind(title)
|
||||
.bind(password_hash)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
|
||||
sqlx::query_as::<_, Workspace>(queries::get(pool.kind(), queries::Q001))
|
||||
.bind(slug)
|
||||
.fetch_one(pool.pool())
|
||||
.await
|
||||
.bind(slug)
|
||||
.fetch_one(pool.pool())
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn verify_workspace_password(workspace: &Workspace, password: Option<&str>) -> bool {
|
||||
match (&workspace.password_hash, password.filter(|value| !value.is_empty())) {
|
||||
match (
|
||||
&workspace.password_hash,
|
||||
password.filter(|value| !value.is_empty()),
|
||||
) {
|
||||
(None, _) => true,
|
||||
(Some(hash), Some(password)) => PasswordHash::new(hash)
|
||||
.ok()
|
||||
@@ -177,13 +186,13 @@ pub async fn create_note(
|
||||
created_by: Option<&str>,
|
||||
) -> Result<Note, sqlx::Error> {
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q005))
|
||||
.bind(workspace_id)
|
||||
.bind(slug)
|
||||
.bind(title)
|
||||
.bind(protected)
|
||||
.bind(created_by)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
.bind(workspace_id)
|
||||
.bind(slug)
|
||||
.bind(title)
|
||||
.bind(protected)
|
||||
.bind(created_by)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
|
||||
find_note(pool, workspace_id, slug)
|
||||
.await?
|
||||
@@ -227,9 +236,9 @@ pub async fn save_revision(
|
||||
|
||||
pub async fn list_revisions(pool: &Database, note_id: i64) -> Result<Vec<Revision>, sqlx::Error> {
|
||||
sqlx::query_as::<_, Revision>(queries::get(pool.kind(), queries::Q010))
|
||||
.bind(note_id)
|
||||
.fetch_all(pool.pool())
|
||||
.await
|
||||
.bind(note_id)
|
||||
.fetch_all(pool.pool())
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn random_suffix(length: usize) -> String {
|
||||
@@ -265,7 +274,9 @@ pub fn normalize_timestamp(value: &str) -> String {
|
||||
let offset_start = postgres.len() - 3;
|
||||
let offset = &postgres[offset_start..];
|
||||
if (offset.starts_with('+') || offset.starts_with('-'))
|
||||
&& offset[1..].chars().all(|character| character.is_ascii_digit())
|
||||
&& offset[1..]
|
||||
.chars()
|
||||
.all(|character| character.is_ascii_digit())
|
||||
{
|
||||
postgres.push_str(":00");
|
||||
}
|
||||
@@ -305,9 +316,9 @@ pub struct Pad {
|
||||
|
||||
pub async fn find_pad(pool: &Database, slug: &str) -> Result<Option<Pad>, sqlx::Error> {
|
||||
sqlx::query_as::<_, Pad>(queries::get(pool.kind(), queries::Q011))
|
||||
.bind(slug)
|
||||
.fetch_optional(pool.pool())
|
||||
.await
|
||||
.bind(slug)
|
||||
.fetch_optional(pool.pool())
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn create_pad(
|
||||
@@ -316,22 +327,27 @@ pub async fn create_pad(
|
||||
title: &str,
|
||||
password: Option<&str>,
|
||||
) -> Result<Pad, sqlx::Error> {
|
||||
let password_hash = password.filter(|value| !value.is_empty()).map(hash_password);
|
||||
let password_hash = password
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(hash_password);
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q012))
|
||||
.bind(slug)
|
||||
.bind(title)
|
||||
.bind(password_hash)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
.bind(slug)
|
||||
.bind(title)
|
||||
.bind(password_hash)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
|
||||
sqlx::query_as::<_, Pad>(queries::get(pool.kind(), queries::Q011))
|
||||
.bind(slug)
|
||||
.fetch_one(pool.pool())
|
||||
.await
|
||||
.bind(slug)
|
||||
.fetch_one(pool.pool())
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn verify_pad_password(pad: &Pad, password: Option<&str>) -> bool {
|
||||
match (&pad.password_hash, password.filter(|value| !value.is_empty())) {
|
||||
match (
|
||||
&pad.password_hash,
|
||||
password.filter(|value| !value.is_empty()),
|
||||
) {
|
||||
(None, _) => true,
|
||||
(Some(hash), Some(password)) => PasswordHash::new(hash)
|
||||
.ok()
|
||||
@@ -380,9 +396,9 @@ pub async fn list_pad_revisions(
|
||||
pad_id: i64,
|
||||
) -> Result<Vec<Revision>, sqlx::Error> {
|
||||
sqlx::query_as::<_, Revision>(queries::get(pool.kind(), queries::Q016))
|
||||
.bind(pad_id)
|
||||
.fetch_all(pool.pool())
|
||||
.await
|
||||
.bind(pad_id)
|
||||
.fetch_all(pool.pool())
|
||||
.await
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
@@ -407,7 +423,6 @@ struct PublishedPageRow {
|
||||
updated_at: String,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, FromRow)]
|
||||
struct PostgresPublishedPageRow {
|
||||
token: String,
|
||||
@@ -421,7 +436,15 @@ struct PostgresPublishedPageRow {
|
||||
|
||||
impl From<PostgresPublishedPageRow> for PublishedPage {
|
||||
fn from(value: PostgresPublishedPageRow) -> Self {
|
||||
Self { token: value.token, pad_id: value.pad_id, note_id: value.note_id, allow_task_updates: value.allow_task_updates, title: value.title, content: value.content, updated_at: value.updated_at }
|
||||
Self {
|
||||
token: value.token,
|
||||
pad_id: value.pad_id,
|
||||
note_id: value.note_id,
|
||||
allow_task_updates: value.allow_task_updates,
|
||||
title: value.title,
|
||||
content: value.content,
|
||||
updated_at: value.updated_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<PublishedPageRow> for PublishedPage {
|
||||
@@ -472,78 +495,151 @@ pub async fn publish_note(pool: &Database, note_id: i64) -> Result<String, sqlx:
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
pub async fn find_published_page(pool: &Database, token: &str) -> Result<Option<PublishedPage>, sqlx::Error> {
|
||||
pub async fn find_published_page(
|
||||
pool: &Database,
|
||||
token: &str,
|
||||
) -> Result<Option<PublishedPage>, sqlx::Error> {
|
||||
if pool.kind() == DatabaseKind::Postgres {
|
||||
return Ok(sqlx::query_as::<_, PostgresPublishedPageRow>(queries::Q021_POSTGRES)
|
||||
.bind(token).fetch_optional(pool.pool()).await?.map(Into::into));
|
||||
return Ok(
|
||||
sqlx::query_as::<_, PostgresPublishedPageRow>(queries::Q021_POSTGRES)
|
||||
.bind(token)
|
||||
.fetch_optional(pool.pool())
|
||||
.await?
|
||||
.map(Into::into),
|
||||
);
|
||||
}
|
||||
Ok(sqlx::query_as::<_, PublishedPageRow>(queries::get(pool.kind(), queries::Q021))
|
||||
.bind(token).fetch_optional(pool.pool()).await?.map(Into::into))
|
||||
Ok(
|
||||
sqlx::query_as::<_, PublishedPageRow>(queries::get(pool.kind(), queries::Q021))
|
||||
.bind(token)
|
||||
.fetch_optional(pool.pool())
|
||||
.await?
|
||||
.map(Into::into),
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn pad_public_task_updates(pool: &Database, pad_id: i64) -> Result<bool, sqlx::Error> {
|
||||
if pool.kind() == DatabaseKind::Postgres {
|
||||
return Ok(sqlx::query_scalar::<_, bool>(queries::Q044_POSTGRES)
|
||||
.bind(pad_id).fetch_optional(pool.pool()).await?.unwrap_or(false));
|
||||
.bind(pad_id)
|
||||
.fetch_optional(pool.pool())
|
||||
.await?
|
||||
.unwrap_or(false));
|
||||
}
|
||||
let value = sqlx::query_scalar::<_, i64>(queries::get(pool.kind(), queries::Q044))
|
||||
.bind(pad_id).fetch_optional(pool.pool()).await?.unwrap_or(0);
|
||||
.bind(pad_id)
|
||||
.fetch_optional(pool.pool())
|
||||
.await?
|
||||
.unwrap_or(0);
|
||||
Ok(value != 0)
|
||||
}
|
||||
|
||||
pub async fn note_public_task_updates(pool: &Database, note_id: i64) -> Result<bool, sqlx::Error> {
|
||||
if pool.kind() == DatabaseKind::Postgres {
|
||||
return Ok(sqlx::query_scalar::<_, bool>(queries::Q045_POSTGRES)
|
||||
.bind(note_id).fetch_optional(pool.pool()).await?.unwrap_or(false));
|
||||
.bind(note_id)
|
||||
.fetch_optional(pool.pool())
|
||||
.await?
|
||||
.unwrap_or(false));
|
||||
}
|
||||
let value = sqlx::query_scalar::<_, i64>(queries::get(pool.kind(), queries::Q045))
|
||||
.bind(note_id).fetch_optional(pool.pool()).await?.unwrap_or(0);
|
||||
.bind(note_id)
|
||||
.fetch_optional(pool.pool())
|
||||
.await?
|
||||
.unwrap_or(0);
|
||||
Ok(value != 0)
|
||||
}
|
||||
|
||||
pub async fn set_pad_public_task_updates(pool: &Database, pad_id: i64, allow: bool) -> Result<(), sqlx::Error> {
|
||||
pub async fn set_pad_public_task_updates(
|
||||
pool: &Database,
|
||||
pad_id: i64,
|
||||
allow: bool,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
publish_pad(pool, pad_id).await?;
|
||||
let mut query = sqlx::query(queries::get(pool.kind(), queries::Q040));
|
||||
query = if pool.kind() == DatabaseKind::Postgres { query.bind(allow) } else { query.bind(if allow { 1i64 } else { 0i64 }) };
|
||||
query = if pool.kind() == DatabaseKind::Postgres {
|
||||
query.bind(allow)
|
||||
} else {
|
||||
query.bind(if allow { 1i64 } else { 0i64 })
|
||||
};
|
||||
query.bind(pad_id).execute(pool.pool()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_note_public_task_updates(pool: &Database, note_id: i64, allow: bool) -> Result<(), sqlx::Error> {
|
||||
pub async fn set_note_public_task_updates(
|
||||
pool: &Database,
|
||||
note_id: i64,
|
||||
allow: bool,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
publish_note(pool, note_id).await?;
|
||||
let mut query = sqlx::query(queries::get(pool.kind(), queries::Q041));
|
||||
query = if pool.kind() == DatabaseKind::Postgres { query.bind(allow) } else { query.bind(if allow { 1i64 } else { 0i64 }) };
|
||||
query = if pool.kind() == DatabaseKind::Postgres {
|
||||
query.bind(allow)
|
||||
} else {
|
||||
query.bind(if allow { 1i64 } else { 0i64 })
|
||||
};
|
||||
query.bind(note_id).execute(pool.pool()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_public_task(pool: &Database, token: &str, source_line: usize, checked: bool) -> Result<Option<PublishedPage>, sqlx::Error> {
|
||||
let Some(mut page) = find_published_page(pool, token).await? else { return Ok(None); };
|
||||
if !page.allow_task_updates || source_line == 0 { return Ok(Some(page)); }
|
||||
pub async fn update_public_task(
|
||||
pool: &Database,
|
||||
token: &str,
|
||||
source_line: usize,
|
||||
checked: bool,
|
||||
) -> Result<Option<PublishedPage>, sqlx::Error> {
|
||||
let Some(mut page) = find_published_page(pool, token).await? else {
|
||||
return Ok(None);
|
||||
};
|
||||
if !page.allow_task_updates || source_line == 0 {
|
||||
return Ok(Some(page));
|
||||
}
|
||||
let mut lines: Vec<String> = page.content.split('\n').map(str::to_owned).collect();
|
||||
let Some(line) = lines.get_mut(source_line - 1) else { return Ok(Some(page)); };
|
||||
let Some(line) = lines.get_mut(source_line - 1) else {
|
||||
return Ok(Some(page));
|
||||
};
|
||||
let bytes = line.as_bytes();
|
||||
let mut i = 0usize;
|
||||
while i < bytes.len() && bytes[i].is_ascii_whitespace() { i += 1; }
|
||||
if i >= bytes.len() || !matches!(bytes[i], b'-' | b'*' | b'+') { return Ok(Some(page)); }
|
||||
while i < bytes.len() && bytes[i].is_ascii_whitespace() {
|
||||
i += 1;
|
||||
}
|
||||
if i >= bytes.len() || !matches!(bytes[i], b'-' | b'*' | b'+') {
|
||||
return Ok(Some(page));
|
||||
}
|
||||
i += 1;
|
||||
while i < bytes.len() && bytes[i].is_ascii_whitespace() { i += 1; }
|
||||
if i + 2 >= bytes.len() || bytes[i] != b'[' || !matches!(bytes[i + 1], b' ' | b'x' | b'X') || bytes[i + 2] != b']' { return Ok(Some(page)); }
|
||||
while i < bytes.len() && bytes[i].is_ascii_whitespace() {
|
||||
i += 1;
|
||||
}
|
||||
if i + 2 >= bytes.len()
|
||||
|| bytes[i] != b'['
|
||||
|| !matches!(bytes[i + 1], b' ' | b'x' | b'X')
|
||||
|| bytes[i + 2] != b']'
|
||||
{
|
||||
return Ok(Some(page));
|
||||
}
|
||||
line.replace_range(i + 1..i + 2, if checked { "x" } else { " " });
|
||||
page.content = lines.join("\n");
|
||||
if let Some(id) = page.pad_id {
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q042)).bind(&page.content).bind(id).execute(pool.pool()).await?;
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q042))
|
||||
.bind(&page.content)
|
||||
.bind(id)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
} else if let Some(id) = page.note_id {
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q043)).bind(&page.content).bind(id).execute(pool.pool()).await?;
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q043))
|
||||
.bind(&page.content)
|
||||
.bind(id)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
}
|
||||
find_published_page(pool, token).await
|
||||
}
|
||||
|
||||
pub async fn pad_file_token(pool: &Database, pad_id: i64) -> Result<String, sqlx::Error> {
|
||||
if let Some(token) = sqlx::query_scalar::<_, Option<String>>(queries::get(pool.kind(), queries::Q022))
|
||||
.bind(pad_id)
|
||||
.fetch_one(pool.pool())
|
||||
.await?
|
||||
if let Some(token) =
|
||||
sqlx::query_scalar::<_, Option<String>>(queries::get(pool.kind(), queries::Q022))
|
||||
.bind(pad_id)
|
||||
.fetch_one(pool.pool())
|
||||
.await?
|
||||
{
|
||||
return Ok(token);
|
||||
}
|
||||
@@ -562,10 +658,11 @@ pub async fn pad_file_token(pool: &Database, pad_id: i64) -> Result<String, sqlx
|
||||
}
|
||||
|
||||
pub async fn note_file_token(pool: &Database, note_id: i64) -> Result<String, sqlx::Error> {
|
||||
if let Some(token) = sqlx::query_scalar::<_, Option<String>>(queries::get(pool.kind(), queries::Q024))
|
||||
.bind(note_id)
|
||||
.fetch_one(pool.pool())
|
||||
.await?
|
||||
if let Some(token) =
|
||||
sqlx::query_scalar::<_, Option<String>>(queries::get(pool.kind(), queries::Q024))
|
||||
.bind(note_id)
|
||||
.fetch_one(pool.pool())
|
||||
.await?
|
||||
{
|
||||
return Ok(token);
|
||||
}
|
||||
@@ -583,7 +680,6 @@ pub async fn note_file_token(pool: &Database, note_id: i64) -> Result<String, sq
|
||||
.await
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum FileOwnerKind {
|
||||
Pad,
|
||||
@@ -596,25 +692,33 @@ pub struct FileOwner {
|
||||
pub id: i64,
|
||||
}
|
||||
|
||||
pub async fn find_file_owner(pool: &Database, token: &str) -> Result<Option<FileOwner>, sqlx::Error> {
|
||||
pub async fn find_file_owner(
|
||||
pool: &Database,
|
||||
token: &str,
|
||||
) -> Result<Option<FileOwner>, sqlx::Error> {
|
||||
if let Some(id) = sqlx::query_scalar::<_, i64>(queries::get(pool.kind(), queries::Q026))
|
||||
.bind(token)
|
||||
.fetch_optional(pool.pool())
|
||||
.await?
|
||||
{
|
||||
return Ok(Some(FileOwner { kind: FileOwnerKind::Pad, id }));
|
||||
return Ok(Some(FileOwner {
|
||||
kind: FileOwnerKind::Pad,
|
||||
id,
|
||||
}));
|
||||
}
|
||||
if let Some(id) = sqlx::query_scalar::<_, i64>(queries::get(pool.kind(), queries::Q027))
|
||||
.bind(token)
|
||||
.fetch_optional(pool.pool())
|
||||
.await?
|
||||
{
|
||||
return Ok(Some(FileOwner { kind: FileOwnerKind::Note, id }));
|
||||
return Ok(Some(FileOwner {
|
||||
kind: FileOwnerKind::Note,
|
||||
id,
|
||||
}));
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize, FromRow)]
|
||||
pub struct NoteFile {
|
||||
pub id: i64,
|
||||
@@ -655,14 +759,29 @@ impl From<SqliteNoteFile> for NoteFile {
|
||||
}
|
||||
|
||||
pub async fn delete_note(pool: &Database, note_id: i64) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q031)).bind(note_id).execute(pool.pool()).await?;
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q031))
|
||||
.bind(note_id)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn register_note_file(pool: &Database, note_id: i64, filename: &str, url: &str, mime_type: &str, size_bytes: i64) -> Result<(), sqlx::Error> {
|
||||
pub async fn register_note_file(
|
||||
pool: &Database,
|
||||
note_id: i64,
|
||||
filename: &str,
|
||||
url: &str,
|
||||
mime_type: &str,
|
||||
size_bytes: i64,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q032))
|
||||
.bind(note_id).bind(filename).bind(url).bind(mime_type).bind(size_bytes)
|
||||
.execute(pool.pool()).await?;
|
||||
.bind(note_id)
|
||||
.bind(filename)
|
||||
.bind(url)
|
||||
.bind(mime_type)
|
||||
.bind(size_bytes)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -670,7 +789,11 @@ pub async fn list_note_files(pool: &Database, note_id: i64) -> Result<Vec<NoteFi
|
||||
list_files(pool, queries::Q033, note_id).await
|
||||
}
|
||||
|
||||
async fn list_files(pool: &Database, query: &'static str, owner_id: i64) -> Result<Vec<NoteFile>, sqlx::Error> {
|
||||
async fn list_files(
|
||||
pool: &Database,
|
||||
query: &'static str,
|
||||
owner_id: i64,
|
||||
) -> Result<Vec<NoteFile>, sqlx::Error> {
|
||||
if pool.kind() == DatabaseKind::Sqlite {
|
||||
return Ok(sqlx::query_as::<_, SqliteNoteFile>(query)
|
||||
.bind(owner_id)
|
||||
@@ -686,17 +809,41 @@ async fn list_files(pool: &Database, query: &'static str, owner_id: i64) -> Resu
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn set_note_file_attached(pool: &Database, file_id: i64, attached: bool) -> Result<(), sqlx::Error> {
|
||||
let detached_at: Option<String> = if attached { None } else { Some(chrono::Utc::now().to_rfc3339()) };
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q034)).bind(attached).bind(detached_at).bind(file_id).execute(pool.pool()).await?;
|
||||
pub async fn set_note_file_attached(
|
||||
pool: &Database,
|
||||
file_id: i64,
|
||||
attached: bool,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
let detached_at: Option<String> = if attached {
|
||||
None
|
||||
} else {
|
||||
Some(chrono::Utc::now().to_rfc3339())
|
||||
};
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q034))
|
||||
.bind(attached)
|
||||
.bind(detached_at)
|
||||
.bind(file_id)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
pub async fn register_pad_file(pool: &Database, pad_id: i64, filename: &str, url: &str, mime_type: &str, size_bytes: i64) -> Result<(), sqlx::Error> {
|
||||
pub async fn register_pad_file(
|
||||
pool: &Database,
|
||||
pad_id: i64,
|
||||
filename: &str,
|
||||
url: &str,
|
||||
mime_type: &str,
|
||||
size_bytes: i64,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q035))
|
||||
.bind(pad_id).bind(filename).bind(url).bind(mime_type).bind(size_bytes)
|
||||
.execute(pool.pool()).await?;
|
||||
.bind(pad_id)
|
||||
.bind(filename)
|
||||
.bind(url)
|
||||
.bind(mime_type)
|
||||
.bind(size_bytes)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -704,14 +851,30 @@ pub async fn list_pad_files(pool: &Database, pad_id: i64) -> Result<Vec<NoteFile
|
||||
list_files(pool, queries::Q036, pad_id).await
|
||||
}
|
||||
|
||||
pub async fn set_pad_file_attached(pool: &Database, file_id: i64, attached: bool) -> Result<(), sqlx::Error> {
|
||||
let detached_at: Option<String> = if attached { None } else { Some(chrono::Utc::now().to_rfc3339()) };
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q037)).bind(attached).bind(detached_at).bind(file_id).execute(pool.pool()).await?;
|
||||
pub async fn set_pad_file_attached(
|
||||
pool: &Database,
|
||||
file_id: i64,
|
||||
attached: bool,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
let detached_at: Option<String> = if attached {
|
||||
None
|
||||
} else {
|
||||
Some(chrono::Utc::now().to_rfc3339())
|
||||
};
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q037))
|
||||
.bind(attached)
|
||||
.bind(detached_at)
|
||||
.bind(file_id)
|
||||
.execute(pool.pool())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
pub async fn find_note_file(pool: &Database, note_id: i64, file_id: i64) -> Result<Option<NoteFile>, sqlx::Error> {
|
||||
pub async fn find_note_file(
|
||||
pool: &Database,
|
||||
note_id: i64,
|
||||
file_id: i64,
|
||||
) -> Result<Option<NoteFile>, sqlx::Error> {
|
||||
if pool.kind() == DatabaseKind::Sqlite {
|
||||
return Ok(sqlx::query_as::<_, SqliteNoteFile>(queries::Q038)
|
||||
.bind(file_id)
|
||||
@@ -727,7 +890,11 @@ pub async fn find_note_file(pool: &Database, note_id: i64, file_id: i64) -> Resu
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_note_file(pool: &Database, note_id: i64, file_id: i64) -> Result<(), sqlx::Error> {
|
||||
pub async fn delete_note_file(
|
||||
pool: &Database,
|
||||
note_id: i64,
|
||||
file_id: i64,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(queries::get(pool.kind(), queries::Q039))
|
||||
.bind(file_id)
|
||||
.bind(note_id)
|
||||
|
||||
Reference in New Issue
Block a user