diff options
| author | Nathan Perry <avaglir@gmail.com> | 2019-03-09 17:09:57 -0500 |
|---|---|---|
| committer | Nathan Perry <avaglir@gmail.com> | 2019-03-09 17:09:57 -0500 |
| commit | a541cc28c435f039b977087cf7d07648a160633b (patch) | |
| tree | 57f4148dcd789e865dbf8d81daf07458c88fc07e /src/db | |
| parent | a0aa6c6310b046d27c71cef23dcf6e6811cc11de (diff) | |
add silentmeme command
Diffstat (limited to 'src/db')
| -rw-r--r-- | src/db/mod.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs index b3861d2..191b9e9 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -158,6 +158,27 @@ pub fn rand_audio_meme(conn: &PgConnection) -> Result<Meme> { .map_err(Error::from) } +pub fn rand_silent_meme(conn: &PgConnection) -> Result<Meme> { + use rand::{thread_rng, seq::SliceRandom}; + use failure::err_msg; + use std::ops::Try; + + let ids: Vec<i32> = memes::table + .select(memes::id) + .filter(memes::audio_id.is_null()) + .load(conn) + .map_err(Error::from)?; + + let id = ids.choose(&mut thread_rng()) + .into_result() + .map_err(|_| err_msg("couldn't load audio meme"))?; + + memes::table + .find(id) + .first::<Meme>(conn) + .map_err(Error::from) +} + #[derive(Debug, Copy, Clone)] pub struct Stats { pub memes_overall: usize, |
