mirror of
https://github.com/spacedriveapp/spacedrive.git
synced 2026-03-13 03:56:33 -04:00
47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
use crate::db::{connection, entity::library};
|
|
use anyhow::{bail, Result};
|
|
use sea_orm::{entity::*, DatabaseConnection, QueryFilter};
|
|
use strum::Display;
|
|
|
|
#[derive(Display)]
|
|
pub enum InitError {
|
|
LibraryNotFound,
|
|
}
|
|
|
|
pub async fn get_primary_library(db: &DatabaseConnection) -> Result<library::Model> {
|
|
// get library entity by is_primary column, should be unique
|
|
let mut existing_libs = library::Entity::find()
|
|
.filter(library::Column::IsPrimary.eq(true))
|
|
.all(db)
|
|
.await?;
|
|
|
|
// return library
|
|
if existing_libs.len() == 0 {
|
|
bail!(InitError::LibraryNotFound.to_string());
|
|
} else {
|
|
Ok(existing_libs.swap_remove(0))
|
|
}
|
|
}
|
|
|
|
pub async fn init_library() -> Result<()> {
|
|
let db = connection::get_connection().await?;
|
|
|
|
let library = get_primary_library(&db).await;
|
|
// if no library create one now
|
|
if library.is_err() {
|
|
let library = library::ActiveModel {
|
|
name: Set("Primary".to_owned()),
|
|
is_primary: Set(true),
|
|
..Default::default()
|
|
};
|
|
|
|
let library = library.save(&db).await?;
|
|
|
|
println!("created library {:?}", &library);
|
|
} else {
|
|
// println!("library loaded {:?}", library.unwrap());
|
|
};
|
|
|
|
Ok(())
|
|
}
|