use axum::{ extract::{Form, Path, State}, response::{IntoResponse, Redirect}, routing::post, Router, }; use crate::{ app::App, clock::RequestedAt, error::InternalError, repo::{channel, login::Login}, }; pub fn router() -> Router { Router::new() .route("/create", post(on_create)) .route("/:channel/send", post(on_send)) } #[derive(serde::Deserialize)] struct CreateRequest { name: String, } async fn on_create( State(app): State, _: Login, // requires auth, but doesn't actually care who you are Form(form): Form, ) -> Result { app.channels().create(&form.name).await?; Ok(Redirect::to("/")) } #[derive(serde::Deserialize)] struct SendRequest { message: String, } async fn on_send( Path(channel): Path, RequestedAt(sent_at): RequestedAt, State(app): State, login: Login, Form(form): Form, ) -> Result { app.channels() .send(&login, &channel, &form.message, &sent_at) .await?; Ok(Redirect::to(&format!("/{}", channel))) }