diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-09-04 23:38:21 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-09-04 23:38:21 -0400 |
| commit | 6366fb3c96e4ed281e233279c85bbfd90ab3ecbc (patch) | |
| tree | bb46e2f9d42d6085517767ed7d53a47885a30345 /src/channel | |
| parent | beeb40acdc07d5652bf2128ecb8f71a1116993ae (diff) | |
Support joining channels.
Diffstat (limited to 'src/channel')
| -rw-r--r-- | src/channel/repo.rs | 28 | ||||
| -rw-r--r-- | src/channel/routes.rs | 18 |
2 files changed, 45 insertions, 1 deletions
diff --git a/src/channel/repo.rs b/src/channel/repo.rs index bb39d6e..a255305 100644 --- a/src/channel/repo.rs +++ b/src/channel/repo.rs @@ -62,7 +62,7 @@ impl<'c> Channels<'c> { Ok(()) } - pub async fn for_login(&mut self, login: &LoginId) -> Result<Vec<Channel>, BoxedError> { + pub async fn joined(&mut self, login: &LoginId) -> Result<Vec<Channel>, BoxedError> { let channels = sqlx::query_as!( Channel, r#" @@ -83,6 +83,32 @@ impl<'c> Channels<'c> { 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!( diff --git a/src/channel/routes.rs b/src/channel/routes.rs index 4453a1e..3dc2b1a 100644 --- a/src/channel/routes.rs +++ b/src/channel/routes.rs @@ -12,6 +12,7 @@ use crate::{error::InternalError, login::repo::logins::Login}; pub fn router() -> Router<SqlitePool> { Router::new() .route("/create", post(on_create)) + .route("/join", post(on_join)) .route("/:channel/leave", post(on_leave)) } @@ -33,6 +34,23 @@ async fn on_create( Ok(Redirect::to("/")) } +#[derive(serde::Deserialize)] +struct JoinRequest { + channel: ChannelId, +} + +async fn on_join( + State(db): State<SqlitePool>, + login: Login, + Form(req): Form<JoinRequest>, +) -> Result<impl IntoResponse, InternalError> { + let mut tx = db.begin().await?; + tx.channels().join(&req.channel, &login.id).await?; + tx.commit().await?; + + Ok(Redirect::to("/")) +} + async fn on_leave( State(db): State<SqlitePool>, login: Login, |
