Compare commits
4 commits
66a59df6ab
...
54280087dc
Author | SHA1 | Date | |
---|---|---|---|
54280087dc | |||
84c3513987 | |||
fc625b50d7 | |||
805723f10d |
8 changed files with 54 additions and 7 deletions
|
@ -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
|
||||
|
|
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 ?
|
21
src/db.rs
Normal file
21
src/db.rs
Normal 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
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
pub mod cache;
|
||||
pub mod db;
|
||||
pub mod router;
|
||||
pub mod state;
|
||||
pub mod util;
|
||||
|
|
|
@ -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 })
|
||||
}
|
||||
|
|
|
@ -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>,
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue