aboutsummaryrefslogtreecommitdiff
path: root/src/db/models.rs
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2019-03-03 02:39:24 -0500
committerNathan Perry <avaglir@gmail.com>2019-03-03 02:39:24 -0500
commit83a06bfbd18e4f539b8e42e8e65d719040863100 (patch)
tree93e93db99ee97b75672f714e48fdfd3ce53bde92 /src/db/models.rs
parent410596f875111fab3d666a472c61be8869d7f7ec (diff)
record meme invocations
Diffstat (limited to 'src/db/models.rs')
-rw-r--r--src/db/models.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/db/models.rs b/src/db/models.rs
index 67aa1ca..66e161a 100644
--- a/src/db/models.rs
+++ b/src/db/models.rs
@@ -235,3 +235,52 @@ pub struct NewTombstone {
pub metadata_id: i32,
pub meme_id: i32,
}
+
+#[derive(Queryable, Identifiable, PartialEq, Debug)]
+#[table_name="invocation_records"]
+pub struct InvocationRecord {
+ pub id: i32,
+ pub user_id: i64,
+ pub message_id: i64,
+ pub meme_id: i32,
+ pub time: NaiveDateTime,
+ pub random: bool,
+}
+
+#[derive(Insertable, PartialEq, Debug)]
+#[table_name="invocation_records"]
+pub struct NewInvocationRecord {
+ pub user_id: i64,
+ pub message_id: i64,
+ pub meme_id: i32,
+ pub random: bool,
+}
+
+impl InvocationRecord {
+ pub fn create(conn: &PgConnection, user_id: u64, message_id: u64, meme_id: i32, random: bool) -> Result<Self> {
+ ::diesel::insert_into(invocation_records::table)
+ .values(&NewInvocationRecord {
+ user_id: user_id as i64,
+ message_id: message_id as i64,
+ meme_id,
+ random,
+ })
+ .get_result::<InvocationRecord>(conn)
+ .map_err(Error::from)
+ }
+
+ pub fn last(conn: &PgConnection) -> Result<Self> {
+ invocation_records::table
+ .order(invocation_records::time.desc())
+ .first(conn)
+ .map_err(Error::from)
+ }
+
+ pub fn last_n(conn: &PgConnection, n: usize) -> Result<Vec<Self>> {
+ invocation_records::table
+ .order(invocation_records::time.desc())
+ .limit(n as i64)
+ .load(conn)
+ .map_err(Error::from)
+ }
+}