#![allow(deprecated)]
use crate::error::{Result as ThingsResult, ThingsError};
use crate::models::ThingsId;
use sqlx::SqlitePool;
use tracing::instrument;
#[instrument(skip(pool))]
pub async fn validate_task_exists(pool: &SqlitePool, id: &ThingsId) -> ThingsResult<()> {
let exists = sqlx::query("SELECT 1 FROM TMTask WHERE uuid = ? AND trashed = 0")
.bind(id.as_str())
.fetch_optional(pool)
.await
.map_err(|e| ThingsError::unknown(format!("Failed to validate task: {e}")))?
.is_some();
if !exists {
return Err(ThingsError::unknown(format!("Task not found: {id}")));
}
Ok(())
}
#[instrument(skip(pool))]
pub async fn validate_project_exists(pool: &SqlitePool, id: &ThingsId) -> ThingsResult<()> {
let exists = sqlx::query("SELECT 1 FROM TMTask WHERE uuid = ? AND type = 1 AND trashed = 0")
.bind(id.as_str())
.fetch_optional(pool)
.await
.map_err(|e| ThingsError::unknown(format!("Failed to validate project: {e}")))?
.is_some();
if !exists {
return Err(ThingsError::ProjectNotFound {
uuid: id.to_string(),
});
}
Ok(())
}
#[instrument(skip(pool))]
pub async fn validate_area_exists(pool: &SqlitePool, id: &ThingsId) -> ThingsResult<()> {
let exists = sqlx::query("SELECT 1 FROM TMArea WHERE uuid = ?")
.bind(id.as_str())
.fetch_optional(pool)
.await
.map_err(|e| ThingsError::unknown(format!("Failed to validate area: {e}")))?
.is_some();
if !exists {
return Err(ThingsError::unknown(format!("Area not found: {id}")));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "test-utils")]
#[tokio::test]
async fn test_validate_nonexistent_task() {
use crate::test_utils::create_test_database;
use tempfile::NamedTempFile;
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path();
create_test_database(db_path).await.unwrap();
let pool = sqlx::SqlitePool::connect(&format!("sqlite://{}", db_path.display()))
.await
.unwrap();
let id = ThingsId::new_v4();
let result = validate_task_exists(&pool, &id).await;
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Task not found"));
}
#[cfg(feature = "test-utils")]
#[tokio::test]
async fn test_validate_nonexistent_project() {
use crate::test_utils::create_test_database;
use tempfile::NamedTempFile;
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path();
create_test_database(db_path).await.unwrap();
let pool = sqlx::SqlitePool::connect(&format!("sqlite://{}", db_path.display()))
.await
.unwrap();
let id = ThingsId::new_v4();
let result = validate_project_exists(&pool, &id).await;
assert!(result.is_err());
assert!(result
.unwrap_err()
.to_string()
.contains("Project not found"));
}
#[cfg(feature = "test-utils")]
#[tokio::test]
async fn test_validate_nonexistent_area() {
use crate::test_utils::create_test_database;
use tempfile::NamedTempFile;
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path();
create_test_database(db_path).await.unwrap();
let pool = sqlx::SqlitePool::connect(&format!("sqlite://{}", db_path.display()))
.await
.unwrap();
let id = ThingsId::new_v4();
let result = validate_area_exists(&pool, &id).await;
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Area not found"));
}
}