BlueGit

ict rusty-teams

Files

rusty-teams / crates / store / src / teams.rs

teams.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
teams.rs 4.0 KiB Download Raw
1use rusqlite::{OptionalExtension, Row, params};
2
3use crate::error::Result;
4use crate::models::{ChannelRecord, TeamRecord};
5use crate::store::{Store, json_ids};
6use crate::time::{optional_from_millis, optional_to_millis};
7
8const CHANNEL_COLUMNS: &str = "id, team_id, name, membership_type, last_message_at, unread";
9
10impl Store {
11 pub fn upsert_teams(&self, teams: &[TeamRecord]) -> Result<()> {
12 let mut connection = self.lock()?;
13 let transaction = connection.transaction()?;
14 {
15 let mut upsert = transaction.prepare_cached(
16 "INSERT INTO teams (id, name) VALUES (?1, ?2) ON CONFLICT (id) DO UPDATE SET name = excluded.name",
17 )?;
18 for team in teams {
19 upsert.execute(params![team.id, team.name])?;
20 }
21 }
22 Ok(transaction.commit()?)
23 }
24
25 pub fn upsert_channels(&self, channels: &[ChannelRecord]) -> Result<()> {
26 let mut connection = self.lock()?;
27 let transaction = connection.transaction()?;
28 {
29 let mut upsert = transaction.prepare_cached(
30 "INSERT INTO channels (id, team_id, name, membership_type, last_message_at, unread)
31 VALUES (?1, ?2, ?3, ?4, ?5, ?6)
32 ON CONFLICT (id) DO UPDATE SET
33 team_id = excluded.team_id, name = excluded.name, membership_type = excluded.membership_type,
34 last_message_at = COALESCE(excluded.last_message_at, channels.last_message_at),
35 unread = excluded.unread",
36 )?;
37 for channel in channels {
38 upsert.execute(params![
39 channel.id,
40 channel.team_id,
41 channel.name,
42 channel.membership_type,
43 optional_to_millis(channel.last_message_at),
44 channel.unread,
45 ])?;
46 }
47 }
48 Ok(transaction.commit()?)
49 }
50
51 pub fn remove_teams_except(&self, kept_team_ids: &[String]) -> Result<usize> {
52 let mut connection = self.lock()?;
53 let transaction = connection.transaction()?;
54 let kept = json_ids(kept_team_ids);
55 let removed = transaction.execute(
56 "DELETE FROM teams WHERE id NOT IN (SELECT value FROM json_each(?1))",
57 [kept],
58 )?;
59 transaction.commit()?;
60 Ok(removed)
61 }
62
63 pub fn remove_channels_except(
64 &self,
65 team_id: &str,
66 kept_channel_ids: &[String],
67 ) -> Result<usize> {
68 let connection = self.lock()?;
69 Ok(connection.execute(
70 "DELETE FROM channels WHERE team_id = ?1 AND id NOT IN (SELECT value FROM json_each(?2))",
71 params![team_id, json_ids(kept_channel_ids)],
72 )?)
73 }
74
75 pub fn team(&self, team_id: &str) -> Result<Option<TeamRecord>> {
76 let connection = self.lock()?;
77 Ok(connection
78 .query_row(
79 "SELECT id, name FROM teams WHERE id = ?1",
80 [team_id],
81 |row| {
82 Ok(TeamRecord {
83 id: row.get(0)?,
84 name: row.get(1)?,
85 })
86 },
87 )
88 .optional()?)
89 }
90
91 pub fn channel(&self, channel_id: &str) -> Result<Option<ChannelRecord>> {
92 let connection = self.lock()?;
93 let mut statement = connection.prepare_cached(&format!(
94 "SELECT {CHANNEL_COLUMNS} FROM channels WHERE id = ?1"
95 ))?;
96 let mut channels = statement
97 .query_map([channel_id], channel_from_row)?
98 .collect::<rusqlite::Result<Vec<_>>>()?;
99 Ok(channels.pop())
100 }
101}
102
103pub(crate) fn channel_from_row(row: &Row<'_>) -> rusqlite::Result<ChannelRecord> {
104 Ok(ChannelRecord {
105 id: row.get(0)?,
106 team_id: row.get(1)?,
107 name: row.get(2)?,
108 membership_type: row.get(3)?,
109 last_message_at: optional_from_millis(row.get(4)?),
110 unread: row.get(5)?,
111 })
112}