|
1 | | -use crate::db::{Drawing, DB}; |
| 1 | +use crate::db::{Drawing, Snapshot, RoomSettings, DB}; |
2 | 2 | use std::time::{SystemTime, UNIX_EPOCH}; |
3 | 3 |
|
4 | 4 | #[tauri::command] |
@@ -96,3 +96,175 @@ pub fn delete_drawing(id: String) -> Result<(), String> { |
96 | 96 |
|
97 | 97 | Ok(()) |
98 | 98 | } |
| 99 | + |
| 100 | +// Snapshot-related commands |
| 101 | + |
| 102 | +#[tauri::command] |
| 103 | +pub fn save_snapshot( |
| 104 | + room_id: String, |
| 105 | + name: Option<String>, |
| 106 | + description: Option<String>, |
| 107 | + thumbnail: Option<String>, |
| 108 | + created_by: Option<String>, |
| 109 | + data: String, |
| 110 | +) -> Result<String, String> { |
| 111 | + let timestamp = SystemTime::now() |
| 112 | + .duration_since(UNIX_EPOCH) |
| 113 | + .unwrap() |
| 114 | + .as_secs() as i64; |
| 115 | + |
| 116 | + let id = uuid::Uuid::new_v4().to_string(); |
| 117 | + |
| 118 | + let conn = DB.lock().map_err(|e| e.to_string())?; |
| 119 | + |
| 120 | + // Get room settings to check max snapshots |
| 121 | + let settings = get_room_settings_internal(&conn, &room_id)?; |
| 122 | + |
| 123 | + // Count existing snapshots |
| 124 | + let count: i32 = conn |
| 125 | + .query_row( |
| 126 | + "SELECT COUNT(*) FROM snapshots WHERE room_id = ?1", |
| 127 | + rusqlite::params![&room_id], |
| 128 | + |row| row.get(0), |
| 129 | + ) |
| 130 | + .map_err(|e| e.to_string())?; |
| 131 | + |
| 132 | + // If at limit, delete oldest snapshot |
| 133 | + if count >= settings.max_snapshots { |
| 134 | + conn.execute( |
| 135 | + "DELETE FROM snapshots WHERE id = (SELECT id FROM snapshots WHERE room_id = ?1 ORDER BY created_at ASC LIMIT 1)", |
| 136 | + rusqlite::params![&room_id], |
| 137 | + ) |
| 138 | + .map_err(|e| e.to_string())?; |
| 139 | + } |
| 140 | + |
| 141 | + // Insert new snapshot |
| 142 | + conn.execute( |
| 143 | + "INSERT INTO snapshots (id, room_id, name, description, thumbnail, created_by, created_at, data) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)", |
| 144 | + rusqlite::params![&id, &room_id, &name, &description, &thumbnail, &created_by, timestamp, &data], |
| 145 | + ) |
| 146 | + .map_err(|e| e.to_string())?; |
| 147 | + |
| 148 | + Ok(id) |
| 149 | +} |
| 150 | + |
| 151 | +#[tauri::command] |
| 152 | +pub fn list_snapshots(room_id: String) -> Result<Vec<Snapshot>, String> { |
| 153 | + let conn = DB.lock().map_err(|e| e.to_string())?; |
| 154 | + |
| 155 | + let mut stmt = conn |
| 156 | + .prepare("SELECT id, room_id, name, description, thumbnail, created_by, created_at, '' as data FROM snapshots WHERE room_id = ?1 ORDER BY created_at DESC") |
| 157 | + .map_err(|e| e.to_string())?; |
| 158 | + |
| 159 | + let snapshots = stmt |
| 160 | + .query_map(rusqlite::params![&room_id], |row| { |
| 161 | + Ok(Snapshot { |
| 162 | + id: row.get(0)?, |
| 163 | + room_id: row.get(1)?, |
| 164 | + name: row.get(2)?, |
| 165 | + description: row.get(3)?, |
| 166 | + thumbnail: row.get(4)?, |
| 167 | + created_by: row.get(5)?, |
| 168 | + created_at: row.get(6)?, |
| 169 | + data: row.get(7)?, |
| 170 | + }) |
| 171 | + }) |
| 172 | + .map_err(|e| e.to_string())? |
| 173 | + .collect::<Result<Vec<_>, _>>() |
| 174 | + .map_err(|e| e.to_string())?; |
| 175 | + |
| 176 | + Ok(snapshots) |
| 177 | +} |
| 178 | + |
| 179 | +#[tauri::command] |
| 180 | +pub fn load_snapshot(id: String) -> Result<Snapshot, String> { |
| 181 | + let conn = DB.lock().map_err(|e| e.to_string())?; |
| 182 | + |
| 183 | + let snapshot = conn |
| 184 | + .query_row( |
| 185 | + "SELECT id, room_id, name, description, thumbnail, created_by, created_at, data FROM snapshots WHERE id = ?1", |
| 186 | + rusqlite::params![&id], |
| 187 | + |row| { |
| 188 | + Ok(Snapshot { |
| 189 | + id: row.get(0)?, |
| 190 | + room_id: row.get(1)?, |
| 191 | + name: row.get(2)?, |
| 192 | + description: row.get(3)?, |
| 193 | + thumbnail: row.get(4)?, |
| 194 | + created_by: row.get(5)?, |
| 195 | + created_at: row.get(6)?, |
| 196 | + data: row.get(7)?, |
| 197 | + }) |
| 198 | + }, |
| 199 | + ) |
| 200 | + .map_err(|e| e.to_string())?; |
| 201 | + |
| 202 | + Ok(snapshot) |
| 203 | +} |
| 204 | + |
| 205 | +#[tauri::command] |
| 206 | +pub fn delete_snapshot(id: String) -> Result<(), String> { |
| 207 | + let conn = DB.lock().map_err(|e| e.to_string())?; |
| 208 | + |
| 209 | + conn.execute("DELETE FROM snapshots WHERE id = ?1", rusqlite::params![&id]) |
| 210 | + .map_err(|e| e.to_string())?; |
| 211 | + |
| 212 | + Ok(()) |
| 213 | +} |
| 214 | + |
| 215 | +#[tauri::command] |
| 216 | +pub fn update_snapshot_metadata(id: String, name: String, description: String) -> Result<(), String> { |
| 217 | + let conn = DB.lock().map_err(|e| e.to_string())?; |
| 218 | + |
| 219 | + conn.execute( |
| 220 | + "UPDATE snapshots SET name = ?1, description = ?2 WHERE id = ?3", |
| 221 | + rusqlite::params![&name, &description, &id], |
| 222 | + ) |
| 223 | + .map_err(|e| e.to_string())?; |
| 224 | + |
| 225 | + Ok(()) |
| 226 | +} |
| 227 | + |
| 228 | +// Room settings commands |
| 229 | + |
| 230 | +fn get_room_settings_internal(conn: &rusqlite::Connection, room_id: &str) -> Result<RoomSettings, String> { |
| 231 | + conn.query_row( |
| 232 | + "SELECT room_id, max_snapshots, auto_save_interval FROM room_settings WHERE room_id = ?1", |
| 233 | + rusqlite::params![room_id], |
| 234 | + |row| { |
| 235 | + Ok(RoomSettings { |
| 236 | + room_id: row.get(0)?, |
| 237 | + max_snapshots: row.get(1)?, |
| 238 | + auto_save_interval: row.get(2)?, |
| 239 | + }) |
| 240 | + }, |
| 241 | + ) |
| 242 | + .or_else(|_| { |
| 243 | + // Return default settings if not found |
| 244 | + Ok(RoomSettings { |
| 245 | + room_id: room_id.to_string(), |
| 246 | + max_snapshots: 10, |
| 247 | + auto_save_interval: 300, |
| 248 | + }) |
| 249 | + }) |
| 250 | +} |
| 251 | + |
| 252 | +#[tauri::command] |
| 253 | +pub fn get_room_settings(room_id: String) -> Result<RoomSettings, String> { |
| 254 | + let conn = DB.lock().map_err(|e| e.to_string())?; |
| 255 | + get_room_settings_internal(&conn, &room_id) |
| 256 | +} |
| 257 | + |
| 258 | +#[tauri::command] |
| 259 | +pub fn update_room_settings(room_id: String, max_snapshots: i32, auto_save_interval: i32) -> Result<(), String> { |
| 260 | + let conn = DB.lock().map_err(|e| e.to_string())?; |
| 261 | + |
| 262 | + conn.execute( |
| 263 | + "INSERT INTO room_settings (room_id, max_snapshots, auto_save_interval) VALUES (?1, ?2, ?3) |
| 264 | + ON CONFLICT(room_id) DO UPDATE SET max_snapshots = ?2, auto_save_interval = ?3", |
| 265 | + rusqlite::params![&room_id, max_snapshots, auto_save_interval], |
| 266 | + ) |
| 267 | + .map_err(|e| e.to_string())?; |
| 268 | + |
| 269 | + Ok(()) |
| 270 | +} |
0 commit comments