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/routes.rs | |
| parent | 2c999920d8f6f0b320960b01721e1f29f4078755 (diff) | |
Allow any login to create channels.
Diffstat (limited to 'src/channel/routes.rs')
| -rw-r--r-- | src/channel/routes.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/channel/routes.rs b/src/channel/routes.rs new file mode 100644 index 0000000..c8d6c3f --- /dev/null +++ b/src/channel/routes.rs @@ -0,0 +1,32 @@ +use axum::{ + extract::{Form, State}, + response::{IntoResponse, Redirect}, + routing::post, + Router, +}; +use sqlx::sqlite::SqlitePool; + +use super::repo::Provider as _; +use crate::{error::InternalError, login::repo::logins::Login}; + +pub fn router() -> Router<SqlitePool> { + Router::new().route("/create", post(on_create)) +} + +#[derive(serde::Deserialize)] +struct CreateRequest { + name: String, +} + +async fn on_create( + State(db): State<SqlitePool>, + login: Login, + Form(form): Form<CreateRequest>, +) -> Result<impl IntoResponse, InternalError> { + let mut tx = db.begin().await?; + let channel = tx.channels().create(&form.name).await?; + tx.channels().join(&channel.id, &login.id).await?; + tx.commit().await?; + + Ok(Redirect::to("/")) +} |
