修改封装ResResult

This commit is contained in:
李运家 2024-06-05 16:27:08 +08:00
parent 1661a71f21
commit b1b64558fe
6 changed files with 29 additions and 29 deletions

View File

@ -1,22 +1,22 @@
use axum::Json;
use axum_extra::extract::WithRejection;
use domain::dto::account::{AuthenticateGooleAccountReq, AuthenticateWithPassword};
use library::resp::{rejection::IRejection, response::{ ResOK, ResResult}};
use library::resp::{rejection::IRejection, response::{ ResData, ResResult}};
use validator::Validate;
pub async fn authenticate_google(
WithRejection(Json(req), _): IRejection<Json<AuthenticateGooleAccountReq>>
) -> ResResult<ResOK<()>> {
) -> ResResult<ResData<()>> {
req.validate()?;
service::account::authenticate_google(req).await?;
Ok(ResOK::none())
Ok(ResData::none())
}
pub async fn authenticate_with_password(
WithRejection(Json(req), _): IRejection<Json<AuthenticateWithPassword>>
) -> ResResult<ResOK<(String, String)>> {
) -> ResResult<ResData<(String, String)>> {
req.validate()?;
service::sys_account::authenticate_with_password(req).await

View File

@ -7,23 +7,22 @@ use domain::dto::pageable::PageParams;
use domain::entities::feedback::Feedback;
use library::resp::pageable::Pageable;
use library::resp::rejection::IRejection;
use library::resp::response::{ResErr, ResOK, ResResult};
use library::resp::response::{ResErr, ResData, ResResult};
/// 添加反馈信息
pub async fn add_feedback(
WithRejection(Json(req), _): IRejection<Json<FeedbackAdd>>
) -> ResResult<ResOK<()>> {
) -> ResResult<ResData<()>> {
if let Err(err) = req.validate() {
return Err(ResErr::ErrParams(Some(err.to_string())));
}
service::feedback::add_feedback(req).await?;
Ok(ResOK(None))
service::feedback::add_feedback(req).await
}
/// 获取反馈信息列表
pub async fn get_feedback_list_by_page(
Query(page_params): Query<PageParams>
) -> ResResult<ResOK<Pageable<Feedback>>> {
) -> ResResult<ResData<Pageable<Feedback>>> {
service::feedback::get_feedback_list_by_page(page_params.page, page_params.page_size).await
}

View File

@ -8,28 +8,28 @@ use serde::Serialize;
use super::status::Status;
pub struct ResOK<T>(pub Option<T>)
pub struct ResData<T>(pub Option<T>)
where
T: Serialize;
impl<T> IntoResponse for ResOK<T>
impl<T> IntoResponse for ResData<T>
where
T: Serialize,
{
fn into_response(self) -> Response {
let ResOK(data) = self;
let ResData(data) = self;
let status = Status::OK(data);
Json(status.to_reply()).into_response()
}
}
impl <T: Serialize> ResOK<T> {
impl <T: Serialize> ResData<T> {
pub fn none() -> Self {
ResOK(None)
ResData(None)
}
pub fn some(data: T) -> Self {
ResOK(Some(data))
ResData(Some(data))
}
}
@ -230,4 +230,5 @@ impl ResErr {
}
}
pub type ResResult<T> = std::result::Result<T, ResErr>;

View File

@ -3,13 +3,13 @@ use domain::dto::account::AuthenticateGooleAccountReq;
use domain::entities::account::Account;
use library::cache::login_cache::{LoginAccount, LOGIN_CACHE};
use library::resp::response::ResErr::ErrPerm;
use library::resp::response::{ResErr, ResOK, ResResult};
use library::resp::response::{ResErr, ResData, ResResult};
use library::social::google::GOOGLE_SOCIAL;
use library::{db, token};
pub async fn authenticate_google(
req: AuthenticateGooleAccountReq,
) -> ResResult<ResOK<(String, String)>> {
) -> ResResult<ResData<(String, String)>> {
let verify_result = GOOGLE_SOCIAL
.verify_id_token(&req.id_token.unwrap())
.await
@ -58,5 +58,5 @@ pub async fn authenticate_google(
)
.await;
return Ok(ResOK::some((token, refresh_token)));
return Ok(ResData::some((token, refresh_token)));
}

View File

@ -2,17 +2,17 @@ use domain::dto::feedback::FeedbackAdd;
use domain::entities::feedback::Feedback;
use library::db;
use library::resp::pageable::Pageable;
use library::resp::response::{ResOK, ResResult};
use library::resp::response::{ResData, ResResult};
/// 获取反馈信息列表
pub async fn get_feedback_list_by_page(page: i64, page_size: i64) -> ResResult<ResOK<Pageable<Feedback>>> {
pub async fn get_feedback_list_by_page(page: i64, page_size: i64) -> ResResult<ResData<Pageable<Feedback>>> {
let feedback_list = Feedback::search_feedback(page, page_size, db!()).await.ok();
if feedback_list.is_none() {
tracing::error!("反馈信息为空");
return Ok(ResOK::some(Pageable::<Feedback>::empty()));
return Ok(ResData::some(Pageable::<Feedback>::empty()));
}
let total = get_feedback_count().await;
Ok(ResOK::some(Pageable::new(feedback_list.unwrap(), total)))
Ok(ResData::some(Pageable::new(feedback_list.unwrap(), total)))
}
/// 获取反馈信息总数
@ -26,7 +26,7 @@ async fn get_feedback_count() -> i64 {
}
/// 添加反馈信息
pub async fn add_feedback(req: FeedbackAdd) -> ResResult<ResOK<()>> {
pub async fn add_feedback(req: FeedbackAdd) -> ResResult<ResData<()>> {
match Feedback::add_feedback(&mut Feedback{
user_id: req.user_id.unwrap(),
content: req.content.unwrap(),
@ -41,5 +41,5 @@ pub async fn add_feedback(req: FeedbackAdd) -> ResResult<ResOK<()>> {
}
}
Ok(ResOK::none())
Ok(ResData::none())
}

View File

@ -4,13 +4,13 @@ use domain::{
entities::account::{Account, Role},
};
use library::{
cache::login_cache::{LoginAccount, LOGIN_CACHE}, db, resp::response::{ResErr, ResOK, ResResult}, token::{generate_refresh_token, generate_token}
cache::login_cache::{LoginAccount, LOGIN_CACHE}, db, resp::response::{ResErr, ResData, ResResult}, token::{generate_refresh_token, generate_token}
};
pub async fn authenticate_with_password(
req: AuthenticateWithPassword,
) -> ResResult<ResOK<(String, String)>> {
) -> ResResult<ResData<(String, String)>> {
let account =
Account::find_with_password(req.username.unwrap(), req.password.unwrap(), db!()).await?;
@ -41,10 +41,10 @@ pub async fn authenticate_with_password(
)
.await;
Ok(ResOK(Some((token, refresh_token))))
Ok(ResData(Some((token, refresh_token))))
}
pub async fn add_account() -> ResResult<ResOK<()>> {
pub async fn add_account() -> ResResult<ResData<()>> {
let account = Account::add_account(
&Account {
username: "admin".to_string(),
@ -59,5 +59,5 @@ pub async fn add_account() -> ResResult<ResOK<()>> {
tracing::error!("添加用户失败");
return Err(ResErr::service("添加用户失败"));
}
Ok(ResOK(None))
Ok(ResData(None))
}