This commit is contained in:
Joe Ardent 2024-03-28 22:21:16 -07:00
parent aad60a5752
commit e96a993f9a
1 changed files with 17 additions and 11 deletions

View File

@ -51,6 +51,11 @@ async fn main() {
pool.close().await;
}
//-************************************************************************
// the two route hanlders
//-************************************************************************
/// This is the main handler. It counts the hit and returns the latest count.
#[debug_handler]
async fn register_hit(
Path(slug): Path<String>,
@ -91,6 +96,7 @@ async fn register_hit(
format!("{hits}")
}
/// fer fancy people what want to be a little finer
#[axum::debug_handler]
async fn get_hits(
State(db): State<SqlitePool>,
@ -120,17 +126,6 @@ async fn get_hits(
format!("{count}")
}
async fn get_period_hits(db: &SqlitePool, page: &str, when: &str) -> i32 {
sqlx::query_scalar!(
"select count(*) from hits where page = ? and accessed > ?",
page,
when
)
.fetch_one(db)
.await
.unwrap_or(0)
}
//-************************************************************************
// li'l helpers
//-************************************************************************
@ -226,3 +221,14 @@ fn shasum(input: &[u8]) -> Vec<u8> {
context.update(input);
context.finish().as_ref().to_vec()
}
async fn get_period_hits(db: &SqlitePool, page: &str, when: &str) -> i32 {
sqlx::query_scalar!(
"select count(*) from hits where page = ? and accessed > ?",
page,
when
)
.fetch_one(db)
.await
.unwrap_or(0)
}