Skip to content

Commit

Permalink
update AppResponse to AppWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
fankaiLiu committed Mar 5, 2024
1 parent 40e05f6 commit 074ed44
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 42 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "salvo-cli"
version = "0.1.45"
version = "0.1.46"
edition = "2021"
authors = ["Fankai Liu [email protected]","mrxiaozhuox [email protected]"]
keywords = ["salvo", "cli","template"]
Expand All @@ -15,7 +15,7 @@ path = "src/main.rs"

[dependencies]
ansi_term = "0.12.1"
anyhow = "1.0.75"
anyhow = "1.0.80"
clap = { version = "4.4.4", features = ["derive"] }
dialoguer = "0.10.4"
git2 = "0.18.1"
Expand Down
2 changes: 1 addition & 1 deletion locales/readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ src_main_rs:
th: จุดเริ่มต้นของแอปพลิเคชัน ตั้งค่าและเริ่มต้นบริการ
el: Σημείο εισόδου της εφαρμογής, ρυθμίζει και εκκινεί τις υπηρεσίες
da: Programmets indgangspunkt, opsætter og starter tjenester
src_app_response_rs:
src_app_writer_rs:
en: Standardize response
zh_CN: 规范化返回请求
zh_TW: 規範化返回請求
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use serde::Serialize;

use crate::app_error::AppError;

pub struct AppResponse<T>(pub AppResult<T>);
pub struct AppWriter<T>(pub AppResult<T>);

#[async_trait]
impl<T: Serialize + Default + Send> Writer for AppResponse<T> {
impl<T: Serialize + Default + Send> Writer for AppWriter<T> {
async fn write(self, req: &mut Request, depot: &mut Depot, res: &mut Response) {
match self.0 {
Ok(data) => ResponseBuilder::with_data(data).into_response(res),
Expand All @@ -18,7 +18,7 @@ impl<T: Serialize + Default + Send> Writer for AppResponse<T> {
}
}

impl<T: Serialize + Default + Send> EndpointOutRegister for AppResponse<T> {
impl<T: Serialize + Default + Send> EndpointOutRegister for AppWriter<T> {
fn register(_components: &mut salvo::oapi::Components, operation: &mut salvo::oapi::Operation) {
operation
.responses
Expand All @@ -29,15 +29,15 @@ impl<T: Serialize + Default + Send> EndpointOutRegister for AppResponse<T> {
}
}

impl<T> From<AppResult<T>> for AppResponse<T> {
impl<T> From<AppResult<T>> for AppWriter<T> {
fn from(result: AppResult<T>) -> Self {
AppResponse(result)
AppWriter(result)
}
}

impl<T> From<AppError> for AppResponse<T> {
impl<T> From<AppError> for AppWriter<T> {
fn from(result: AppError) -> Self {
AppResponse(Err(result))
AppWriter(Err(result))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/template/src/main_template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod app_error;
{{else}}
#[allow(dead_code)]
{{/if}}
mod app_response;
mod app_writer;
mod config;
{{#if need_db_conn}}
mod db;
Expand Down
2 changes: 1 addition & 1 deletion src/template/src/routers/demo.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use salvo::{oapi::endpoint, writing::Text, Request, Response};
{{else}}
use salvo::oapi::endpoint;
{{/if}}
use crate::app_response::AppResult;
use crate::app_writer::AppResult;
{{#if is_web_site}}

#[derive(Template)]
Expand Down
38 changes: 19 additions & 19 deletions src/template/src/routers/user.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{#if is_web_site}}
use crate::{
app_response::{AppResponse, AppResult, ErrorResponseBuilder},
app_writer::{AppWriter, AppResult, ErrorResponseBuilder},
dtos::user::{
UserAddRequest, UserLoginRequest, UserLoginResponse, UserResponse, UserUpdateRequest,
},
Expand Down Expand Up @@ -78,37 +78,37 @@ pub async fn post_login(req: JsonBody<UserLoginRequest>, res: &mut Response) {
}

#[endpoint(tags("users"))]
pub async fn post_add_user(new_user: JsonBody<UserAddRequest>) -> AppResponse<UserResponse> {
pub async fn post_add_user(new_user: JsonBody<UserAddRequest>) -> AppWriter<UserResponse> {
let result = user::add_user(new_user.0).await;
AppResponse(result)
AppWriter(result)
}

#[endpoint( tags("users"),
parameters(
("id", description = "user id"),
))]
pub async fn put_update_user(req: &mut Request) -> AppResult<AppResponse<UserResponse>> {
pub async fn put_update_user(req: &mut Request) -> AppResult<AppWriter<UserResponse>> {
let req: UserUpdateRequest = req.extract().await?;
let result = user::update_user(req).await;
Ok(AppResponse(result))
Ok(AppWriter(result))
}

#[endpoint(tags("users"))]
pub async fn delete_user(id: PathParam<String>) -> AppResponse<()> {
pub async fn delete_user(id: PathParam<String>) -> AppWriter<()> {
let result = user::delete_user(id.0).await;
AppResponse(result)
AppWriter(result)
}

#[endpoint(tags("users"))]
pub async fn get_users() -> AppResponse<Vec<UserResponse>> {
pub async fn get_users() -> AppWriter<Vec<UserResponse>> {
let result = user::users().await;
AppResponse(result)
AppWriter(result)
}

{{else}}
use crate::{
app_response::ErrorResponseBuilder,
app_response::{AppResponse, AppResult},
app_writer::ErrorResponseBuilder,
app_writer::{AppWriter, AppResult},
dtos::user::{
UserAddRequest, UserLoginRequest, UserLoginResponse, UserResponse, UserUpdateRequest,
},
Expand Down Expand Up @@ -139,30 +139,30 @@ pub async fn post_login(req: JsonBody<UserLoginRequest>, res: &mut Response) {
}

#[endpoint(tags("users"))]
pub async fn post_add_user(new_user: JsonBody<UserAddRequest>) -> AppResponse<UserResponse> {
pub async fn post_add_user(new_user: JsonBody<UserAddRequest>) -> AppWriter<UserResponse> {
let result = user::add_user(new_user.0).await;
AppResponse(result)
AppWriter(result)
}

#[endpoint( tags("users"),
parameters(
("id", description = "user id"),
))]
pub async fn put_update_user(req: &mut Request) -> AppResult<AppResponse<UserResponse>> {
pub async fn put_update_user(req: &mut Request) -> AppResult<AppWriter<UserResponse>> {
let req: UserUpdateRequest = req.extract().await?;
let result = user::update_user(req).await;
Ok(AppResponse(result))
Ok(AppWriter(result))
}

#[endpoint(tags("users"))]
pub async fn delete_user(id: PathParam<String>) -> AppResponse<()> {
pub async fn delete_user(id: PathParam<String>) -> AppWriter<()> {
let result = user::delete_user(id.0).await;
AppResponse(result)
AppWriter(result)
}

#[endpoint(tags("users"))]
pub async fn get_users() -> AppResponse<Vec<UserResponse>> {
pub async fn get_users() -> AppWriter<Vec<UserResponse>> {
let result = user::users().await;
AppResponse(result)
AppWriter(result)
}
{{/if}}
10 changes: 5 additions & 5 deletions src/template/src/services/user.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#if is_sea_orm_or_sqlx}}
{{#if is_sqlx}}
use crate::{
app_response::AppResult,
app_writer::AppResult,
db::DB,
dtos::user::{
UserAddRequest, UserLoginRequest, UserLoginResponse, UserResponse,
Expand All @@ -14,7 +14,7 @@ use crate::{
{{/if}}
{{#if is_sea_orm}}
use crate::{
app_response::AppResult,
app_writer::AppResult,
db::DB,
dtos::user::{
UserAddRequest, UserLoginRequest, UserLoginResponse, UserResponse,
Expand Down Expand Up @@ -223,7 +223,7 @@ use uuid::Uuid;

use crate::schema::users::dsl::users as diesel_users;
use crate::{
app_response::AppResult,
app_writer::AppResult,
db::establish_connection,
dtos::user::{
UserAddRequest, UserLoginRequest, UserLoginResponse, UserResponse, UserUpdateRequest,
Expand Down Expand Up @@ -355,7 +355,7 @@ pub async fn users() -> AppResult<Vec<UserResponse>> {
use uuid::Uuid;

use crate::{
app_response::AppResult,
app_writer::AppResult,
dtos::user::{
UserAddRequest, UserLoginRequest, UserLoginResponse, UserResponse, UserUpdateRequest,
},
Expand Down Expand Up @@ -437,7 +437,7 @@ pub async fn users() -> AppResult<Vec<UserResponse>> {
{{/if}}
{{#if is_mongodb}}
use crate::{
app_response::AppResult,
app_writer::AppResult,
db::{COLL_NAME, DB_NAME, MONGODB_CLIENT},
dtos::user::{
UserAddRequest, UserLoginRequest, UserLoginResponse, UserResponse, UserUpdateRequest,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/create_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ fn create_basic_file(
include_str!("../template/src/app_error.hbs"),
),
(
"src/app_response.rs",
include_str!("../template/src/app_response.hbs"),
"src/app_writer.rs",
include_str!("../template/src/app_writer.hbs"),
),
//src/middleware
(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/directory2md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static PATH_DESCRIPTIONS: Lazy<HashMap<String, String>> = Lazy::new(|| {
m.insert("data".to_string(), t!("data"));
m.insert("assets".to_string(), t!("assets"));
m.insert("src".to_string(), t!("src"));
m.insert("src/app_response.rs".to_string(), t!("src_app_response_rs"));
m.insert("src/app_writer.rs".to_string(), t!("src_app_writer_rs"));
m.insert("src/routers".to_string(), t!("src_routers"));
m.insert("src/middleware".to_string(), t!("src_middleware"));
m.insert("src/utils".to_string(), t!("src_utils"));
Expand Down

0 comments on commit 074ed44

Please sign in to comment.