summaryrefslogtreecommitdiff
path: root/src/index.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-09-04 12:13:54 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-09-04 12:13:54 -0400
commitcae21da31ff795cc21ec19288fcdc5fdb8a713c7 (patch)
treec3ae1f5fdfc6ebd703a9387b1108671c003b7eaa /src/index.rs
parent2c999920d8f6f0b320960b01721e1f29f4078755 (diff)
Allow any login to create channels.
Diffstat (limited to 'src/index.rs')
-rw-r--r--src/index.rs93
1 files changed, 76 insertions, 17 deletions
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<SqlitePool> {
- Router::new().route("/", get(index))
+async fn index(
+ State(db): State<SqlitePool>,
+ login: Option<Login>,
+) -> Result<Markup, InternalError> {
+ match login {
+ None => Ok(templates::unauthenticated()),
+ Some(login) => index_authenticated(db, login).await,
+ }
+}
+
+async fn index_authenticated(db: SqlitePool, login: Login) -> Result<Markup, InternalError> {
+ 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<Login>) -> impl IntoResponse {
- templates::index(login)
+pub fn router() -> Router<SqlitePool> {
+ 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<Login>) -> Markup {
+ pub fn authenticated<'c>(
+ login: Login,
+ channels: impl IntoIterator<Item = &'c Channel>,
+ ) -> 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<Item = &'c Channel>) -> 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" }
+ }
+ }
+ }
}