-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
174 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE users; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE users ( | ||
id INTEGER PRIMARY KEY NOT NULL, | ||
refresh_token TEXT NOT NULL, | ||
access_token TEXT NOT NULL, | ||
expires_at INTEGER NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use diesel::prelude::*; | ||
use diesel::{Connection, SqliteConnection}; | ||
use std::env; | ||
|
||
use crate::models::UserDb; | ||
use crate::{schema, strava}; | ||
|
||
fn establish_connection() -> SqliteConnection { | ||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); | ||
SqliteConnection::establish(&database_url) | ||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) | ||
} | ||
|
||
pub fn save_user(t: &strava::TokenResponse) { | ||
let user = UserDb { | ||
id: t.athlete.id, | ||
refresh_token: t.refresh_token.clone(), | ||
access_token: t.access_token.clone(), | ||
expires_at: t.expires_at, | ||
}; | ||
|
||
let conn = &mut establish_connection(); | ||
diesel::insert_into(schema::users::table) | ||
.values(&user) | ||
.execute(conn) | ||
.expect("Error saving new post"); | ||
} | ||
|
||
pub fn get_user(user_id: i32) -> Option<UserDb> { | ||
use self::schema::users::dsl::*; | ||
let conn = &mut establish_connection(); | ||
users | ||
.find(user_id) | ||
.select(UserDb::as_select()) | ||
.first(conn).ok() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// @generated automatically by Diesel CLI. | ||
|
||
diesel::table! { | ||
users (id) { | ||
id -> Integer, | ||
refresh_token -> Text, | ||
access_token -> Text, | ||
expires_at -> Integer, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters