1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
use std::fmt;
use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction};
use crate::id::Id as BaseId;
use crate::{error::BoxedError, login::repo::logins::Id as LoginId};
pub trait Provider {
fn channels(&mut self) -> Channels;
}
impl<'c> Provider for Transaction<'c, Sqlite> {
fn channels(&mut self) -> Channels {
Channels(self)
}
}
pub struct Channels<'t>(&'t mut SqliteConnection);
#[derive(Debug)]
pub struct Channel {
pub id: Id,
pub name: String,
}
impl<'c> Channels<'c> {
/// Create a new channel.
pub async fn create(&mut self, name: &str) -> Result<Channel, BoxedError> {
let id = Id::generate();
let channel = sqlx::query_as!(
Channel,
r#"
insert
into channel (id, name)
values ($1, $2)
returning id as "id: Id", name
"#,
id,
name,
)
.fetch_one(&mut *self.0)
.await?;
Ok(channel)
}
/// Enrol a login in a channel.
pub async fn join(&mut self, channel: &Id, login: &LoginId) -> Result<(), BoxedError> {
sqlx::query!(
r#"
insert
into channel_member (channel, login)
values ($1, $2)
"#,
channel,
login,
)
.execute(&mut *self.0)
.await?;
Ok(())
}
pub async fn joined(&mut self, login: &LoginId) -> Result<Vec<Channel>, BoxedError> {
let channels = sqlx::query_as!(
Channel,
r#"
select
channel.id as "id: Id",
channel.name
from channel
join channel_member
on (channel.id = channel_member.channel)
where channel_member.login = $1
order by channel.name
"#,
login,
)
.fetch_all(&mut *self.0)
.await?;
Ok(channels)
}
pub async fn unjoined(&mut self, login: &LoginId) -> Result<Vec<Channel>, BoxedError> {
let channels = sqlx::query_as!(
Channel,
r#"
select
channel.id as "id: Id",
channel.name
from channel
except
select
channel.id as "id: Id",
channel.name
from channel
join channel_member
on (channel.id = channel_member.channel)
where channel_member.login = $1
order by channel.name
"#,
login,
)
.fetch_all(&mut *self.0)
.await?;
Ok(channels)
}
/// Unenrol a login from a channel.
pub async fn leave(&mut self, channel: &Id, login: &LoginId) -> Result<(), BoxedError> {
sqlx::query_scalar!(
r#"
delete
from channel_member
where channel = $1
and login = $2
returning 1 as "deleted: bool"
"#,
channel,
login,
)
.fetch_one(&mut *self.0)
.await?;
Ok(())
}
}
/// Stable identifier for a [Channel]. Prefixed with `C`.
#[derive(Debug, sqlx::Type, serde::Deserialize)]
#[sqlx(transparent)]
#[serde(transparent)]
pub struct Id(BaseId);
impl From<BaseId> for Id {
fn from(id: BaseId) -> Self {
Self(id)
}
}
impl Id {
pub fn generate() -> Self {
BaseId::generate("C")
}
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
|