From eab580793ff7a94b81aa9cfa830485990506c252 Mon Sep 17 00:00:00 2001 From: Fankai Liu Date: Fri, 20 Oct 2023 22:55:39 +0800 Subject: [PATCH] update template --- src/template/src/dtos/user.hbs | 10 +- src/template/src/routers/mod.hbs | 50 +++---- src/template/src/routers/user.hbs | 141 +++++++++++--------- src/template/templates/login.hbs | 2 +- src/template/templates/user_list.hbs | 152 ++++++++++++++-------- src/template/templates/user_list_page.hbs | 33 +---- 6 files changed, 210 insertions(+), 178 deletions(-) diff --git a/src/template/src/dtos/user.hbs b/src/template/src/dtos/user.hbs index c45fea8..ddbcfd2 100644 --- a/src/template/src/dtos/user.hbs +++ b/src/template/src/dtos/user.hbs @@ -1,4 +1,4 @@ -use salvo::prelude::ToSchema; +use salvo::prelude::{ToSchema, Extractible}; use serde::{Deserialize, Serialize}; #[derive(Deserialize, Debug, ToSchema, Default)] @@ -13,17 +13,15 @@ pub struct UserLoginRequest { pub password: String, } -#[derive(Debug, Deserialize, ToSchema, Default)] +#[derive(Debug, Deserialize,Extractible,ToSchema, Default)] +#[salvo(extract(default_source(from = "body", format = "json")))] pub struct UserUpdateRequest { + #[salvo(extract(source(from = "param")))] pub id: String, pub username: String, pub password: String, } -#[derive(Debug, Deserialize, ToSchema, Default)] -pub struct UserDeleteRequest { - pub id: String, -} #[derive(Debug, Serialize, ToSchema, Default)] pub struct UserResponse { diff --git a/src/template/src/routers/mod.hbs b/src/template/src/routers/mod.hbs index 04ae14c..39f1739 100644 --- a/src/template/src/routers/mod.hbs +++ b/src/template/src/routers/mod.hbs @@ -5,7 +5,6 @@ use salvo::{ prelude::{CatchPanic, Logger, OpenApi, SwaggerUi}, Router, }; - use self::{ demo::hello, {{#if need_db_conn}} @@ -27,35 +26,36 @@ pub mod user; {{/if}} pub fn router() -> Router { + {{#if is_web_site}} + {{#if need_db_conn}} + let mut no_auth_routers = vec![ + Router::with_path("login").get(login_page), + Router::with_path("/api/login").post(post_login), + ]; + + let mut need_auth_routers = vec![ + Router::with_path("users") + .post(post_add_user) + .get(user_list_page), + Router::with_path("/api/users") + .get(get_users) + .put(post_update_user) + .push(Router::with_path("").delete(delete_user)), + ]; let router = Router::new() .hoop(Logger::new()) .hoop(CatchPanic::new()) - {{#if need_db_conn}} .get(hello) - {{#if is_web_site}} - .push(Router::with_path("login").get(login_page).post(post_login)) - {{else}} - .push(Router::with_path("login").post(post_login)) - {{/if}} - .push(user_router().hoop(jwt_hoop())); - {{else}} - .get(hello); - {{/if}} + .append(&mut no_auth_routers) + .push( + Router::new() + .append(&mut need_auth_routers) + .hoop(jwt_hoop()), + ); + {{/if}} + {{/if}} let doc = OpenApi::new("salvo web api", "0.0.1").merge_router(&router); router .push(doc.into_router("/api-doc/openapi.json")) .push(SwaggerUi::new("/api-doc/openapi.json").into_router("swagger-ui")) -} -{{#if need_db_conn}} -pub fn user_router() -> Router { - Router::with_path("user") - .post(post_add_user) - .put(post_update_user) - .delete(delete_user) - {{#if is_web_site}} - .get(user_list_page) - {{else}} - .get(get_users) - {{/if}} -} -{{/if}} \ No newline at end of file +} \ No newline at end of file diff --git a/src/template/src/routers/user.hbs b/src/template/src/routers/user.hbs index 8ad1089..d4d4ef1 100644 --- a/src/template/src/routers/user.hbs +++ b/src/template/src/routers/user.hbs @@ -1,48 +1,24 @@ {{#if is_web_site}} -use askama::Template; -{{/if}} -{{#if is_web_site}} -use salvo::{ - endpoint, - http::cookie::Cookie, - oapi::extract::{FormBody, JsonBody}, - writing::{Redirect, Text}, - Response, -}; -use crate::{ - app_error::AppResult, - app_response::{ErrRes, Res}, - dtos::user::{ - UserAddRequest, UserDeleteRequest, UserLoginRequest, UserResponse, UserUpdateRequest, - }, - services::user, -}; -{{else}} -use salvo::{ - endpoint, - oapi::extract::{FormBody, JsonBody}, - Response, -}; -use crate::{ - app_response::{ErrRes, Res}, - dtos::user::{ - UserAddRequest, UserDeleteRequest, UserLoginRequest, UserUpdateRequest, - }, - services::user, -}; -{{/if}} -{{#if is_web_site}} #[derive(Template)] #[template(path = "login.html")] struct LoginTemplate {} #[endpoint] pub async fn login_page(res: &mut Response) -> AppResult<()> { + let cookies = res.cookies(); + let cookie = cookies.get("jwt_token"); + if let Some(cookie) = cookie { + let token = cookie.value().to_string(); + if decode_token(&token) { + res.render(Redirect::other("/users")); + return Ok(()); + } else { + } + } let hello_tmpl = LoginTemplate {}; res.render(Text::Html(hello_tmpl.render().unwrap())); Ok(()) } -{{/if}} #[endpoint] pub async fn post_add_user(req: JsonBody, res: &mut Response) { @@ -54,8 +30,9 @@ pub async fn post_add_user(req: JsonBody, res: &mut Response) { } #[endpoint] -pub async fn post_update_user(req: JsonBody, res: &mut Response) { - let result = user::update_user(req.0).await; +pub async fn put_update_user(req: &mut Request, res: &mut Response) { + let req: UserUpdateRequest = req.extract().await.unwrap(); + let result = user::update_user(req).await; match result { Ok(data) => Res::with_data(data).into_response(res), Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), @@ -63,8 +40,8 @@ pub async fn post_update_user(req: JsonBody, res: &mut Respon } #[endpoint] -pub async fn delete_user(req: JsonBody, res: &mut Response) { - let result = user::delete_user(req.0).await; +pub async fn delete_user(id: PathParam, res: &mut Response) { + let result = user::delete_user(id.0).await; match result { Ok(_) => Res::with_data(()).into_response(res), Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), @@ -79,26 +56,11 @@ pub async fn get_users(res: &mut Response) { Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), } } -{{#if is_web_site}} - -#[derive(Template)] -#[template(path = "user_list_page.html")] -pub struct UserListPageTemplate { - users: Vec, -} - -#[endpoint] -pub async fn user_list_page(res: &mut Response) -> AppResult<()> { - let users = user::users().await?; - let hello_tmpl = UserListPageTemplate { users }; - - res.render(Text::Html(hello_tmpl.render().unwrap())); - Ok(()) -} #[endpoint] -pub async fn post_login(req: FormBody, res: &mut Response) { - let result = user::login(req.0).await; +pub async fn post_login(req: JsonBody, res: &mut Response) { + let result: Result = + user::login(req.0).await; match result { Ok(data) => { let jwt_token = data.token.clone(); @@ -107,15 +69,76 @@ pub async fn post_login(req: FormBody, res: &mut Response) { .http_only(true) .finish(); res.add_cookie(cookie); - res.render(Redirect::other("/user")); } Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), } } +#[derive(Template)] +#[template(path = "user_list_page.html")] +pub struct UserListPageTemplate {} + +#[derive(Template)] +#[template(path = "user_list.html")] +pub struct UserListTemplate {} + +#[endpoint] +pub async fn user_list_page(req: &mut Request, res: &mut Response) -> AppResult<()> { + let is_fragment = req.headers().get("X-Fragment-Header"); + match is_fragment { + Some(_) => { + let hello_tmpl = UserListTemplate {}; + res.render(Text::Html(hello_tmpl.render().unwrap())); + } + None => { + let hello_tmpl = UserListPageTemplate {}; + res.render(Text::Html(hello_tmpl.render().unwrap())); + } + } + Ok(()) +} {{else}} +use salvo::{ + endpoint, + oapi::extract::{FormBody, JsonBody}, + Response, +}; +use crate::{ + app_response::{ErrRes, Res}, + dtos::user::{ + UserAddRequest, UserDeleteRequest, UserLoginRequest, UserUpdateRequest, + }, + services::user, +}; #[endpoint] -pub async fn post_login(req: FormBody, res: &mut Response) { - let result = user::login(req.0).await; +pub async fn post_add_user(req: JsonBody, res: &mut Response) { + let result = user::add_user(req.0).await; + match result { + Ok(data) => Res::with_data(data).into_response(res), + Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), + } +} + +#[endpoint] +pub async fn post_update_user(req: JsonBody, res: &mut Response) { + let result = user::update_user(req.0).await; + match result { + Ok(data) => Res::with_data(data).into_response(res), + Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), + } +} + +#[endpoint] +pub async fn delete_user(req: JsonBody, res: &mut Response) { + let result = user::delete_user(req.0).await; + match result { + Ok(_) => Res::with_data(()).into_response(res), + Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), + } +} + +#[endpoint] +pub async fn get_users(res: &mut Response) { + let result = user::users().await; match result { Ok(data) => Res::with_data(data).into_response(res), Err(e) => ErrRes::with_err(&e.to_string()).into_response(res), diff --git a/src/template/templates/login.hbs b/src/template/templates/login.hbs index ac66f49..30e67cb 100644 --- a/src/template/templates/login.hbs +++ b/src/template/templates/login.hbs @@ -113,7 +113,7 @@ }, }); this.userList = await userListResponse.text(); - history.pushState(null, '', '/user'); + //history.pushState(null, '', '/user'); } catch (error) { Swal.fire({ title: "Error!", diff --git a/src/template/templates/user_list.hbs b/src/template/templates/user_list.hbs index dd02a58..69d9a20 100644 --- a/src/template/templates/user_list.hbs +++ b/src/template/templates/user_list.hbs @@ -1,4 +1,4 @@ -
+
@@ -7,8 +7,7 @@
@@ -71,38 +60,87 @@
+ diff --git a/src/template/templates/user_list_page.hbs b/src/template/templates/user_list_page.hbs index d7f65dc..7958716 100644 --- a/src/template/templates/user_list_page.hbs +++ b/src/template/templates/user_list_page.hbs @@ -7,34 +7,7 @@ salvo {% include "user_list.html" %} - - - - - - - + + +