| 1 | use rusqlite::params; |
| 2 | |
| 3 | use crate::error::Result; |
| 4 | use crate::models::FolderRecord; |
| 5 | use crate::store::Store; |
| 6 | |
| 7 | impl Store { |
| 8 | pub fn replace_folders( |
| 9 | &self, |
| 10 | folders: &[FolderRecord], |
| 11 | pinned_channel_ids: &[String], |
| 12 | ) -> Result<()> { |
| 13 | let mut connection = self.lock()?; |
| 14 | let transaction = connection.transaction()?; |
| 15 | transaction.execute("DELETE FROM folders", [])?; |
| 16 | transaction.execute("DELETE FROM pinned_channels", [])?; |
| 17 | { |
| 18 | let mut insert_folder = transaction.prepare_cached( |
| 19 | "INSERT INTO folders (id, position, name, kind, expanded) VALUES (?1, ?2, ?3, ?4, ?5)", |
| 20 | )?; |
| 21 | let mut insert_item = transaction.prepare_cached( |
| 22 | "INSERT INTO folder_items (folder_id, position, conversation_id) VALUES (?1, ?2, ?3)", |
| 23 | )?; |
| 24 | for (position, folder) in folders.iter().enumerate() { |
| 25 | insert_folder.execute(params![ |
| 26 | folder.id, |
| 27 | position as i64, |
| 28 | folder.name, |
| 29 | folder.kind, |
| 30 | folder.expanded |
| 31 | ])?; |
| 32 | for (item_position, conversation_id) in folder.conversation_ids.iter().enumerate() { |
| 33 | insert_item.execute(params![ |
| 34 | folder.id, |
| 35 | item_position as i64, |
| 36 | conversation_id |
| 37 | ])?; |
| 38 | } |
| 39 | } |
| 40 | let mut insert_channel = transaction.prepare_cached( |
| 41 | "INSERT INTO pinned_channels (position, channel_id) VALUES (?1, ?2)", |
| 42 | )?; |
| 43 | for (position, channel_id) in pinned_channel_ids.iter().enumerate() { |
| 44 | insert_channel.execute(params![position as i64, channel_id])?; |
| 45 | } |
| 46 | } |
| 47 | Ok(transaction.commit()?) |
| 48 | } |
| 49 | |
| 50 | pub fn folders(&self) -> Result<Vec<FolderRecord>> { |
| 51 | let connection = self.read()?; |
| 52 | let mut statement = connection |
| 53 | .prepare_cached("SELECT id, name, kind, expanded FROM folders ORDER BY position")?; |
| 54 | let mut folders = statement |
| 55 | .query_map([], |row| { |
| 56 | Ok(FolderRecord { |
| 57 | id: row.get(0)?, |
| 58 | name: row.get(1)?, |
| 59 | kind: row.get(2)?, |
| 60 | expanded: row.get(3)?, |
| 61 | conversation_ids: Vec::new(), |
| 62 | }) |
| 63 | })? |
| 64 | .collect::<rusqlite::Result<Vec<_>>>()?; |
| 65 | let mut items = connection.prepare_cached( |
| 66 | "SELECT conversation_id FROM folder_items WHERE folder_id = ?1 ORDER BY position", |
| 67 | )?; |
| 68 | for folder in &mut folders { |
| 69 | folder.conversation_ids = items |
| 70 | .query_map([&folder.id], |row| row.get::<_, String>(0))? |
| 71 | .collect::<rusqlite::Result<_>>()?; |
| 72 | } |
| 73 | Ok(folders) |
| 74 | } |
| 75 | |
| 76 | pub fn set_folder_expanded(&self, folder_id: &str, expanded: bool) -> Result<()> { |
| 77 | let connection = self.lock()?; |
| 78 | connection.execute( |
| 79 | "UPDATE folders SET expanded = ?2 WHERE id = ?1", |
| 80 | params![folder_id, expanded], |
| 81 | )?; |
| 82 | Ok(()) |
| 83 | } |
| 84 | |
| 85 | pub fn pinned_channel_ids(&self) -> Result<Vec<String>> { |
| 86 | let connection = self.read()?; |
| 87 | let mut statement = connection |
| 88 | .prepare_cached("SELECT channel_id FROM pinned_channels ORDER BY position")?; |
| 89 | let rows = statement.query_map([], |row| row.get::<_, String>(0))?; |
| 90 | Ok(rows.collect::<rusqlite::Result<_>>()?) |
| 91 | } |
| 92 | |
| 93 | pub fn replace_pinned_channels(&self, channel_ids: &[String]) -> Result<()> { |
| 94 | let mut connection = self.lock()?; |
| 95 | let transaction = connection.transaction()?; |
| 96 | transaction.execute("DELETE FROM pinned_channels", [])?; |
| 97 | { |
| 98 | let mut insert_channel = transaction.prepare_cached( |
| 99 | "INSERT INTO pinned_channels (position, channel_id) VALUES (?1, ?2)", |
| 100 | )?; |
| 101 | for (position, channel_id) in channel_ids.iter().enumerate() { |
| 102 | insert_channel.execute(params![position as i64, channel_id])?; |
| 103 | } |
| 104 | } |
| 105 | Ok(transaction.commit()?) |
| 106 | } |
| 107 | } |