horizon-sdk 9.0.0

Canonical Rust data access layer for the Horizon platform
Documentation
use chrono::Utc;
use uuid::Uuid;

use crate::types::error::{HorizonError, PostgresError, Result};
use crate::types::model::FocusRange;

use super::PostgresRepository;

impl PostgresRepository {
    /// Delete a focus range by ID. Returns the number of rows removed.
    pub async fn delete_focus_range(&self, id: Uuid) -> Result<u64> {
        let result = sqlx::query!("DELETE FROM horizon_public.focus_range WHERE id = $1", id)
            .execute(&self.pool)
            .await?;
        Ok(result.rows_affected())
    }

    /// Create a focus range.
    pub async fn insert_focus_range(&self, focus_range: &FocusRange) -> Result<FocusRange> {
        let now = Utc::now();
        let org = self.organization_id.or(focus_range.organization_id);
        Ok(sqlx::query_as!(
            FocusRange,
            r#"
            INSERT INTO horizon_public.focus_range
                (id, name, description, organization_id, modified_datetime)
            VALUES (COALESCE($1, gen_random_uuid()), $2, $3, $4, $5)
            RETURNING id, created_datetime, modified_datetime,
                      name, description, organization_id
            "#,
            focus_range.id,
            focus_range.name,
            focus_range.description,
            org,
            now,
        )
        .fetch_one(&self.pool)
        .await?)
    }

    /// List all focus range records.
    pub async fn list_focus_ranges(&self) -> Result<Vec<FocusRange>> {
        Ok(sqlx::query_as!(
            FocusRange,
            r#"
            SELECT id, created_datetime, modified_datetime,
                   name, description, organization_id
            FROM horizon_public.focus_range
            "#,
        )
        .fetch_all(&self.pool)
        .await?)
    }

    /// Read a focus range by ID.
    pub async fn read_focus_range(&self, id: Uuid) -> Result<Option<FocusRange>> {
        Ok(sqlx::query_as!(
            FocusRange,
            r#"
            SELECT id, created_datetime, modified_datetime,
                   name, description, organization_id
            FROM horizon_public.focus_range WHERE id = $1
            "#,
            id,
        )
        .fetch_optional(&self.pool)
        .await?)
    }

    /// Update an existing focus range by ID.
    ///
    /// Returns `HorizonError::Postgres(PostgresError::NotFound)` if no row
    /// with the given ID exists. Matches Python `BaseRepository.update()`
    /// semantics.
    pub async fn update_focus_range(&self, focus_range: &FocusRange) -> Result<FocusRange> {
        let now = Utc::now();
        let org = self.organization_id.or(focus_range.organization_id);
        let result = sqlx::query_as!(
            FocusRange,
            r#"
            UPDATE horizon_public.focus_range
            SET
                name = $2,
                description = $3,
                organization_id = $4,
                modified_datetime = $5
            WHERE id = $1
            RETURNING id, created_datetime, modified_datetime,
                      name, description, organization_id
            "#,
            focus_range.id,
            focus_range.name,
            focus_range.description,
            org,
            now,
        )
        .fetch_optional(&self.pool)
        .await?;

        result.ok_or_else(|| {
            HorizonError::Postgres(PostgresError::NotFound {
                entity: "focus_range".to_owned(),
                id: focus_range.id.into(),
            })
        })
    }

    /// Insert a focus range or update it if a row with the same ID already
    /// exists. Sets `modified_datetime` to now and falls back to the
    /// repository's `organization_id` when the model does not provide one.
    pub async fn upsert_focus_range(&self, focus_range: &FocusRange) -> Result<FocusRange> {
        let now = Utc::now();
        let org = self.organization_id.or(focus_range.organization_id);
        Ok(sqlx::query_as!(
            FocusRange,
            r#"
            INSERT INTO horizon_public.focus_range
                (id, name, description, organization_id, modified_datetime)
            VALUES (COALESCE($1, gen_random_uuid()), $2, $3, $4, $5)
            ON CONFLICT (id) DO UPDATE SET
                name = EXCLUDED.name, description = EXCLUDED.description,
                organization_id = EXCLUDED.organization_id,
                modified_datetime = EXCLUDED.modified_datetime
            RETURNING id, created_datetime, modified_datetime,
                      name, description, organization_id
            "#,
            focus_range.id,
            focus_range.name,
            focus_range.description,
            org,
            now,
        )
        .fetch_one(&self.pool)
        .await?)
    }
}