Add hash model

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 13f3b39f8b
commit 3d65a09e9b

@ -19,7 +19,7 @@ pub struct File {
}
impl File {
fn new(db: DatabaseConnection, model: FileModel, hash: HashModel) -> Self {
pub(crate) fn new(db: DatabaseConnection, model: FileModel, hash: HashModel) -> Self {
Self { db, model, hash }
}

@ -0,0 +1,75 @@
use crate::file::File;
use crate::thumbnail::Thumbnail;
use mediarepo_core::error::RepoResult;
use mediarepo_database::entities::file;
use mediarepo_database::entities::hash;
use mediarepo_database::entities::thumbnail;
use sea_orm::prelude::*;
use sea_orm::{DatabaseConnection, Set};
pub struct Hash {
db: DatabaseConnection,
model: hash::Model,
}
impl Hash {
pub(crate) fn new(db: DatabaseConnection, model: hash::Model) -> Self {
Self { db, model }
}
/// Searches for the hash by id
pub async fn by_id(db: DatabaseConnection, id: i64) -> RepoResult<Option<Self>> {
let hash = hash::Entity::find_by_id(id)
.one(&db)
.await?
.map(|model| Self::new(db, model));
Ok(hash)
}
/// Adds a new hash to the database
pub async fn add(db: DatabaseConnection, value: String) -> RepoResult<Self> {
let active_model = hash::ActiveModel {
value: Set(value),
..Default::default()
};
let active_model: hash::ActiveModel = active_model.insert(&db).await?;
let hash = Self::by_id(db, active_model.id.unwrap())
.await?
.expect("Inserted value does not exist");
Ok(hash)
}
pub fn id(&self) -> i64 {
self.model.id
}
pub fn value(&self) -> &String {
&self.model.value
}
/// Returns the file associated with the hash
pub async fn file(&self) -> RepoResult<Option<File>> {
let file = self
.model
.find_related(file::Entity)
.one(&self.db)
.await?
.map(|file_model| File::new(self.db.clone(), file_model, self.model.clone()));
Ok(file)
}
/// Returns the the thumbnail associated with the hash
pub async fn thumbnail(&self) -> RepoResult<Option<Thumbnail>> {
let thumbnail = self
.model
.find_related(thumbnail::Entity)
.one(&self.db)
.await?
.map(|thumb_model| Thumbnail::new(self.db.clone(), thumb_model, self.model.clone()));
Ok(thumbnail)
}
}

@ -1,5 +1,6 @@
pub mod file;
pub mod file_type;
pub mod hash;
pub mod repo;
pub mod storage;
pub mod thumbnail;

@ -3,7 +3,7 @@ use mediarepo_core::error::RepoResult;
use mediarepo_database::entities::hash;
use mediarepo_database::entities::thumbnail;
use sea_orm::prelude::*;
use sea_orm::DatabaseConnection;
use sea_orm::{DatabaseConnection, Set};
pub struct Thumbnail {
db: DatabaseConnection,
@ -31,6 +31,29 @@ impl Thumbnail {
}
}
/// Inserts a thumbnail into the database
pub async fn add(
db: DatabaseConnection,
hash_id: i64,
file_id: i64,
height: i32,
width: i32,
) -> RepoResult<Self> {
let active_model = thumbnail::ActiveModel {
hash_id: Set(hash_id),
file_id: Set(file_id),
height: Set(height),
width: Set(width),
..Default::default()
};
let active_model: thumbnail::ActiveModel = active_model.insert(&db).await?;
let thumbnail = Self::by_id(db, active_model.id.unwrap())
.await?
.expect("Inserted thumbnail does not exist");
Ok(thumbnail)
}
/// Returns all thumbnails for a given file
pub async fn for_file_id(db: DatabaseConnection, file_id: i64) -> RepoResult<Vec<Self>> {
let thumb_models: Vec<(thumbnail::Model, Option<hash::Model>)> = thumbnail::Entity::find()

Loading…
Cancel
Save