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 { 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, artworks).into_response()); } else { return Ok(Redirect::see_other("/admin/login").into_response()); } } #[handler] pub fn login(session: &Session, data: Data<&Critch>, form: Form) -> 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 fn logout(session: &Session) -> Response { session.purge(); Redirect::see_other("/").into_response() } #[handler] 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() }