diff options
| author | Nathan Perry <avaglir@gmail.com> | 2019-02-21 00:20:23 -0500 |
|---|---|---|
| committer | Nathan Perry <avaglir@gmail.com> | 2019-02-21 00:20:23 -0500 |
| commit | 299f9813bbae50dfdecccd6c8ecdaeaa9b4f4ec6 (patch) | |
| tree | 6b2f15059c9773a22fc19f805f8d9a772750b500 /src/db | |
| parent | 2f0af295cd75d64943edb4f1af0ebcd950297893 (diff) | |
audiomeme 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 1c2a032..00b2138 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -134,4 +134,25 @@ pub fn rand_meme(conn: &PgConnection, audio: bool) -> Result<Meme> { .map_err(Error::from) } +pub fn rand_audio_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_not_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) +} + no_arg_sql_function!(random, sql_types::Double, "SQL random() function"); |
