dev api key
This commit is contained in:
parent
06c837b244
commit
89091a03d3
11 changed files with 791 additions and 762 deletions
41
src/db.rs
41
src/db.rs
|
|
@ -12,11 +12,7 @@ pub fn hash_token(token: &str) -> Vec<u8> {
|
|||
hasher.finalize().to_vec()
|
||||
}
|
||||
|
||||
pub async fn create_session(
|
||||
db: &PgPool,
|
||||
athlete_id: i64,
|
||||
token: &str,
|
||||
) -> Result<()> {
|
||||
pub async fn create_session(db: &PgPool, athlete_id: i64, token: &str) -> Result<()> {
|
||||
let hash = hash_token(token);
|
||||
|
||||
sqlx::query(
|
||||
|
|
@ -28,7 +24,7 @@ pub async fn create_session(
|
|||
VALUES ($1, $2)
|
||||
"#,
|
||||
)
|
||||
.bind(hash)
|
||||
.bind(&hash)
|
||||
.bind(athlete_id)
|
||||
.execute(db)
|
||||
.await?;
|
||||
|
|
@ -36,10 +32,7 @@ pub async fn create_session(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn athlete_for_session(
|
||||
db: &PgPool,
|
||||
token: &str,
|
||||
) -> Result<Option<(i64, String)>> {
|
||||
pub async fn athlete_for_session(db: &PgPool, token: &str) -> Result<Option<(i64, String)>> {
|
||||
let hash = hash_token(token);
|
||||
|
||||
let row = sqlx::query(
|
||||
|
|
@ -53,7 +46,7 @@ pub async fn athlete_for_session(
|
|||
WHERE s.token_hash = $1
|
||||
"#,
|
||||
)
|
||||
.bind(hash)
|
||||
.bind(&hash)
|
||||
.fetch_optional(db)
|
||||
.await?;
|
||||
|
||||
|
|
@ -64,9 +57,13 @@ pub async fn athlete_for_session(
|
|||
let name: String = row.try_get("display_name")?;
|
||||
|
||||
sqlx::query(
|
||||
"UPDATE sessions SET last_seen_at = now() WHERE token_hash = $1",
|
||||
r#"
|
||||
UPDATE sessions
|
||||
SET last_seen_at = now()
|
||||
WHERE token_hash = $1
|
||||
"#,
|
||||
)
|
||||
.bind(hash)
|
||||
.bind(&hash)
|
||||
.execute(db)
|
||||
.await?;
|
||||
|
||||
|
|
@ -76,11 +73,12 @@ pub async fn athlete_for_session(
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn delete_old_oauth_states(
|
||||
db: &PgPool,
|
||||
) -> Result<()> {
|
||||
pub async fn delete_old_oauth_states(db: &PgPool) -> Result<()> {
|
||||
sqlx::query(
|
||||
"DELETE FROM oauth_states WHERE created_at < now() - interval '10 minutes'",
|
||||
r#"
|
||||
DELETE FROM oauth_states
|
||||
WHERE created_at < now() - interval '10 minutes'
|
||||
"#,
|
||||
)
|
||||
.execute(db)
|
||||
.await?;
|
||||
|
|
@ -105,7 +103,6 @@ pub async fn ensure_activity(
|
|||
processed_at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, NULL)
|
||||
|
||||
ON CONFLICT (
|
||||
athlete_id,
|
||||
intervals_activity_id
|
||||
|
|
@ -114,7 +111,6 @@ pub async fn ensure_activity(
|
|||
start_time = EXCLUDED.start_time,
|
||||
activity_json = EXCLUDED.activity_json,
|
||||
processed_at = NULL
|
||||
|
||||
RETURNING id
|
||||
"#,
|
||||
)
|
||||
|
|
@ -135,7 +131,10 @@ pub async fn delete_activity_crossings(
|
|||
activity_id: i64,
|
||||
) -> Result<()> {
|
||||
sqlx::query(
|
||||
"DELETE FROM county_crossings WHERE activity_id = $1",
|
||||
r#"
|
||||
DELETE FROM county_crossings
|
||||
WHERE activity_id = $1
|
||||
"#,
|
||||
)
|
||||
.bind(activity_id)
|
||||
.execute(&mut **tx)
|
||||
|
|
@ -165,12 +164,10 @@ pub async fn save_crossing(
|
|||
$1,
|
||||
$2,
|
||||
$3,
|
||||
|
||||
ST_SetSRID(
|
||||
ST_MakePoint($4, $5),
|
||||
4326
|
||||
),
|
||||
|
||||
$6,
|
||||
$7,
|
||||
$8
|
||||
|
|
|
|||
Loading…
Reference in a new issue