From 54fa4949c83be35f3007ea7ebd4df4c481451ecd Mon Sep 17 00:00:00 2001 From: Nicole Tietz-Sokolskaya Date: Thu, 9 May 2024 12:19:50 -0400 Subject: [PATCH] Add page for displaying documents --- src/handler.rs | 1 + src/handler/documents.rs | 26 ++++++++++++++ src/server.rs | 3 +- templates/documents/create_document.html | 0 templates/documents/list_documents.html | 43 ++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/handler/documents.rs create mode 100644 templates/documents/create_document.html create mode 100644 templates/documents/list_documents.html diff --git a/src/handler.rs b/src/handler.rs index b7a2586..566411a 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -1,3 +1,4 @@ +pub mod documents; pub mod home; pub mod login; pub mod projects; diff --git a/src/handler/documents.rs b/src/handler/documents.rs new file mode 100644 index 0000000..498eaba --- /dev/null +++ b/src/handler/documents.rs @@ -0,0 +1,26 @@ +use axum::{response::Redirect, Form}; +use axum_login::AuthSession; + +use crate::{models::ModelPermission, prelude::*}; + +pub async fn documents_page( + State(ctx): State, + auth_session: AuthSession, +) -> Response { + if let Some(user) = auth_session.user { + render_documents_page(ctx, user).await + } else { + Redirect::to("/login").into_response() + } +} + +async fn render_documents_page(ctx: Context, user: crate::entity::user::Model) -> Response { + let documents = ModelPermission::user_documents(&ctx.kv_handles, user.id).unwrap_or_default(); + + let values = context! { + user => user, + documents => documents, + }; + + ctx.render_resp("documents/list_documents.html", values) +} diff --git a/src/server.rs b/src/server.rs index e427cb6..7ac8b47 100644 --- a/src/server.rs +++ b/src/server.rs @@ -10,7 +10,7 @@ use tower_sessions::SessionManagerLayer; use tower_sessions_sqlx_store::{sqlx::SqlitePool, SqliteStore}; use tracing::Level; -use crate::{config::CommandLineOptions, context::Context, handler::{home::home_page, login::logout, login_page, login_submit, projects::{create_project_page, create_project_submit, projects_page}}, kv::KvHandle, logging::setup_logging, templates::make_template_loader}; +use crate::{config::CommandLineOptions, context::Context, handler::{documents::documents_page, home::home_page, login::logout, login_page, login_submit, projects::{create_project_page, create_project_submit, projects_page}}, kv::KvHandle, logging::setup_logging, templates::make_template_loader}; pub async fn run() -> Result<()> { dotenvy::dotenv()?; @@ -46,6 +46,7 @@ pub async fn run() -> Result<()> { .route("/projects", get(projects_page)) .route("/projects/new", get(create_project_page)) .route("/projects/new", post(create_project_submit)) + .route("/documents", get(documents_page)) .layer(trace_layer) .layer(session_layer) .layer(auth_layer) diff --git a/templates/documents/create_document.html b/templates/documents/create_document.html new file mode 100644 index 0000000..e69de29 diff --git a/templates/documents/list_documents.html b/templates/documents/list_documents.html new file mode 100644 index 0000000..0fc4e48 --- /dev/null +++ b/templates/documents/list_documents.html @@ -0,0 +1,43 @@ + + +{% include "head.html" %} + + +
+ {% set current_page = "documents" %} + {% include "components/sidebar.html" %} + +
+ +
+ + + {% for document in documents %} +
+
+

{{ document.name }}

+
+ +
+
+
+ {% endfor %} + +
+
+ + + + +