/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

use anyhow::Result;
use jellycommon::{
    NO_SLUG,
    jellyobject::{OBB, Path},
};
use jellydb::{Filter, Query, RowNum, Transaction};

pub fn get_or_insert_slug(txn: &mut dyn Transaction, slug: &str) -> Result<RowNum> {
    match txn.query_single(Query {
        filter: Filter::Match(Path(vec![NO_SLUG.0]), slug.into()),
        ..Default::default()
    })? {
        Some(r) => Ok(r),
        None => txn.insert(OBB::new().with(NO_SLUG, slug).finish()),
    }
}
