diff --git a/src/login.rs b/src/login.rs index a2dea90..33e9567 100644 --- a/src/login.rs +++ b/src/login.rs @@ -8,9 +8,10 @@ use axum::{ response::{IntoResponse, Redirect, Response}, Form, }; +use serde::{Deserialize, Serialize}; use sqlx::SqlitePool; -use crate::{AuthContext, LoginGet, LoginPost, LogoutGet, LogoutPost, User}; +use crate::{AuthContext, LoginPage, LogoutPage, LogoutSuccess, User}; //-************************************************************************ // Constants @@ -45,6 +46,13 @@ impl IntoResponse for LoginError { } } +// for receiving form submissions +#[derive(Debug, Default, Deserialize, Serialize, PartialEq, Eq)] +pub struct LoginPostForm { + pub username: String, + pub password: String, +} + //-************************************************************************ // Login handlers //-************************************************************************ @@ -54,7 +62,7 @@ impl IntoResponse for LoginError { pub async fn post_login( mut auth: AuthContext, State(pool): State, - Form(login): Form, + Form(login): Form, ) -> Result { let username = &login.username; let username = username.trim(); @@ -82,24 +90,24 @@ pub async fn post_login( } pub async fn get_login() -> impl IntoResponse { - LoginGet::default() + LoginPage::default() } pub async fn get_logout() -> impl IntoResponse { - LogoutGet + LogoutPage } pub async fn post_logout(mut auth: AuthContext) -> impl IntoResponse { if auth.current_user.is_some() { auth.logout().await; } - LogoutPost + LogoutSuccess } #[cfg(test)] mod test { use crate::{ - templates::{LoginGet, LogoutGet, LogoutPost, MainPage}, + templates::{LoginPage, LogoutPage, LogoutSuccess, MainPage}, test_utils::{get_test_user, massage, server, FORM_CONTENT_TYPE}, }; @@ -110,7 +118,7 @@ mod test { let s = server().await; let resp = s.get("/login").await; let body = std::str::from_utf8(resp.bytes()).unwrap().to_string(); - assert_eq!(body, LoginGet::default().to_string()); + assert_eq!(body, LoginPage::default().to_string()); } #[tokio::test] @@ -165,7 +173,7 @@ mod test { let s = server().await; let resp = s.get("/logout").await; let body = std::str::from_utf8(resp.bytes()).unwrap().to_string(); - assert_eq!(body, LogoutGet.to_string()); + assert_eq!(body, LogoutPage.to_string()); } #[tokio::test] @@ -174,7 +182,7 @@ mod test { let resp = s.post("/logout").await; resp.assert_status_ok(); let body = std::str::from_utf8(resp.bytes()).unwrap(); - let default = LogoutPost.to_string(); + let default = LogoutSuccess.to_string(); assert_eq!(body, &default); } @@ -205,7 +213,7 @@ mod test { let resp = s.post("/logout").await; let body = std::str::from_utf8(resp.bytes()).unwrap(); - let default = LogoutPost.to_string(); + let default = LogoutSuccess.to_string(); assert_eq!(body, &default); } } diff --git a/src/templates.rs b/src/templates.rs index 664533f..778d96d 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -19,27 +19,20 @@ pub struct CreateUser { #[template(path = "signup_success.html")] pub struct CreateUserSuccess(pub User); -#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)] -#[template(path = "login_post.html")] -pub struct LoginPost { +#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] +#[template(path = "login_page.html")] +pub struct LoginPage { pub username: String, pub password: String, } #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] -#[template(path = "login_get.html")] -pub struct LoginGet { - pub username: String, - pub password: String, -} +#[template(path = "logout_page.html")] +pub struct LogoutPage; #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] -#[template(path = "logout_get.html")] -pub struct LogoutGet; - -#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] -#[template(path = "logout_post.html")] -pub struct LogoutPost; +#[template(path = "logout_success.html")] +pub struct LogoutSuccess; #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] #[template(path = "index.html")] diff --git a/src/watches/handlers.rs b/src/watches/handlers.rs index 2696e80..d37c991 100644 --- a/src/watches/handlers.rs +++ b/src/watches/handlers.rs @@ -7,8 +7,8 @@ use serde::{de, Deserialize, Deserializer}; use sqlx::{query, query_as, SqlitePool}; use uuid::Uuid; -use super::templates::{GetAddNewWatch, GetSearchWatches, GetWatch}; -use crate::{AuthContext, GetWatches, ShowKind, Watch}; +use super::templates::{AddNewWatchPage, GetWatchPage, SearchWatchesPage}; +use crate::{AuthContext, MyWatchesPage, ShowKind, Watch}; //-************************************************************************ // Constants @@ -100,7 +100,7 @@ pub struct PostAddExistingWatch { //-************************************************************************ pub async fn get_add_new_watch(auth: AuthContext) -> impl IntoResponse { - GetAddNewWatch { + AddNewWatchPage { user: auth.current_user, } } @@ -185,7 +185,7 @@ pub async fn get_watch( .await .ok(); - GetWatch { + GetWatchPage { watch, user: auth.current_user, } @@ -204,7 +204,7 @@ pub async fn get_watches(auth: AuthContext, State(pool): State) -> i vec![] }; - GetWatches { watches, user } + MyWatchesPage { watches, user } } pub async fn get_search_watch( @@ -227,7 +227,7 @@ pub async fn get_search_watch( .await .unwrap_or_default(); - GetSearchWatches { + SearchWatchesPage { watches, user, search, diff --git a/src/watches/templates.rs b/src/watches/templates.rs index 2cd4651..6dbf83f 100644 --- a/src/watches/templates.rs +++ b/src/watches/templates.rs @@ -4,29 +4,29 @@ use serde::{Deserialize, Serialize}; use crate::{OptionalOptionalUser, User, Watch}; #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] -#[template(path = "get_watches.html")] -pub struct GetWatches { +#[template(path = "my_watches_page.html")] +pub struct MyWatchesPage { pub watches: Vec, pub user: Option, } #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] -#[template(path = "get_search_watches.html")] -pub struct GetSearchWatches { +#[template(path = "search_watches_page.html")] +pub struct SearchWatchesPage { pub watches: Vec, pub user: Option, pub search: String, } #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] -#[template(path = "get_watch.html")] -pub struct GetWatch { +#[template(path = "get_watch_page.html")] +pub struct GetWatchPage { pub watch: Option, pub user: Option, } #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] -#[template(path = "get_add_new_watch.html")] -pub struct GetAddNewWatch { +#[template(path = "add_new_watch_page.html")] +pub struct AddNewWatchPage { pub user: Option, } diff --git a/templates/get_add_new_watch.html b/templates/add_new_watch_page.html similarity index 100% rename from templates/get_add_new_watch.html rename to templates/add_new_watch_page.html diff --git a/templates/get_watch.html b/templates/get_watch_page.html similarity index 100% rename from templates/get_watch.html rename to templates/get_watch_page.html diff --git a/templates/login_get.html b/templates/login_page.html similarity index 100% rename from templates/login_get.html rename to templates/login_page.html diff --git a/templates/logout_get.html b/templates/logout_page.html similarity index 100% rename from templates/logout_get.html rename to templates/logout_page.html diff --git a/templates/logout_post.html b/templates/logout_success.html similarity index 100% rename from templates/logout_post.html rename to templates/logout_success.html diff --git a/templates/get_watches.html b/templates/my_watches_page.html similarity index 100% rename from templates/get_watches.html rename to templates/my_watches_page.html diff --git a/templates/get_search_watches.html b/templates/search_watches_page.html similarity index 100% rename from templates/get_search_watches.html rename to templates/search_watches_page.html