Added picox support!
This commit is contained in:
10 files changed
+284
-266
No files matched your search
+18
-14
@@ -1,4 +1,4 @@
|
||||
use crate::album_manager::Album;
|
||||
use crate::album_manager::{AlbumManager, AlbumQuery, Image};
|
||||
use crate::error::Error;
|
||||
use j_db::database::Database;
|
||||
use j_db::error::JDbError;
|
||||
@@ -6,7 +6,7 @@ use j_db::model::JdbModel;
|
||||
use rand::prelude::{IteratorRandom, SliceRandom};
|
||||
use rand::thread_rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct MotivationConfig {
|
||||
@@ -47,12 +47,9 @@ impl MotivationConfig {
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn add_album(db: &Database, album: &str) -> Result<(), Error> {
|
||||
pub fn add_album(db: &Database, id: u64) -> Result<(), Error> {
|
||||
let mut motivation = Self::get_motivation_config(db)?;
|
||||
let album = Album::find_album_by_name_or_alias(db, album)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
motivation.album.push(album.id().unwrap());
|
||||
motivation.album.push(id);
|
||||
|
||||
db.insert(motivation)?;
|
||||
|
||||
@@ -79,7 +76,7 @@ impl MotivationConfig {
|
||||
|
||||
pub async fn generate_motivation(
|
||||
db: &Database,
|
||||
base_path: &Path,
|
||||
picox: &AlbumManager,
|
||||
border_color: &str,
|
||||
album_name: Option<String>,
|
||||
) -> Result<Motivation, Error> {
|
||||
@@ -88,16 +85,21 @@ impl MotivationConfig {
|
||||
let mut images = Vec::new();
|
||||
|
||||
if let Some(album_name) = album_name {
|
||||
let album = Album::find_album_by_name_or_alias(db, &album_name)?;
|
||||
let query = AlbumQuery {
|
||||
album_name: Some(album_name),
|
||||
};
|
||||
let albums = picox.query_album(query).await?;
|
||||
|
||||
if let Some(mut album) = album {
|
||||
images.append(&mut album.images)
|
||||
let album = albums.first();
|
||||
|
||||
if let Some(album) = album {
|
||||
images.append(&mut album.images.clone())
|
||||
} else {
|
||||
return Err(Error::from(JDbError::NotFound));
|
||||
}
|
||||
} else {
|
||||
for album in motivation.album {
|
||||
let album = db.get::<Album>(album);
|
||||
let album = picox.get_album_by_id(album).await;
|
||||
|
||||
if let Ok(album) = album {
|
||||
let mut album_images = album.images.clone();
|
||||
@@ -114,8 +116,10 @@ impl MotivationConfig {
|
||||
|
||||
let goal = motivation.goal.iter().choose(&mut thread_rng()).unwrap();
|
||||
|
||||
let image = picox.get_image_by_id(*image).await.unwrap();
|
||||
|
||||
Ok(Motivation {
|
||||
image_path: image.full_path(base_path.to_path_buf()),
|
||||
image,
|
||||
action: action.to_string(),
|
||||
goal: goal.to_string(),
|
||||
border_color: border_color.to_string(),
|
||||
@@ -125,7 +129,7 @@ impl MotivationConfig {
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct Motivation {
|
||||
pub image_path: PathBuf,
|
||||
pub image: Image,
|
||||
pub action: String,
|
||||
pub goal: String,
|
||||
pub border_color: String,
|
||||
|
||||
Reference in new issue
Block a user