From 533e9d5d2e6b95b03c83c305d18b7e720adf20cf Mon Sep 17 00:00:00 2001 From: TheGeneralist <180094941+thegeneralist01@users.noreply.github.com> Date: Fri, 26 Jun 2026 17:33:35 +0200 Subject: [PATCH] =?UTF-8?q?feat(collections):=20db=20=E2=80=94=20update=5F?= =?UTF-8?q?collection,=20delete=5Fcollection=20helpers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/archivr-core/src/database.rs | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/crates/archivr-core/src/database.rs b/crates/archivr-core/src/database.rs index 07126a3..d690070 100644 --- a/crates/archivr-core/src/database.rs +++ b/crates/archivr-core/src/database.rs @@ -1692,6 +1692,45 @@ pub fn get_entry_collection_memberships( .map_err(Into::into) } +/// Renames a collection and/or updates its default_visibility_bits. +/// Returns true if updated, false if not found. +/// Refuses to rename the '_default_' collection. +pub fn update_collection( + conn: &Connection, + collection_uid: &str, + new_name: Option<&str>, + new_visibility_bits: Option, +) -> Result { + let coll = get_collection_by_uid(conn, collection_uid)?; + let Some(coll) = coll else { return Ok(false) }; + if coll.slug == "_default_" { + anyhow::bail!("cannot modify the default collection"); + } + let name = new_name.unwrap_or(&coll.name); + let vbits = new_visibility_bits.unwrap_or(coll.default_visibility_bits); + conn.execute( + "UPDATE collections SET name = ?1, default_visibility_bits = ?2 WHERE id = ?3", + params![name, vbits as i64, coll.id], + )?; + Ok(true) +} + +/// Deletes a collection and cascades to collection_entries. +/// Returns true if deleted, false if not found. +/// Refuses to delete the '_default_' collection. +pub fn delete_collection( + conn: &Connection, + collection_uid: &str, +) -> Result { + let coll = get_collection_by_uid(conn, collection_uid)?; + let Some(coll) = coll else { return Ok(false) }; + if coll.slug == "_default_" { + anyhow::bail!("cannot delete the default collection"); + } + conn.execute("DELETE FROM collections WHERE id = ?1", [coll.id])?; + Ok(true) +} + fn run_id_for_item(conn: &Connection, item_id: i64) -> Result { let run_id = conn.query_row( "SELECT run_id FROM archive_run_items WHERE id = ?1",