summaryrefslogtreecommitdiffstats
path: root/src/routes
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2024-11-14 17:59:21 +0000
committerLibravatar cel 🌸 <cel@blos.sm>2024-11-14 17:59:21 +0000
commit469a3ad33914f7eff6edc9ca7fabb12f2950da84 (patch)
tree2712ba2e927fb820b6aa58443c9227d1da24a03f /src/routes
parentb7a2265e9b29d8fa09f84f5213ef7f8ed3045ca6 (diff)
downloadcritch-469a3ad33914f7eff6edc9ca7fabb12f2950da84.tar.gz
critch-469a3ad33914f7eff6edc9ca7fabb12f2950da84.tar.bz2
critch-469a3ad33914f7eff6edc9ca7fabb12f2950da84.zip
database work
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/admin.rs49
-rw-r--r--src/routes/error.rs9
-rw-r--r--src/routes/mod.rs1
3 files changed, 54 insertions, 5 deletions
diff --git a/src/routes/admin.rs b/src/routes/admin.rs
index 98cb954..ccca2de 100644
--- a/src/routes/admin.rs
+++ b/src/routes/admin.rs
@@ -1,11 +1,50 @@
-use poem::handler;
+use poem::{
+ handler,
+ http::StatusCode,
+ session::Session,
+ web::{Data, Form, Redirect},
+ IntoResponse, Response,
+};
+use serde::Deserialize;
+
+use crate::{ructe_poem::render, templates, Critch, Result};
+
+#[derive(Deserialize)]
+struct Login {
+ password: String,
+}
+
+#[handler]
+pub async fn get_dashboard(session: &Session, critch: Data<&Critch>) -> Result<Response> {
+ if let Some(true) = session.get("is_admin") {
+ let comments = critch.db.comments().read_all().await?;
+ let artworks = critch.db.artworks().read_all().await?;
+ return Ok(render!(templates::admin_dashboard_html).into_response());
+ } else {
+ return Ok(Redirect::see_other("/admin/login").into_response());
+ }
+}
+
+#[handler]
+pub fn login(session: &Session, data: Data<&Critch>, form: Form<Login>) -> Response {
+ if form.password == data.config.admin_password() {
+ session.set("is_admin", true);
+ return Redirect::see_other("/admin").into_response();
+ } else {
+ return render!(templates::admin_login_html).into_response();
+ }
+}
#[handler]
-pub async fn login() {
- todo!()
+pub fn logout(session: &Session) -> Response {
+ session.purge();
+ Redirect::see_other("/").into_response()
}
#[handler]
-pub async fn get_login_form() {
- todo!()
+pub fn get_login_form(session: &Session) -> Response {
+ if let Some(true) = session.get("is_admin") {
+ return Redirect::see_other("/admin").into_response();
+ };
+ render!(templates::admin_login_html).into_response()
}
diff --git a/src/routes/error.rs b/src/routes/error.rs
new file mode 100644
index 0000000..3813998
--- /dev/null
+++ b/src/routes/error.rs
@@ -0,0 +1,9 @@
+use poem::{IntoResponse, Response};
+
+use crate::{ructe_poem::render, templates};
+
+pub async fn error(err: poem::Error) -> Response {
+ let status = err.status().to_string();
+ let message = err.to_string();
+ render!(templates::error_html, &status, &message).into_response()
+}
diff --git a/src/routes/mod.rs b/src/routes/mod.rs
index fe8e0d1..0844fb7 100644
--- a/src/routes/mod.rs
+++ b/src/routes/mod.rs
@@ -1,3 +1,4 @@
pub mod admin;
pub mod artists;
pub mod artworks;
+pub mod error;