Moved motivation config to the database
+ More refactoring + clippy + fmt
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
pub mod album;
|
||||
pub mod insult_compliment;
|
||||
pub mod motivation;
|
||||
pub mod random;
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
use crate::error::Error;
|
||||
use crate::imgur::get_album_images;
|
||||
use j_db::database::Database;
|
||||
use j_db::model::JdbModel;
|
||||
use rand::prelude::{IteratorRandom, SliceRandom};
|
||||
use rand::thread_rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct MotivationConfig {
|
||||
id: Option<u64>,
|
||||
pub album: Vec<String>,
|
||||
pub action: Vec<String>,
|
||||
pub goal: Vec<String>,
|
||||
}
|
||||
|
||||
impl JdbModel for MotivationConfig {
|
||||
fn id(&self) -> Option<u64> {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: u64) {
|
||||
self.id = Some(id)
|
||||
}
|
||||
|
||||
fn tree() -> String {
|
||||
"Motivation".to_string()
|
||||
}
|
||||
|
||||
fn check_unique(&self, _: &Self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl MotivationConfig {
|
||||
pub fn get_motivation_config(db: &Database) -> Result<Self, Error> {
|
||||
Ok(db
|
||||
.filter(|_, _: &MotivationConfig| true)?
|
||||
.next()
|
||||
.unwrap_or(Self {
|
||||
id: None,
|
||||
album: vec![],
|
||||
action: vec![],
|
||||
goal: vec![],
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn add_album(db: &Database, album: &str) -> Result<(), Error> {
|
||||
let mut motivation = Self::get_motivation_config(db)?;
|
||||
motivation.album.push(album.to_string());
|
||||
|
||||
db.insert(motivation)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_action(db: &Database, action: &str) -> Result<(), Error> {
|
||||
let mut motivation = Self::get_motivation_config(db)?;
|
||||
motivation.action.push(action.to_string());
|
||||
|
||||
db.insert(motivation)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_goal(db: &Database, goal: &str) -> Result<(), Error> {
|
||||
let mut motivation = Self::get_motivation_config(db)?;
|
||||
motivation.goal.push(goal.to_string());
|
||||
|
||||
db.insert(motivation)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn generate_motivation(
|
||||
db: &Database,
|
||||
imgur_client_id: &str,
|
||||
border_color: &str,
|
||||
) -> Result<Motivation, Error> {
|
||||
let motivation = Self::get_motivation_config(db)?;
|
||||
|
||||
let mut images = Vec::new();
|
||||
|
||||
for album in motivation.album {
|
||||
let mut album_images = get_album_images(imgur_client_id, &album).await?;
|
||||
images.append(&mut album_images);
|
||||
}
|
||||
|
||||
let image = images.choose(&mut thread_rng()).unwrap();
|
||||
|
||||
let action = motivation.action.iter().choose(&mut thread_rng()).unwrap();
|
||||
|
||||
let goal = motivation.goal.iter().choose(&mut thread_rng()).unwrap();
|
||||
|
||||
Ok(Motivation {
|
||||
image_url: image.link.clone(),
|
||||
action: action.to_string(),
|
||||
goal: goal.to_string(),
|
||||
border_color: border_color.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct Motivation {
|
||||
pub image_url: String,
|
||||
pub action: String,
|
||||
pub goal: String,
|
||||
pub border_color: String,
|
||||
}
|
||||
Reference in New Issue
Block a user