From 299f9813bbae50dfdecccd6c8ecdaeaa9b4f4ec6 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Thu, 21 Feb 2019 00:20:23 -0500 Subject: audiomeme command --- src/db/mod.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/db/mod.rs') 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 { .map_err(Error::from) } +pub fn rand_audio_meme(conn: &PgConnection) -> Result { + use rand::{thread_rng, seq::SliceRandom}; + use failure::err_msg; + use std::ops::Try; + + let ids: Vec = 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::(conn) + .map_err(Error::from) +} + no_arg_sql_function!(random, sql_types::Double, "SQL random() function"); -- cgit v1.3.1