use crate::error::DatabaseError;
use crate::{Error, Result};
use super::{Database, Download};
impl Database {
pub async fn find_by_nzb_hash(&self, nzb_hash: &str) -> Result<Option<Download>> {
let row = sqlx::query_as::<_, Download>(
r#"
SELECT
id, name, nzb_path, nzb_meta_name, nzb_hash, job_name,
category, destination, post_process, priority, status,
progress, speed_bps, size_bytes, downloaded_bytes,
error_message, created_at, started_at, completed_at,
direct_unpack_state, direct_unpack_extracted_count
FROM downloads
WHERE nzb_hash = ?
LIMIT 1
"#,
)
.bind(nzb_hash)
.fetch_optional(&self.pool)
.await
.map_err(|e| {
Error::Database(DatabaseError::QueryFailed(format!(
"Failed to find download by nzb_hash: {}",
e
)))
})?;
Ok(row)
}
pub async fn find_by_name(&self, name: &str) -> Result<Option<Download>> {
let row = sqlx::query_as::<_, Download>(
r#"
SELECT
id, name, nzb_path, nzb_meta_name, nzb_hash, job_name,
category, destination, post_process, priority, status,
progress, speed_bps, size_bytes, downloaded_bytes,
error_message, created_at, started_at, completed_at,
direct_unpack_state, direct_unpack_extracted_count
FROM downloads
WHERE name = ?
LIMIT 1
"#,
)
.bind(name)
.fetch_optional(&self.pool)
.await
.map_err(|e| {
Error::Database(DatabaseError::QueryFailed(format!(
"Failed to find download by name: {}",
e
)))
})?;
Ok(row)
}
pub async fn find_by_job_name(&self, job_name: &str) -> Result<Option<Download>> {
let row = sqlx::query_as::<_, Download>(
r#"
SELECT
id, name, nzb_path, nzb_meta_name, nzb_hash, job_name,
category, destination, post_process, priority, status,
progress, speed_bps, size_bytes, downloaded_bytes,
error_message, created_at, started_at, completed_at,
direct_unpack_state, direct_unpack_extracted_count
FROM downloads
WHERE job_name = ?
LIMIT 1
"#,
)
.bind(job_name)
.fetch_optional(&self.pool)
.await
.map_err(|e| {
Error::Database(DatabaseError::QueryFailed(format!(
"Failed to find download by job_name: {}",
e
)))
})?;
Ok(row)
}
}