diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-09-04 12:13:54 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-09-04 12:13:54 -0400 |
| commit | cae21da31ff795cc21ec19288fcdc5fdb8a713c7 (patch) | |
| tree | c3ae1f5fdfc6ebd703a9387b1108671c003b7eaa /src/channel/repo.rs | |
| parent | 2c999920d8f6f0b320960b01721e1f29f4078755 (diff) | |
Allow any login to create channels.
Diffstat (limited to 'src/channel/repo.rs')
| -rw-r--r-- | src/channel/repo.rs | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/channel/repo.rs b/src/channel/repo.rs new file mode 100644 index 0000000..e6a5e5c --- /dev/null +++ b/src/channel/repo.rs @@ -0,0 +1,108 @@ +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 for_login(&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) + } +} + +/// Stable identifier for a [Channel]. Prefixed with `C`. +#[derive(Debug, sqlx::Type)] +#[sqlx(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) + } +} |
