refactor: remove code duplication
This commit is contained in:
parent
54280087dc
commit
7db8ac69c9
3 changed files with 14 additions and 27 deletions
|
@ -51,23 +51,13 @@ async fn link_delete(
|
||||||
Ok(Html("".to_string()))
|
Ok(Html("".to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn get_remaining_text(downloads_remaining: u8) -> String {
|
|
||||||
let plural = if downloads_remaining > 1 { "s" } else { "" };
|
|
||||||
format!(
|
|
||||||
"You have {} download{} remaining!",
|
|
||||||
downloads_remaining, plural
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn remaining(
|
async fn remaining(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
axum::extract::Path(id): axum::extract::Path<String>,
|
axum::extract::Path(id): axum::extract::Path<String>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
let records = state.records.lock().await;
|
let records = state.records.lock().await;
|
||||||
if let Some(record) = records.get(&id) {
|
if let Some(record) = records.get(&id) {
|
||||||
let downloads_remaining = record.downloads_remaining();
|
Html(crate::templates::get_downloads_remaining_text(record))
|
||||||
Html(get_remaining_text(downloads_remaining))
|
|
||||||
} else {
|
} else {
|
||||||
Html("?".to_string())
|
Html("?".to_string())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use rinja_axum::Template;
|
use rinja_axum::Template;
|
||||||
|
|
||||||
use crate::{link::get_remaining_text, UploadRecord};
|
use crate::UploadRecord;
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "welcome.html")]
|
#[template(path = "welcome.html")]
|
||||||
|
@ -14,6 +14,17 @@ impl WelcomeTemplate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn get_downloads_remaining_text(record: &UploadRecord) -> String {
|
||||||
|
let downloads_remaining = record.max_downloads - record.downloads;
|
||||||
|
let plural = if downloads_remaining > 1 { "s" } else { "" };
|
||||||
|
|
||||||
|
format!(
|
||||||
|
"You have {} download{} remaining!",
|
||||||
|
downloads_remaining, plural
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "link.html")]
|
#[template(path = "link.html")]
|
||||||
pub struct DownloadLinkTemplate {
|
pub struct DownloadLinkTemplate {
|
||||||
|
@ -21,13 +32,6 @@ pub struct DownloadLinkTemplate {
|
||||||
pub record: UploadRecord,
|
pub record: UploadRecord,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DownloadLinkTemplate {
|
|
||||||
fn get_downloads_remaining_text(record: &UploadRecord) -> String {
|
|
||||||
let downloads_remaining = record.max_downloads - record.downloads;
|
|
||||||
get_remaining_text(downloads_remaining)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "link.html", block = "content")]
|
#[template(path = "link.html", block = "content")]
|
||||||
pub struct DownloadLinkFragment {
|
pub struct DownloadLinkFragment {
|
||||||
|
@ -35,13 +39,6 @@ pub struct DownloadLinkFragment {
|
||||||
pub record: UploadRecord,
|
pub record: UploadRecord,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DownloadLinkFragment {
|
|
||||||
fn get_downloads_remaining_text(record: &UploadRecord) -> String {
|
|
||||||
let downloads_remaining = record.max_downloads - record.downloads;
|
|
||||||
get_remaining_text(downloads_remaining)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "linklist.html")]
|
#[template(path = "linklist.html")]
|
||||||
pub struct LinkListTemplate {
|
pub struct LinkListTemplate {
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
hx-get="/link/{{ id }}/remaining"
|
hx-get="/link/{{ id }}/remaining"
|
||||||
hx-trigger="click from:#link delay:0.2s, every 10s"
|
hx-trigger="click from:#link delay:0.2s, every 10s"
|
||||||
>
|
>
|
||||||
{{ Self::get_downloads_remaining_text(record) }}
|
{{ self::get_downloads_remaining_text(record) }}
|
||||||
</div>
|
</div>
|
||||||
<button class="return-button" onclick="clipboard()">
|
<button class="return-button" onclick="clipboard()">
|
||||||
Copy to Clipboard
|
Copy to Clipboard
|
||||||
|
|
Loading…
Reference in a new issue