summaryrefslogtreecommitdiff
path: root/src/channel/routes.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/channel/routes.rs')
-rw-r--r--src/channel/routes.rs32
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("/"))
+}