new functions

This commit is contained in:
Mateusz Gruszczyński
2026-07-29 09:49:37 +02:00
parent bbcd1fe987
commit 904f59146b
23 changed files with 469 additions and 53 deletions
+48
View File
@@ -94,6 +94,28 @@ pub async fn publish_note(pool: &Database, note_id: i64) -> Result<String, sqlx:
Ok(token)
}
pub async fn pad_public_page_enabled(pool: &Database, pad_id: i64) -> Result<bool, sqlx::Error> {
Ok(sqlx::query_scalar::<_, String>(queries::get(pool.kind(), queries::Q017))
.bind(pad_id).fetch_optional(pool.pool()).await?.is_some())
}
pub async fn note_public_page_enabled(pool: &Database, note_id: i64) -> Result<bool, sqlx::Error> {
Ok(sqlx::query_scalar::<_, String>(queries::get(pool.kind(), queries::Q019))
.bind(note_id).fetch_optional(pool.pool()).await?.is_some())
}
pub async fn unpublish_pad(pool: &Database, pad_id: i64) -> Result<(), sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "DELETE FROM published_pages WHERE pad_id = $1", _ => "DELETE FROM published_pages WHERE pad_id = ?" };
sqlx::query(sql).bind(pad_id).execute(pool.pool()).await?;
Ok(())
}
pub async fn unpublish_note(pool: &Database, note_id: i64) -> Result<(), sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "DELETE FROM published_pages WHERE note_id = $1", _ => "DELETE FROM published_pages WHERE note_id = ?" };
sqlx::query(sql).bind(note_id).execute(pool.pool()).await?;
Ok(())
}
pub async fn find_published_page(
pool: &Database,
token: &str,
@@ -342,3 +364,29 @@ impl<'r> sqlx::FromRow<'r, AnyRow> for PostgresPublishedPageRow {
})
}
}
pub async fn pad_public_page_disabled(pool: &Database, pad_id: i64) -> Result<bool, sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "SELECT public_page_disabled FROM pads WHERE id = $1", _ => "SELECT public_page_disabled FROM pads WHERE id = ?" };
if pool.kind() == DatabaseKind::Postgres { return Ok(sqlx::query_scalar::<_, bool>(sql).bind(pad_id).fetch_one(pool.pool()).await?); }
Ok(sqlx::query_scalar::<_, i64>(sql).bind(pad_id).fetch_one(pool.pool()).await? != 0)
}
pub async fn note_public_page_disabled(pool: &Database, note_id: i64) -> Result<bool, sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "SELECT public_page_disabled FROM notes WHERE id = $1", _ => "SELECT public_page_disabled FROM notes WHERE id = ?" };
if pool.kind() == DatabaseKind::Postgres { return Ok(sqlx::query_scalar::<_, bool>(sql).bind(note_id).fetch_one(pool.pool()).await?); }
Ok(sqlx::query_scalar::<_, i64>(sql).bind(note_id).fetch_one(pool.pool()).await? != 0)
}
pub async fn set_pad_public_page_disabled(pool: &Database, pad_id: i64, disabled: bool) -> Result<(), sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "UPDATE pads SET public_page_disabled = $1 WHERE id = $2", _ => "UPDATE pads SET public_page_disabled = ? WHERE id = ?" };
let mut query = sqlx::query(sql);
query = if pool.kind() == DatabaseKind::Postgres { query.bind(disabled) } else { query.bind(if disabled { 1i64 } else { 0i64 }) };
query.bind(pad_id).execute(pool.pool()).await?; Ok(())
}
pub async fn set_note_public_page_disabled(pool: &Database, note_id: i64, disabled: bool) -> Result<(), sqlx::Error> {
let sql = match pool.kind() { DatabaseKind::Postgres => "UPDATE notes SET public_page_disabled = $1 WHERE id = $2", _ => "UPDATE notes SET public_page_disabled = ? WHERE id = ?" };
let mut query = sqlx::query(sql);
query = if pool.kind() == DatabaseKind::Postgres { query.bind(disabled) } else { query.bind(if disabled { 1i64 } else { 0i64 }) };
query.bind(note_id).execute(pool.pool()).await?; Ok(())
}