use std::hash::Hash;
use rusqlite::params;
use crate::{
error::Result,
manager::{PostArchiverConnection, PostArchiverManager, UpdateCollection},
CollectionId, FileMetaId,
};
impl<T> PostArchiverManager<T>
where
T: PostArchiverConnection,
{
pub fn import_collection(&self, collection: UnsyncCollection) -> Result<CollectionId> {
if let Some(id) = self.find_collection_by_source(&collection.source)? {
self.bind(id)
.update(UpdateCollection::default().name(collection.name))?;
return Ok(id);
}
let mut ins_stmt = self.conn().prepare_cached(
"INSERT INTO collections (name, source, thumb) VALUES (?, ?, ?) RETURNING id",
)?;
let id: CollectionId = ins_stmt.query_row(
params![
collection.name,
collection.source,
Option::<FileMetaId>::None
],
|row| row.get(0),
)?;
Ok(id)
}
pub fn import_collections(
&self,
collections: impl IntoIterator<Item = UnsyncCollection>,
) -> Result<Vec<CollectionId>> {
collections
.into_iter()
.map(|collection| self.import_collection(collection))
.collect::<Result<Vec<CollectionId>>>()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UnsyncCollection {
pub name: String,
pub source: String,
}
impl UnsyncCollection {
pub fn new(name: String, source: String) -> Self {
Self { name, source }
}
pub fn name(mut self, name: String) -> Self {
self.name = name;
self
}
pub fn source(mut self, source: String) -> Self {
self.source = source;
self
}
}