aboutsummaryrefslogtreecommitdiff
path: root/src/db
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2019-03-08 16:57:27 -0500
committerNathan Perry <avaglir@gmail.com>2019-03-08 16:57:27 -0500
commit86025df1f6d814c98a14211ceb4da6cf6de915c7 (patch)
treec615b6634eea05498d772f68aec2d52fa3ca4f01 /src/db
parent24a7cb3c8eb0517b69c145170765c891a82ccc76 (diff)
first pass with oauth
Diffstat (limited to 'src/db')
-rw-r--r--src/db/models.rs43
-rw-r--r--src/db/schema.rs11
2 files changed, 54 insertions, 0 deletions
diff --git a/src/db/models.rs b/src/db/models.rs
index 66e161a..030daba 100644
--- a/src/db/models.rs
+++ b/src/db/models.rs
@@ -284,3 +284,46 @@ impl InvocationRecord {
.map_err(Error::from)
}
}
+
+#[derive(Queryable, Identifiable, PartialEq, Debug)]
+#[table_name="google_oauth_tokens"]
+pub struct GoogleOAuthToken {
+ pub id: i32,
+ pub token: String,
+ pub refresh_token: String,
+ pub expiration: NaiveDateTime,
+ pub created: NaiveDateTime,
+}
+
+impl GoogleOAuthToken {
+ pub fn create(conn: &PgConnection, token: String, refresh_token: String, expiration: NaiveDateTime) -> Result<Self> {
+ ::diesel::insert_into(google_oauth_tokens::table)
+ .values(&NewGoogleOAuthToken {
+ token,
+ refresh_token,
+ expiration,
+ })
+ .get_result::<GoogleOAuthToken>(conn)
+ .map_err(Error::from)
+ }
+
+ pub fn latest(conn: &PgConnection) -> Result<Self> {
+ use chrono;
+
+ let now = chrono::Utc::now().naive_utc();
+
+ google_oauth_tokens::table
+ .filter(google_oauth_tokens::expiration.gt(now))
+ .order(google_oauth_tokens::created.desc())
+ .first(conn)
+ .map_err(Error::from)
+ }
+}
+
+#[derive(Insertable, PartialEq, Debug)]
+#[table_name="google_oauth_tokens"]
+pub struct NewGoogleOAuthToken {
+ pub token: String,
+ pub refresh_token: String,
+ pub expiration: NaiveDateTime,
+}
diff --git a/src/db/schema.rs b/src/db/schema.rs
index 01ec1d0..9a67cec 100644
--- a/src/db/schema.rs
+++ b/src/db/schema.rs
@@ -17,6 +17,16 @@ table! {
}
table! {
+ google_oauth_tokens (id) {
+ id -> Int4,
+ token -> Varchar,
+ refresh_token -> Varchar,
+ expiration -> Timestamp,
+ created -> Timestamp,
+ }
+}
+
+table! {
images (id) {
id -> Int4,
data -> Bytea,
@@ -78,6 +88,7 @@ joinable!(invocation_records -> memes (meme_id));
allow_tables_to_appear_in_same_query!(
audio,
audit_records,
+ google_oauth_tokens,
images,
invocation_records,
memes,