Compare commits

..

4 commits

Author SHA1 Message Date
54280087dc feat: use db in LinkList 2024-11-21 06:45:08 -08:00
84c3513987 dev: sqlfluff ignore comment line length 2024-11-20 23:46:06 -08:00
fc625b50d7 dev: ignore special chars sqlfluff rule
inferred type columns with sqlx require aliases containing spaces and
special characters

See:
https://docs.rs/sqlx/latest/sqlx/macro.query_as.html#column-type-override-infer-from-struct-field
2024-11-20 23:43:33 -08:00
805723f10d add db structs 2024-11-20 23:34:22 -08:00
8 changed files with 54 additions and 7 deletions

View file

@ -1,5 +1,6 @@
[sqlfluff]
dialect = sqlite
exclude_rules = references.special_chars
[sqlfluff:rules:capitalisation.keywords]
# Keywords
@ -12,3 +13,6 @@ align_within = create_table_statement
[sqlfluff:layout:type:data_type]
spacing_before = align
align_within = create_table_statement
[sqlfluff:rules:layout.long_lines]
ignore_comment_lines = True

View 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;

View 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 ?

21
src/db.rs Normal file
View file

@ -0,0 +1,21 @@
use chrono::{DateTime, Utc};
#[derive(sqlx::Type)]
pub struct CacheRecord {
pub cache_name: String,
pub uploaded: DateTime<Utc>,
// This uses i32 because of how sqlx decodes unsigned integers to sqlite
// See: https://docs.rs/sqlx/latest/sqlx/sqlite/types/index.html
pub downloads: i32,
pub max_downloads: i32,
}
pub struct CacheRecordName {
pub cache_name: String,
}
impl From<CacheRecordName> for String {
fn from(value: CacheRecordName) -> Self {
value.cache_name
}
}

View file

@ -1,4 +1,5 @@
pub mod cache;
pub mod db;
pub mod router;
pub mod state;
pub mod util;

View file

@ -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<AppState> {
// 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 behind some kind of authentication
pub async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
let records = state.records.lock().await.clone();
pub async fn records_links(
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();
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 })
}

View file

@ -45,5 +45,5 @@ impl DownloadLinkFragment {
#[derive(Template)]
#[template(path = "linklist.html")]
pub struct LinkListTemplate {
pub record_keys: Vec<String>,
pub cache_names: Vec<String>,
}

View file

@ -4,7 +4,7 @@
<div class="form-wrapper">
<div class="column-container">
<ul>
{% for key in record_keys %}
{% for key in cache_names %}
<li class="link-wrapper">
<a href="/link/{{ key }}">{{ key }}</a>
<button