more template renaming and tidying

This commit is contained in:
Joe Ardent 2023-06-20 12:14:18 -07:00
parent 987d0937c9
commit 8e54c6f99a
11 changed files with 39 additions and 38 deletions

View File

@ -8,9 +8,10 @@ use axum::{
response::{IntoResponse, Redirect, Response}, response::{IntoResponse, Redirect, Response},
Form, Form,
}; };
use serde::{Deserialize, Serialize};
use sqlx::SqlitePool; use sqlx::SqlitePool;
use crate::{AuthContext, LoginGet, LoginPost, LogoutGet, LogoutPost, User}; use crate::{AuthContext, LoginPage, LogoutPage, LogoutSuccess, User};
//-************************************************************************ //-************************************************************************
// Constants // 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 // Login handlers
//-************************************************************************ //-************************************************************************
@ -54,7 +62,7 @@ impl IntoResponse for LoginError {
pub async fn post_login( pub async fn post_login(
mut auth: AuthContext, mut auth: AuthContext,
State(pool): State<SqlitePool>, State(pool): State<SqlitePool>,
Form(login): Form<LoginPost>, Form(login): Form<LoginPostForm>,
) -> Result<impl IntoResponse, LoginError> { ) -> Result<impl IntoResponse, LoginError> {
let username = &login.username; let username = &login.username;
let username = username.trim(); let username = username.trim();
@ -82,24 +90,24 @@ pub async fn post_login(
} }
pub async fn get_login() -> impl IntoResponse { pub async fn get_login() -> impl IntoResponse {
LoginGet::default() LoginPage::default()
} }
pub async fn get_logout() -> impl IntoResponse { pub async fn get_logout() -> impl IntoResponse {
LogoutGet LogoutPage
} }
pub async fn post_logout(mut auth: AuthContext) -> impl IntoResponse { pub async fn post_logout(mut auth: AuthContext) -> impl IntoResponse {
if auth.current_user.is_some() { if auth.current_user.is_some() {
auth.logout().await; auth.logout().await;
} }
LogoutPost LogoutSuccess
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::{ use crate::{
templates::{LoginGet, LogoutGet, LogoutPost, MainPage}, templates::{LoginPage, LogoutPage, LogoutSuccess, MainPage},
test_utils::{get_test_user, massage, server, FORM_CONTENT_TYPE}, test_utils::{get_test_user, massage, server, FORM_CONTENT_TYPE},
}; };
@ -110,7 +118,7 @@ mod test {
let s = server().await; let s = server().await;
let resp = s.get("/login").await; let resp = s.get("/login").await;
let body = std::str::from_utf8(resp.bytes()).unwrap().to_string(); 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] #[tokio::test]
@ -165,7 +173,7 @@ mod test {
let s = server().await; let s = server().await;
let resp = s.get("/logout").await; let resp = s.get("/logout").await;
let body = std::str::from_utf8(resp.bytes()).unwrap().to_string(); 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] #[tokio::test]
@ -174,7 +182,7 @@ mod test {
let resp = s.post("/logout").await; let resp = s.post("/logout").await;
resp.assert_status_ok(); resp.assert_status_ok();
let body = std::str::from_utf8(resp.bytes()).unwrap(); let body = std::str::from_utf8(resp.bytes()).unwrap();
let default = LogoutPost.to_string(); let default = LogoutSuccess.to_string();
assert_eq!(body, &default); assert_eq!(body, &default);
} }
@ -205,7 +213,7 @@ mod test {
let resp = s.post("/logout").await; let resp = s.post("/logout").await;
let body = std::str::from_utf8(resp.bytes()).unwrap(); let body = std::str::from_utf8(resp.bytes()).unwrap();
let default = LogoutPost.to_string(); let default = LogoutSuccess.to_string();
assert_eq!(body, &default); assert_eq!(body, &default);
} }
} }

View File

@ -19,27 +19,20 @@ pub struct CreateUser {
#[template(path = "signup_success.html")] #[template(path = "signup_success.html")]
pub struct CreateUserSuccess(pub User); pub struct CreateUserSuccess(pub User);
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "login_post.html")] #[template(path = "login_page.html")]
pub struct LoginPost { pub struct LoginPage {
pub username: String, pub username: String,
pub password: String, pub password: String,
} }
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "login_get.html")] #[template(path = "logout_page.html")]
pub struct LoginGet { pub struct LogoutPage;
pub username: String,
pub password: String,
}
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "logout_get.html")] #[template(path = "logout_success.html")]
pub struct LogoutGet; pub struct LogoutSuccess;
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "logout_post.html")]
pub struct LogoutPost;
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "index.html")] #[template(path = "index.html")]

View File

@ -7,8 +7,8 @@ use serde::{de, Deserialize, Deserializer};
use sqlx::{query, query_as, SqlitePool}; use sqlx::{query, query_as, SqlitePool};
use uuid::Uuid; use uuid::Uuid;
use super::templates::{GetAddNewWatch, GetSearchWatches, GetWatch}; use super::templates::{AddNewWatchPage, GetWatchPage, SearchWatchesPage};
use crate::{AuthContext, GetWatches, ShowKind, Watch}; use crate::{AuthContext, MyWatchesPage, ShowKind, Watch};
//-************************************************************************ //-************************************************************************
// Constants // Constants
@ -100,7 +100,7 @@ pub struct PostAddExistingWatch {
//-************************************************************************ //-************************************************************************
pub async fn get_add_new_watch(auth: AuthContext) -> impl IntoResponse { pub async fn get_add_new_watch(auth: AuthContext) -> impl IntoResponse {
GetAddNewWatch { AddNewWatchPage {
user: auth.current_user, user: auth.current_user,
} }
} }
@ -185,7 +185,7 @@ pub async fn get_watch(
.await .await
.ok(); .ok();
GetWatch { GetWatchPage {
watch, watch,
user: auth.current_user, user: auth.current_user,
} }
@ -204,7 +204,7 @@ pub async fn get_watches(auth: AuthContext, State(pool): State<SqlitePool>) -> i
vec![] vec![]
}; };
GetWatches { watches, user } MyWatchesPage { watches, user }
} }
pub async fn get_search_watch( pub async fn get_search_watch(
@ -227,7 +227,7 @@ pub async fn get_search_watch(
.await .await
.unwrap_or_default(); .unwrap_or_default();
GetSearchWatches { SearchWatchesPage {
watches, watches,
user, user,
search, search,

View File

@ -4,29 +4,29 @@ use serde::{Deserialize, Serialize};
use crate::{OptionalOptionalUser, User, Watch}; use crate::{OptionalOptionalUser, User, Watch};
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "get_watches.html")] #[template(path = "my_watches_page.html")]
pub struct GetWatches { pub struct MyWatchesPage {
pub watches: Vec<Watch>, pub watches: Vec<Watch>,
pub user: Option<User>, pub user: Option<User>,
} }
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "get_search_watches.html")] #[template(path = "search_watches_page.html")]
pub struct GetSearchWatches { pub struct SearchWatchesPage {
pub watches: Vec<Watch>, pub watches: Vec<Watch>,
pub user: Option<User>, pub user: Option<User>,
pub search: String, pub search: String,
} }
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "get_watch.html")] #[template(path = "get_watch_page.html")]
pub struct GetWatch { pub struct GetWatchPage {
pub watch: Option<Watch>, pub watch: Option<Watch>,
pub user: Option<User>, pub user: Option<User>,
} }
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)] #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "get_add_new_watch.html")] #[template(path = "add_new_watch_page.html")]
pub struct GetAddNewWatch { pub struct AddNewWatchPage {
pub user: Option<User>, pub user: Option<User>,
} }