From f26215248ed108fe09198175f1dc8892e28d4c13 Mon Sep 17 00:00:00 2001 From: famfo Date: Mon, 23 May 2022 16:45:46 +0200 Subject: [PATCH] Navbar stuff --- README.md | 1 - src/database.rs | 10 ++- src/templates.rs | 40 +++++++---- src/web.rs | 81 ++++++++++++++-------- templates/navbar.html | 10 ++- templates/{ => site/admin}/admin.html | 0 templates/{ => site}/index.html | 0 templates/{ => site/login}/login.html | 0 templates/{ => site/login}/register.html | 0 templates/{ => site}/task/create_task.html | 0 templates/{ => site}/task/task.html | 0 templates/{ => site}/task/task_page.html | 2 +- templates/{ => site/users}/users.html | 0 13 files changed, 95 insertions(+), 49 deletions(-) rename templates/{ => site/admin}/admin.html (100%) rename templates/{ => site}/index.html (100%) rename templates/{ => site/login}/login.html (100%) rename templates/{ => site/login}/register.html (100%) rename templates/{ => site}/task/create_task.html (100%) rename templates/{ => site}/task/task.html (100%) rename templates/{ => site}/task/task_page.html (94%) rename templates/{ => site/users}/users.html (100%) diff --git a/README.md b/README.md index 85ea850..0767b33 100644 --- a/README.md +++ b/README.md @@ -9,5 +9,4 @@ - [ ] Actually do database stuff (@lemonsh) - [ ] Cookies! (@lemonsh) - [ ] Error messages when something goes wrong (@famfo) -- [ ] For more then x tasks create a new task page (?) (@famfo) diff --git a/src/database.rs b/src/database.rs index 61695a9..d52b3a1 100644 --- a/src/database.rs +++ b/src/database.rs @@ -53,7 +53,7 @@ impl Database { } // Async might become a problem here - pub fn get_tasks(&self) -> templates::Tasks { + pub async fn get_tasks(&self) -> templates::Tasks { // TODO: actually get the issues from the db based on the category cookies let vec = vec![templates::Task { title: "finish tmtd".to_string(), @@ -64,8 +64,9 @@ impl Database { description: "DO SOMETHING AAAAAAA".to_string(), id: 1, }]; + let logged_in = self.logged_in().await; - templates::Tasks { tasks: vec } + templates::Tasks { tasks: vec, logged_in } } pub async fn create_task(&self, task: &task::CreateTask) { @@ -109,4 +110,9 @@ impl Database { // TODO: find user in DB and check if the password matches // TODO: save that the user is logged in a cookie or something } + + pub async fn logged_in(&self) -> Option { + // TODO: find out if the user is logged in and return the username if yes + None + } } diff --git a/src/templates.rs b/src/templates.rs index aa0a572..a2212f3 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -19,21 +19,28 @@ use askama::Template; #[derive(Template)] -#[template(path = "index.html")] -pub struct Index(); +#[template(path = "site/index.html")] +pub struct Index { + pub logged_in: Option, +} #[derive(Template)] -#[template(path = "users.html")] -pub struct Users(); +#[template(path = "site/users/users.html")] +pub struct Users { + pub logged_in: Option, +} #[derive(Template)] -#[template(path = "admin.html")] -pub struct AdminPanel(); +#[template(path = "site/admin/admin.html")] +pub struct AdminPanel { + pub logged_in: Option, +} #[derive(Template)] -#[template(path = "task/task_page.html")] +#[template(path = "site/task/task_page.html")] pub struct Tasks { pub tasks: Vec, + pub logged_in: Option, } pub struct Task { @@ -46,13 +53,20 @@ pub struct Task { } #[derive(Template)] -#[template(path = "task/create_task.html")] -pub struct CreateTask(); +#[template(path = "site/task/create_task.html")] +pub struct CreateTask { + pub logged_in: Option, +} #[derive(Template)] -#[template(path = "login.html")] -pub struct Login(); +#[template(path = "site/login/login.html")] +pub struct Login { + pub logged_in: Option, +} #[derive(Template)] -#[template(path = "register.html")] -pub struct Register(); +#[template(path = "site/login/register.html")] +pub struct Register { + pub logged_in: Option, +} + diff --git a/src/web.rs b/src/web.rs index ba2e278..08c8ebe 100644 --- a/src/web.rs +++ b/src/web.rs @@ -31,28 +31,43 @@ impl App { let db = db.clone(); let server = HttpServer::new(move || { actix_web::App::new().service(( - web::resource("/").route(web::get().to(index)), - web::resource("/users").route(web::get().to(users)), + web::resource("/") + .app_data(web::Data::new(db.clone())) + .route(web::get().to(index)), + web::resource("/users") + .app_data(web::Data::new(db.clone())) + .route(web::get().to(users)), web::resource("/task") .app_data(web::Data::new(db.clone())) .route(web::get().to(tasks)), - web::resource("/admin").route(web::get().to(admin)), - web::resource("/create").route(web::get().to(create)), - web::resource("/login").route(web::get().to(login)), - web::resource("/register").route(web::get().to(register)), + web::resource("/admin") + .app_data(web::Data::new(db.clone())) + .route(web::get().to(admin)), + web::resource("/create") + .app_data(web::Data::new(db.clone())) + .route(web::get().to(create)), + web::resource("/login") + .app_data(web::Data::new(db.clone())) + .route(web::get().to(login)), + web::resource("/register") + .app_data(web::Data::new(db.clone())) + .route(web::get().to(register)), web::resource("/api/task/create") .app_data(web::Data::new(db.clone())) .route(web::post().to(create_task)), web::resource("/api/task/move") .app_data(web::Data::new(db.clone())) .route(web::post().to(move_task)), - web::resource("/api/task/sort").route(web::post().to(sort_task)), + //web::resource("/api/task/sort").route(web::post().to(sort_task)), web::resource("/api/login") .app_data(web::Data::new(db.clone())) .route(web::post().to(login_user)), web::resource("/api/register") .app_data(web::Data::new(db.clone())) .route(web::post().to(register_user)), + web::resource("/api/logout") + .app_data(web::Data::new(db.clone())) + .route(web::get().to(logout)), )) }) .bind(addr)? @@ -61,44 +76,50 @@ impl App { } } -async fn index() -> impl Responder { - let index = templates::Index {}; +async fn index(db: web::Data>) -> impl Responder { + let logged_in = db.logged_in().await; + let index = templates::Index { logged_in }; let html = index.render().unwrap(); HttpResponseBuilder::new(StatusCode::OK).body(html) } async fn tasks(db: web::Data>) -> impl Responder { - let tasks = db.get_tasks(); + let tasks = db.get_tasks().await; let html = tasks.render().unwrap(); HttpResponseBuilder::new(StatusCode::OK).body(html) } -async fn users() -> impl Responder { - let users = templates::Users {}; +async fn users(db: web::Data>) -> impl Responder { + let logged_in = db.logged_in().await; + let users = templates::Users { logged_in }; let html = users.render().unwrap(); HttpResponseBuilder::new(StatusCode::OK).body(html) } -async fn admin() -> impl Responder { - let admin_panel = templates::AdminPanel {}; +async fn admin(db: web::Data>) -> impl Responder { + let logged_in = db.logged_in().await; + let admin_panel = templates::AdminPanel { logged_in }; let html = admin_panel.render().unwrap(); HttpResponseBuilder::new(StatusCode::OK).body(html) } -async fn create() -> impl Responder { - let create = templates::CreateTask {}; +async fn create(db: web::Data>) -> impl Responder { + let logged_in = db.logged_in().await; + let create = templates::CreateTask { logged_in }; let html = create.render().unwrap(); HttpResponseBuilder::new(StatusCode::OK).body(html) } -async fn login() -> impl Responder { - let login = templates::Login {}; +async fn login(db: web::Data>) -> impl Responder { + let logged_in = db.logged_in().await; + let login = templates::Login { logged_in }; let html = login.render().unwrap(); HttpResponseBuilder::new(StatusCode::OK).body(html) } -async fn register() -> impl Responder { - let register = templates::Register {}; +async fn register(db: web::Data>) -> impl Responder { + let logged_in = db.logged_in().await; + let register = templates::Register { logged_in }; let html = register.render().unwrap(); HttpResponseBuilder::new(StatusCode::OK).body(html) } @@ -107,8 +128,6 @@ async fn create_task( req: web::Form, db: web::Data>, ) -> impl Responder { - tracing::debug!("Got POST request on /api/task/create: {:#?}", req); - let db = db.clone(); let task = req.into_inner(); db.create_task(&task).await; HttpResponse::SeeOther() @@ -117,8 +136,6 @@ async fn create_task( } async fn move_task(req: web::Form, db: web::Data>) -> impl Responder { - tracing::debug!("Got POST request on /api/task/move: {:#?}", req); - let db = db.clone(); let task = req.into_inner(); db.move_task(&task).await; HttpResponse::SeeOther() @@ -127,7 +144,6 @@ async fn move_task(req: web::Form, db: web::Data>) } async fn sort_task(req: web::Form) -> impl Responder { - tracing::debug!("Got POST request on /api/task/sort: {:#?}", req); // TODO: save selected categories in a cookie or something HttpResponse::SeeOther() .insert_header(("Location", "/task")) @@ -135,10 +151,9 @@ async fn sort_task(req: web::Form) -> impl Responder { } async fn login_user(req: web::Form, db: web::Data>) -> impl Responder { - tracing::debug!("Got POST request on /api/login"); db.login(&req.username, &req.password).await; HttpResponse::SeeOther() - .insert_header(("Location", "/task")) + .insert_header(("Location", "/")) .finish() } @@ -146,10 +161,16 @@ async fn register_user( req: web::Form, db: web::Data>, ) -> impl Responder { - tracing::debug!("Got POST request on /api/register"); - let db = db.clone(); db.register(&req.username, &req.password).await; HttpResponse::SeeOther() - .insert_header(("Location", "/task")) + .insert_header(("Location", "/")) .finish() } + +async fn logout(db: web::Data>) -> impl Responder { + // TODO: log the user out + HttpResponse::SeeOther() + .insert_header(("Location", "/")) + .finish() +} + diff --git a/templates/navbar.html b/templates/navbar.html index f698679..c4d8c3c 100644 --- a/templates/navbar.html +++ b/templates/navbar.html @@ -9,8 +9,14 @@ navbar
admin panel - login | - register + {% match logged_in %} + {% when Some with (user) %} + Logged in as {{ user }} | + logout + {% when None %} + login | + register + {% endmatch %} diff --git a/templates/admin.html b/templates/site/admin/admin.html similarity index 100% rename from templates/admin.html rename to templates/site/admin/admin.html diff --git a/templates/index.html b/templates/site/index.html similarity index 100% rename from templates/index.html rename to templates/site/index.html diff --git a/templates/login.html b/templates/site/login/login.html similarity index 100% rename from templates/login.html rename to templates/site/login/login.html diff --git a/templates/register.html b/templates/site/login/register.html similarity index 100% rename from templates/register.html rename to templates/site/login/register.html diff --git a/templates/task/create_task.html b/templates/site/task/create_task.html similarity index 100% rename from templates/task/create_task.html rename to templates/site/task/create_task.html diff --git a/templates/task/task.html b/templates/site/task/task.html similarity index 100% rename from templates/task/task.html rename to templates/site/task/task.html diff --git a/templates/task/task_page.html b/templates/site/task/task_page.html similarity index 94% rename from templates/task/task_page.html rename to templates/site/task/task_page.html index cbcf945..a071e8c 100644 --- a/templates/task/task_page.html +++ b/templates/site/task/task_page.html @@ -22,7 +22,7 @@
    {% for task in tasks %} - {% include "task/task.html" %} + {% include "site/task/task.html" %} {% endfor %}
diff --git a/templates/users.html b/templates/site/users/users.html similarity index 100% rename from templates/users.html rename to templates/site/users/users.html