/correctly/ implement get_old_files fn

main
Volkor 2023-01-19 00:17:17 +11:00
parent 1060dd9b0c
commit 8873b96e28
Signed by: Volkor
GPG Key ID: BAD7CA8A81CC2DA5
2 changed files with 25 additions and 10 deletions

1
.env Normal file
View File

@ -0,0 +1 @@
DATABASE_URL=sqlite://ephemeral.db

View File

@ -20,6 +20,7 @@ pub async fn create_filesdb(sqlconn: &Pool<Sqlite>) -> Result<SqliteQueryResult,
)
.execute(sqlconn)
.await
// TODO: Check for row affected, and give a Result
}
pub async fn create_qrscandb(sqlconn: &Pool<Sqlite>) -> Result<SqliteQueryResult, sqlx::Error> {
@ -35,6 +36,7 @@ pub async fn create_qrscandb(sqlconn: &Pool<Sqlite>) -> Result<SqliteQueryResult
)
.execute(sqlconn)
.await
// TODO: Check for row affected, and give a Result
}
// Adding a file to the database
@ -104,6 +106,8 @@ pub async fn add_file(
result
}
// Will need to add another else if for expiry_override if added later.
// TODO: Check for row affected, and give a Result
}
// This function checks if the filename actually exists in the DB, returning true if it does, false otherwise.
@ -160,6 +164,8 @@ pub async fn delete_file(sqlconn: &Pool<Sqlite>, filename: String) -> SqliteQuer
.execute(sqlconn)
.await;
result.unwrap()
// TODO: Check for row affected, and give a Result
}
// Updating a files viewcount
@ -174,6 +180,8 @@ pub async fn update_fileview(
.execute(sqlconn)
.await
.unwrap()
// TODO: Check for row affected, and give a Result
}
// Returns the unix timestamp of the last access - 0 if unviewed.
@ -207,22 +215,28 @@ pub async fn get_old_files(sqlconn: &Pool<Sqlite>) -> Vec<String> {
.unwrap()
.as_secs() as i32;
// Query for files that expiry or expiry_override is over the current time.
let result = sqlx::query("SELECT file FROM files WHERE (expiry_override <= ? AND expiry_override NOT NULL) OR (expiry <= ?) AND isDeleted = 0")
.bind(time)
.fetch_one(sqlconn)
// Query for files where the expiry has passed (or the expiry_override has passed, if set.)
// Also only returns files that haven't been deleted yet.
let result = sqlx::query!(
"SELECT file FROM files WHERE (expiry_override <= ? AND expiry_override NOT NULL) OR (expiry <= ?) AND isDeleted = 0",
time,
time
)
.fetch_all(sqlconn)
.await;
// TODO: We should probably handle /all/ the errors, but idk how.
let mut files: Vec<String> = Vec::new();
if result.is_err() {
// If result is an error, very likely there was no files to delete, let's log and return no files!
let files: Vec<String> = Vec::new();
tracing::warn!("Error when querying DB for old files.");
// If there was an error, give up and return nothing. Better to not delete any files.
tracing::error!("Error when querying DB for old files. Giving up!");
return files;
}
// Setup the files vec
let files: Vec<String> = Vec::new();
// Parse the result into the vec.
for line in result.unwrap() {
tracing::debug!("oldfile_loop(filename: {})", line.file);
files.push(line.file);
}
// Print debug and return
tracing::debug!("get_old_files(files: {:?})", files);
files