Skip to main content

things3_core/database/queries/
areas.rs

1#![allow(deprecated)]
2
3use crate::{
4    database::ThingsDatabase,
5    error::{Result as ThingsResult, ThingsError},
6    models::{Area, ThingsId},
7};
8use chrono::Utc;
9use sqlx::Row;
10use tracing::{debug, instrument};
11
12impl ThingsDatabase {
13    /// Get all areas
14    ///
15    /// # Errors
16    ///
17    /// Returns an error if the database query fails or if area data is invalid
18    #[instrument]
19    pub async fn get_all_areas(&self) -> ThingsResult<Vec<Area>> {
20        // Get all areas, not just visible ones (MCP clients may want to see all)
21        let rows = sqlx::query(
22            r"
23            SELECT 
24                uuid, title, visible, `index`
25             FROM TMArea 
26            ORDER BY `index` ASC
27            ",
28        )
29        .fetch_all(&self.pool)
30        .await
31        .map_err(|e| ThingsError::unknown(format!("Failed to fetch areas: {e}")))?;
32
33        let mut areas = Vec::new();
34        for row in rows {
35            let uuid_str: String = row.get("uuid");
36            let area = Area {
37                uuid: ThingsId::from_trusted(uuid_str),
38                title: row.get("title"),
39                notes: None,          // Notes not stored in TMArea table
40                projects: Vec::new(), // TODO: Load projects separately
41                tags: Vec::new(),     // TODO: Load tags separately
42                created: Utc::now(),  // Creation date not available in TMArea
43                modified: Utc::now(), // Modification date not available in TMArea
44            };
45            areas.push(area);
46        }
47
48        debug!("Fetched {} areas", areas.len());
49        Ok(areas)
50    }
51
52    /// Get all areas (alias for `get_all_areas` for compatibility)
53    ///
54    /// # Errors
55    ///
56    /// Returns an error if the database query fails or if area data is invalid
57    #[instrument(skip(self))]
58    pub async fn get_areas(&self) -> ThingsResult<Vec<Area>> {
59        self.get_all_areas().await
60    }
61}