From cae21da31ff795cc21ec19288fcdc5fdb8a713c7 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 4 Sep 2024 12:13:54 -0400 Subject: Allow any login to create channels. --- src/index.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 76 insertions(+), 17 deletions(-) (limited to 'src/index.rs') diff --git a/src/index.rs b/src/index.rs index a716af2..8ff9f7e 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1,48 +1,79 @@ -use axum::{response::IntoResponse, routing::get, Router}; +use axum::{extract::State, routing::get, Router}; +use maud::Markup; use sqlx::sqlite::SqlitePool; -use crate::login::repo::logins::Login; +use crate::{channel::repo::Provider as _, error::InternalError, login::repo::logins::Login}; -pub fn router() -> Router { - Router::new().route("/", get(index)) +async fn index( + State(db): State, + login: Option, +) -> Result { + match login { + None => Ok(templates::unauthenticated()), + Some(login) => index_authenticated(db, login).await, + } +} + +async fn index_authenticated(db: SqlitePool, login: Login) -> Result { + let mut tx = db.begin().await?; + let channels = tx.channels().for_login(&login.id).await?; + tx.commit().await?; + + Ok(templates::authenticated(login, &channels)) } -async fn index(login: Option) -> impl IntoResponse { - templates::index(login) +pub fn router() -> Router { + Router::new().route("/", get(index)) } mod templates { use maud::{html, Markup, DOCTYPE}; - use crate::login::repo::logins::Login; + use crate::{channel::repo::Channel, login::repo::logins::Login}; - pub fn index(login: Option) -> Markup { + pub fn authenticated<'c>( + login: Login, + channels: impl IntoIterator, + ) -> Markup { html! { (DOCTYPE) head { title { "hi" } } body { - @match login { - None => { (login_form()) } - Some(login) => { (logout_form(&login.name)) } + section { + (channel_list(channels)) + (create_channel()) + } + section { + (logout_form(&login.name)) } } } } - fn login_form() -> Markup { + fn channel_list<'c>(channels: impl IntoIterator) -> Markup { html! { - form action="/login" method="post" { + ul { + @for channel in channels { + li { + (channel.name) "(" (channel.id) ")" + } + } + } + } + } + + fn create_channel() -> Markup { + html! { + form action="/create" method="post" { label { "name" input name="name" type="text" {} } - label { - "password" - input name="password" type="password" {} + button { + "start channel" } - button { "hi" } } } } @@ -54,4 +85,32 @@ mod templates { } } } + + pub fn unauthenticated() -> Markup { + html! { + (DOCTYPE) + head { + title { "hi" } + } + body { + (login_form()) + } + } + } + + fn login_form() -> Markup { + html! { + form action="/login" method="post" { + label { + "login" + input name="name" type="text" {} + } + label { + "password" + input name="password" type="password" {} + } + button { "hi" } + } + } + } } -- cgit v1.2.3