omni_orchestrator/schemas/v1/db/queries/platforms/
mod.rs

1use sqlx::{MySql, Pool};
2
3use libomni::types::db::v1 as types;
4use types::platform::Platform;
5
6// Get all platforms
7pub async fn get_all_platforms(pool: &Pool<MySql>) -> Result<Vec<Platform>, sqlx::Error> {
8    let platforms = sqlx::query_as::<_, Platform>("SELECT * FROM platforms")
9        .fetch_all(pool)
10        .await?;
11    Ok(platforms)
12}
13
14// Get platform by ID
15pub async fn get_platform_by_id(pool: &Pool<MySql>, platform_id: i64) -> Result<Platform, sqlx::Error> {
16    let platform = sqlx::query_as::<_, Platform>("SELECT * FROM platforms WHERE id = ?")
17        .bind(platform_id)
18        .fetch_one(pool)
19        .await?;
20    Ok(platform)
21}
22
23// Check if platform exists
24pub async fn check_platform_exists(pool: &Pool<MySql>, platform_id: i64) -> Result<bool, sqlx::Error> {
25    let exists = sqlx::query_scalar::<_, bool>("SELECT EXISTS(SELECT 1 FROM platforms WHERE id = ?)")
26        .bind(platform_id)
27        .fetch_one(pool)
28        .await?;
29    Ok(exists)
30}
31
32// Get platform name by ID
33pub async fn get_platform_name(pool: &Pool<MySql>, platform_id: i64) -> Result<String, sqlx::Error> {
34    let name = sqlx::query_scalar::<_, String>("SELECT name FROM platforms WHERE id = ?")
35        .bind(platform_id)
36        .fetch_one(pool)
37        .await?;
38    Ok(name)
39}
40
41// Create a new platform
42pub async fn create_platform(
43    pool: &Pool<MySql>, 
44    name: &str, 
45    description: Option<&str>
46) -> Result<Platform, sqlx::Error> {
47    // First check if a platform with this name already exists
48    let exists = sqlx::query_scalar::<_, bool>("SELECT EXISTS(SELECT 1 FROM platforms WHERE name = ?)")
49        .bind(name)
50        .fetch_one(pool)
51        .await?;
52    
53    if exists {
54        return Err(sqlx::Error::RowNotFound); // Using this error type as a simple way to indicate the row already exists
55    }
56    
57    sqlx::query(
58        "INSERT INTO platforms (name, description) VALUES (?, ?)",
59    )
60    .bind(name)
61    .bind(description)
62    .execute(pool)
63    .await?;
64    
65    // Retrieve the newly created platform
66    let platform = sqlx::query_as::<_, Platform>("SELECT * FROM platforms WHERE name = ?")
67        .bind(name)
68        .fetch_one(pool)
69        .await?;
70    
71    Ok(platform)
72}
73
74// Update platform
75pub async fn update_platform(
76    pool: &Pool<MySql>, 
77    platform_id: i64, 
78    name: Option<&str>, 
79    description: Option<&str>
80) -> Result<Platform, sqlx::Error> {
81    // Build update query dynamically based on provided fields
82    let mut query_parts = Vec::new();
83    let mut query = String::from("UPDATE platforms SET ");
84    
85    if let Some(name_val) = name {
86        query_parts.push("name = ?");
87    }
88    
89    if let Some(desc_val) = description {
90        query_parts.push("description = ?");
91    }
92    
93    // If no fields to update, return the current platform
94    if query_parts.is_empty() {
95        return get_platform_by_id(pool, platform_id).await;
96    }
97    
98    query.push_str(&query_parts.join(", "));
99    query.push_str(" WHERE id = ?");
100    
101    let mut db_query = sqlx::query(&query);
102    
103    // Bind parameters in the order they appear in the query
104    if let Some(name_val) = name {
105        db_query = db_query.bind(name_val);
106    }
107    
108    if let Some(desc_val) = description {
109        db_query = db_query.bind(desc_val);
110    }
111    
112    // Bind the WHERE clause parameter
113    db_query = db_query.bind(platform_id);
114    
115    // Execute the update
116    db_query.execute(pool).await?;
117    
118    // Return the updated platform
119    get_platform_by_id(pool, platform_id).await
120}
121
122// Delete platform
123pub async fn delete_platform(pool: &Pool<MySql>, platform_id: i64) -> Result<(), sqlx::Error> {
124    // Check if platform exists first
125    let exists = check_platform_exists(pool, platform_id).await?;
126    
127    if !exists {
128        return Err(sqlx::Error::RowNotFound);
129    }
130    
131    sqlx::query("DELETE FROM platforms WHERE id = ?")
132        .bind(platform_id)
133        .execute(pool)
134        .await?;
135    
136    Ok(())
137}
138
139// Get count of platforms
140pub async fn count_platforms(pool: &Pool<MySql>) -> Result<i64, sqlx::Error> {
141    let count = sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM platforms")
142        .fetch_one(pool)
143        .await?;
144    Ok(count)
145}
146
147// List platforms with pagination
148pub async fn list_platforms(
149    pool: &Pool<MySql>, 
150    page: i64, 
151    per_page: i64
152) -> Result<Vec<Platform>, sqlx::Error> {
153    let offset = (page - 1) * per_page;
154    
155    let platforms = sqlx::query_as::<_, Platform>(
156        "SELECT * FROM platforms ORDER BY id LIMIT ? OFFSET ?"
157    )
158    .bind(per_page)
159    .bind(offset)
160    .fetch_all(pool)
161    .await?;
162    
163    Ok(platforms)
164}
165
166// Search platforms by name
167pub async fn search_platforms_by_name(
168    pool: &Pool<MySql>,
169    name_pattern: &str
170) -> Result<Vec<Platform>, sqlx::Error> {
171    let search_pattern = format!("%{}%", name_pattern);
172    
173    let platforms = sqlx::query_as::<_, Platform>(
174        "SELECT * FROM platforms WHERE name LIKE ? ORDER BY name"
175    )
176    .bind(search_pattern)
177    .fetch_all(pool)
178    .await?;
179    
180    Ok(platforms)
181}