cleanup: remove unused state code
This commit is contained in:
parent
9d5ee868e8
commit
fb876608a7
3 changed files with 16 additions and 47 deletions
15
src/db.rs
15
src/db.rs
|
@ -1,5 +1,6 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Duration, Utc};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct CacheRecord {
|
pub struct CacheRecord {
|
||||||
pub cache_name: String,
|
pub cache_name: String,
|
||||||
pub uploaded: DateTime<Utc>,
|
pub uploaded: DateTime<Utc>,
|
||||||
|
@ -9,6 +10,18 @@ pub struct CacheRecord {
|
||||||
pub max_downloads: i32,
|
pub max_downloads: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CacheRecord {
|
||||||
|
pub fn can_be_downloaded(&self) -> bool {
|
||||||
|
let dur_since_upload = Utc::now().signed_duration_since(self.uploaded);
|
||||||
|
|
||||||
|
dur_since_upload < Duration::days(3) && self.downloads < self.max_downloads
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn downloads_remaining(&self) -> i32 {
|
||||||
|
self.max_downloads - self.downloads
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct CacheRecordName {
|
pub struct CacheRecordName {
|
||||||
pub cache_name: String,
|
pub cache_name: String,
|
||||||
}
|
}
|
||||||
|
|
46
src/state.rs
46
src/state.rs
|
@ -1,51 +1,7 @@
|
||||||
use std::{
|
use std::str::FromStr;
|
||||||
path::{Path, PathBuf},
|
|
||||||
str::FromStr,
|
|
||||||
};
|
|
||||||
|
|
||||||
use chrono::{DateTime, Duration, Utc};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use sqlx::{sqlite::SqliteConnectOptions, SqlitePool};
|
use sqlx::{sqlite::SqliteConnectOptions, SqlitePool};
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct UploadRecord {
|
|
||||||
pub uploaded: DateTime<Utc>,
|
|
||||||
pub file: PathBuf,
|
|
||||||
pub downloads: u8,
|
|
||||||
pub max_downloads: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl UploadRecord {
|
|
||||||
pub fn new(file: PathBuf) -> Self {
|
|
||||||
Self {
|
|
||||||
file,
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn can_be_downloaded(&self) -> bool {
|
|
||||||
let dur_since_upload = Utc::now().signed_duration_since(self.uploaded);
|
|
||||||
|
|
||||||
dur_since_upload < Duration::days(3) && self.downloads < self.max_downloads
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn downloads_remaining(&self) -> u8 {
|
|
||||||
self.max_downloads - self.downloads
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for UploadRecord {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
uploaded: Utc::now(),
|
|
||||||
file: Path::new("").to_owned(),
|
|
||||||
downloads: 0,
|
|
||||||
max_downloads: 5,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub pool: SqlitePool,
|
pub pool: SqlitePool,
|
||||||
|
|
|
@ -16,7 +16,7 @@ impl WelcomeTemplate {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_downloads_remaining_text(record: &CacheRecord) -> String {
|
pub fn get_downloads_remaining_text(record: &CacheRecord) -> String {
|
||||||
let downloads_remaining = record.max_downloads - record.downloads;
|
let downloads_remaining = record.downloads_remaining();
|
||||||
let plural = if downloads_remaining > 1 { "s" } else { "" };
|
let plural = if downloads_remaining > 1 { "s" } else { "" };
|
||||||
|
|
||||||
format!(
|
format!(
|
||||||
|
|
Loading…
Reference in a new issue