Skip to main content

things3_core/database/queries/
areas.rs

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