feat: use db in LinkList
This commit is contained in:
parent
84c3513987
commit
54280087dc
6 changed files with 39 additions and 11 deletions
4
queries/records/get_cache_names.sql
Normal file
4
queries/records/get_cache_names.sql
Normal file
|
@ -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;
|
5
queries/records/get_cache_names_page.sql
Normal file
5
queries/records/get_cache_names_page.sql
Normal file
|
@ -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 ?
|
15
src/db.rs
15
src/db.rs
|
@ -1,7 +1,8 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
|
||||||
|
#[derive(sqlx::Type)]
|
||||||
pub struct CacheRecord {
|
pub struct CacheRecord {
|
||||||
pub cache_name: CacheName,
|
pub cache_name: String,
|
||||||
pub uploaded: DateTime<Utc>,
|
pub uploaded: DateTime<Utc>,
|
||||||
// This uses i32 because of how sqlx decodes unsigned integers to sqlite
|
// This uses i32 because of how sqlx decodes unsigned integers to sqlite
|
||||||
// See: https://docs.rs/sqlx/latest/sqlx/sqlite/types/index.html
|
// See: https://docs.rs/sqlx/latest/sqlx/sqlite/types/index.html
|
||||||
|
@ -9,6 +10,12 @@ pub struct CacheRecord {
|
||||||
pub max_downloads: i32,
|
pub max_downloads: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(sqlx::Type)]
|
pub struct CacheRecordName {
|
||||||
#[sqlx(transparent)]
|
pub cache_name: String,
|
||||||
pub struct CacheName(i32);
|
}
|
||||||
|
|
||||||
|
impl From<CacheRecordName> for String {
|
||||||
|
fn from(value: CacheRecordName) -> Self {
|
||||||
|
value.cache_name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};
|
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<AppState> {
|
pub fn get_records_router() -> Router<AppState> {
|
||||||
// Records views
|
// Records views
|
||||||
|
@ -15,9 +16,20 @@ pub(crate) async fn records(State(state): State<AppState>) -> impl IntoResponse
|
||||||
|
|
||||||
// This function is to remain ugly until that time in which I properly hide
|
// This function is to remain ugly until that time in which I properly hide
|
||||||
// this behind some kind of authentication
|
// this behind some kind of authentication
|
||||||
pub async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
|
pub async fn records_links(
|
||||||
let records = state.records.lock().await.clone();
|
State(state): State<AppState>,
|
||||||
|
) -> Result<impl IntoResponse, (StatusCode, String)> {
|
||||||
|
let cache_names = {
|
||||||
|
let mut conn = state.pool.acquire().await.unwrap();
|
||||||
|
|
||||||
let record_keys: Vec<String> = records.keys().cloned().collect();
|
sqlx::query_file_as!(CacheRecordName, "queries/records/get_cache_names.sql")
|
||||||
LinkListTemplate { record_keys }
|
.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 })
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,5 +45,5 @@ impl DownloadLinkFragment {
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "linklist.html")]
|
#[template(path = "linklist.html")]
|
||||||
pub struct LinkListTemplate {
|
pub struct LinkListTemplate {
|
||||||
pub record_keys: Vec<String>,
|
pub cache_names: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
<div class="form-wrapper">
|
<div class="form-wrapper">
|
||||||
<div class="column-container">
|
<div class="column-container">
|
||||||
<ul>
|
<ul>
|
||||||
{% for key in record_keys %}
|
{% for key in cache_names %}
|
||||||
<li class="link-wrapper">
|
<li class="link-wrapper">
|
||||||
<a href="/link/{{ key }}">{{ key }}</a>
|
<a href="/link/{{ key }}">{{ key }}</a>
|
||||||
<button
|
<button
|
||||||
|
|
Loading…
Reference in a new issue