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},
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<SqlitePool>,
Form(login): Form<LoginPost>,
Form(login): Form<LoginPostForm>,
) -> Result<impl IntoResponse, LoginError> {
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);
}
}

View File

@ -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")]

View File

@ -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<SqlitePool>) -> 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,

View File

@ -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<Watch>,
pub user: Option<User>,
}
#[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<Watch>,
pub user: Option<User>,
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<Watch>,
pub user: Option<User>,
}
#[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<User>,
}