From 54280087dc33f8de05330b61d78375757213f0df Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Thu, 21 Nov 2024 06:45:08 -0800 Subject: [PATCH] feat: use db in LinkList --- queries/records/get_cache_names.sql | 4 ++++ queries/records/get_cache_names_page.sql | 5 +++++ src/db.rs | 15 +++++++++++---- src/router/records.rs | 22 +++++++++++++++++----- src/views/templates.rs | 2 +- templates/linklist.html | 2 +- 6 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 queries/records/get_cache_names.sql create mode 100644 queries/records/get_cache_names_page.sql diff --git a/queries/records/get_cache_names.sql b/queries/records/get_cache_names.sql new file mode 100644 index 0000000..892c5f7 --- /dev/null +++ b/queries/records/get_cache_names.sql @@ -0,0 +1,4 @@ +-- The funny alias used here is to allow sqlx to do type inference on columns +-- See: https://docs.rs/sqlx/latest/sqlx/macro.query_as.html#column-type-override-infer-from-struct-field +SELECT cache_name AS "cache_name: _" +FROM records; diff --git a/queries/records/get_cache_names_page.sql b/queries/records/get_cache_names_page.sql new file mode 100644 index 0000000..b869257 --- /dev/null +++ b/queries/records/get_cache_names_page.sql @@ -0,0 +1,5 @@ +-- The funny alias used here is to allow sqlx to do type inference on columns +-- See: https://docs.rs/sqlx/latest/sqlx/macro.query_as.html#column-type-override-infer-from-struct-field +SELECT cache_name AS "cache_name: _" +FROM records +LIMIT ? OFFSET ? diff --git a/src/db.rs b/src/db.rs index 1229eab..c27b529 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,7 +1,8 @@ use chrono::{DateTime, Utc}; +#[derive(sqlx::Type)] pub struct CacheRecord { - pub cache_name: CacheName, + pub cache_name: String, pub uploaded: DateTime, // This uses i32 because of how sqlx decodes unsigned integers to sqlite // See: https://docs.rs/sqlx/latest/sqlx/sqlite/types/index.html @@ -9,6 +10,12 @@ pub struct CacheRecord { pub max_downloads: i32, } -#[derive(sqlx::Type)] -#[sqlx(transparent)] -pub struct CacheName(i32); +pub struct CacheRecordName { + pub cache_name: String, +} + +impl From for String { + fn from(value: CacheRecordName) -> Self { + value.cache_name + } +} diff --git a/src/router/records.rs b/src/router/records.rs index cebc18e..68f92f2 100644 --- a/src/router/records.rs +++ b/src/router/records.rs @@ -1,6 +1,7 @@ use axum::{extract::State, response::IntoResponse, routing::get, Json, Router}; +use reqwest::StatusCode; -use crate::{templates::LinkListTemplate, AppState}; +use crate::{db::CacheRecordName, templates::LinkListTemplate, AppState}; pub fn get_records_router() -> Router { // Records views @@ -15,9 +16,20 @@ pub(crate) async fn records(State(state): State) -> impl IntoResponse // This function is to remain ugly until that time in which I properly hide // this behind some kind of authentication -pub async fn records_links(State(state): State) -> impl IntoResponse { - let records = state.records.lock().await.clone(); +pub async fn records_links( + State(state): State, +) -> Result { + let cache_names = { + let mut conn = state.pool.acquire().await.unwrap(); - let record_keys: Vec = records.keys().cloned().collect(); - LinkListTemplate { record_keys } + sqlx::query_file_as!(CacheRecordName, "queries/records/get_cache_names.sql") + .fetch_all(&mut *conn) + .await + .map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))? + .into_iter() + .map(|name| name.into()) + .collect() + }; + + Ok(LinkListTemplate { cache_names }) } diff --git a/src/views/templates.rs b/src/views/templates.rs index 4f4be69..945c1eb 100644 --- a/src/views/templates.rs +++ b/src/views/templates.rs @@ -45,5 +45,5 @@ impl DownloadLinkFragment { #[derive(Template)] #[template(path = "linklist.html")] pub struct LinkListTemplate { - pub record_keys: Vec, + pub cache_names: Vec, } diff --git a/templates/linklist.html b/templates/linklist.html index 1059c3a..4e6557b 100644 --- a/templates/linklist.html +++ b/templates/linklist.html @@ -4,7 +4,7 @@
    - {% for key in record_keys %} + {% for key in cache_names %}