64 lines
1.5 KiB
Rust
64 lines
1.5 KiB
Rust
use crate::api::models::AlbumQuery;
|
|
use chrono::{DateTime, Utc};
|
|
use j_db::database::Database;
|
|
use j_db::model::JdbModel;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
pub struct Album {
|
|
pub album_name: String,
|
|
pub images: Vec<u64>,
|
|
pub aliases: Vec<String>,
|
|
pub created_at: DateTime<Utc>,
|
|
pub modified_at: DateTime<Utc>,
|
|
pub owner: u64,
|
|
|
|
id: Option<u64>,
|
|
}
|
|
|
|
impl Album {
|
|
pub fn new(name: &str, aliases: Vec<String>, owner: u64) -> Album {
|
|
Album {
|
|
album_name: name.to_string(),
|
|
images: vec![],
|
|
aliases,
|
|
created_at: Utc::now(),
|
|
modified_at: Utc::now(),
|
|
owner,
|
|
id: None,
|
|
}
|
|
}
|
|
|
|
pub fn find_album_by_query(db: &Database, album_query: AlbumQuery) -> Vec<Self> {
|
|
db.filter(|_, album: &Album| {
|
|
if let Some(album_name) = &album_query.album_name {
|
|
if album_name != &album.album_name && !album.aliases.contains(album_name) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
true
|
|
})
|
|
.unwrap()
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
impl JdbModel for Album {
|
|
fn id(&self) -> Option<u64> {
|
|
self.id
|
|
}
|
|
|
|
fn set_id(&mut self, id: u64) {
|
|
self.id = Some(id);
|
|
}
|
|
|
|
fn tree() -> String {
|
|
"Album".to_string()
|
|
}
|
|
|
|
fn check_unique(&self, other: &Self) -> bool {
|
|
other.album_name != self.album_name
|
|
}
|
|
}
|