增加context_extractor,并将XXXX_validator变更为XXXX_extractor
This commit is contained in:
parent
4751d54c96
commit
e0d9d708dc
@ -1,5 +1,5 @@
|
|||||||
### todo
|
### todo
|
||||||
- [*] 使用Extractor方式提取数据,包括Body、Query和Path
|
- [*] 使用Extractor方式提取数据,包括Body、Query和Path
|
||||||
- [ ] multipart/form-data的实现
|
- [ ] multipart/form-data的实现
|
||||||
- [ ] 参考example中的jwt实现方式,移除context对extension的依赖?那么language-tag该怎么处理?
|
- [*] 参考example中的jwt实现方式,移除context对extension的依赖?那么language-tag该怎么处理?
|
||||||
- [ ] 参考rocket,移除参数的元组类型
|
- [ ] 参考rocket,移除参数的元组类型
|
||||||
|
@ -1 +1,2 @@
|
|||||||
pub mod response;
|
pub mod response;
|
||||||
|
pub mod validator;
|
@ -45,7 +45,7 @@ where
|
|||||||
match axum::Json::<T>::from_request(req, state).await {
|
match axum::Json::<T>::from_request(req, state).await {
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
let data = value.0;
|
let data = value.0;
|
||||||
match super::validator::validate_params(&data, lang_tag) {
|
match crate::model::validator::validate_params(&data, lang_tag) {
|
||||||
Ok(_) => return Ok(Self(data)),
|
Ok(_) => return Ok(Self(data)),
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err),
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ where
|
|||||||
match axum::Form::<T>::from_request(req, state).await {
|
match axum::Form::<T>::from_request(req, state).await {
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
let data = value.0;
|
let data = value.0;
|
||||||
match super::validator::validate_params(&data, lang_tag) {
|
match crate::model::validator::validate_params(&data, lang_tag) {
|
||||||
Ok(_) => return Ok(Self(data)),
|
Ok(_) => return Ok(Self(data)),
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err),
|
||||||
}
|
}
|
18
library/src/validator/context_extractor.rs
Normal file
18
library/src/validator/context_extractor.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
use axum::{async_trait, extract::FromRequestParts};
|
||||||
|
use http::request::Parts;
|
||||||
|
|
||||||
|
use crate::{context::Context, model::response::ResErr};
|
||||||
|
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl<S> FromRequestParts<S> for Context
|
||||||
|
where
|
||||||
|
S: Send + Sync,
|
||||||
|
{
|
||||||
|
type Rejection = ResErr;
|
||||||
|
|
||||||
|
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||||
|
let context: &Context = parts.extensions.get().unwrap();
|
||||||
|
Ok(context.clone())
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
pub mod validator;
|
pub mod query_extractor;
|
||||||
pub mod query_validator;
|
pub mod path_extractor;
|
||||||
pub mod path_validator;
|
pub mod body_extractor;
|
||||||
pub mod body_validator;
|
pub mod context_extractor;
|
@ -23,7 +23,7 @@ where
|
|||||||
let context: &Context = parts.extensions.get().unwrap();
|
let context: &Context = parts.extensions.get().unwrap();
|
||||||
|
|
||||||
if let Ok(Query(data)) = query {
|
if let Ok(Query(data)) = query {
|
||||||
match super::validator::validate_params(&data, context.get_lang_tag()) {
|
match crate::model::validator::validate_params(&data, context.get_lang_tag()) {
|
||||||
Ok(_) => Ok(Self(data)),
|
Ok(_) => Ok(Self(data)),
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
@ -1,6 +1,5 @@
|
|||||||
use axum::Extension;
|
|
||||||
use domain::{dto::account::{AuthenticateGooleAccountReq, AuthenticateWithPassword, RefreshToken}, vo::account::{LoginAccount, RefreshTokenResult}};
|
use domain::{dto::account::{AuthenticateGooleAccountReq, AuthenticateWithPassword, RefreshToken}, vo::account::{LoginAccount, RefreshTokenResult}};
|
||||||
use library::{context::Context, model::response::ResResult, validator::body_validator::JsonBody};
|
use library::{context::Context, model::response::ResResult, validator::body_extractor::JsonBody};
|
||||||
|
|
||||||
use crate::service;
|
use crate::service;
|
||||||
|
|
||||||
@ -8,7 +7,7 @@ use crate::service;
|
|||||||
///
|
///
|
||||||
/// google账号登录
|
/// google账号登录
|
||||||
pub async fn authenticate_google(
|
pub async fn authenticate_google(
|
||||||
Extension(context): Extension<Context>,
|
context: Context,
|
||||||
JsonBody(req): JsonBody<AuthenticateGooleAccountReq>
|
JsonBody(req): JsonBody<AuthenticateGooleAccountReq>
|
||||||
) -> ResResult<LoginAccount> {
|
) -> ResResult<LoginAccount> {
|
||||||
service::account_service::authenticate_google(context, req).await
|
service::account_service::authenticate_google(context, req).await
|
||||||
@ -18,7 +17,7 @@ pub async fn authenticate_google(
|
|||||||
///
|
///
|
||||||
/// 账号密码登录
|
/// 账号密码登录
|
||||||
pub async fn authenticate_with_password(
|
pub async fn authenticate_with_password(
|
||||||
Extension(context): Extension<Context>,
|
context: Context,
|
||||||
JsonBody(req): JsonBody<AuthenticateWithPassword>
|
JsonBody(req): JsonBody<AuthenticateWithPassword>
|
||||||
) -> ResResult<LoginAccount> {
|
) -> ResResult<LoginAccount> {
|
||||||
service::sys_account_service::authenticate_with_password(context, req).await
|
service::sys_account_service::authenticate_with_password(context, req).await
|
||||||
@ -28,7 +27,7 @@ pub async fn authenticate_with_password(
|
|||||||
///
|
///
|
||||||
/// 刷新token
|
/// 刷新token
|
||||||
pub async fn refresh_token(
|
pub async fn refresh_token(
|
||||||
Extension(context): Extension<Context>,
|
context: Context,
|
||||||
JsonBody(refresh_token): JsonBody<RefreshToken>
|
JsonBody(refresh_token): JsonBody<RefreshToken>
|
||||||
) -> ResResult<RefreshTokenResult> {
|
) -> ResResult<RefreshTokenResult> {
|
||||||
tracing::debug!("刷新token, {:?}", context);
|
tracing::debug!("刷新token, {:?}", context);
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
use axum::Extension;
|
|
||||||
use domain::dto::feedback::FeedbackAdd;
|
use domain::dto::feedback::FeedbackAdd;
|
||||||
use domain::dto::pageable::PageParams;
|
use domain::dto::pageable::PageParams;
|
||||||
use domain::vo::feedback::FeedbackPageable;
|
use domain::vo::feedback::FeedbackPageable;
|
||||||
use library::context::Context;
|
use library::context::Context;
|
||||||
use library::model::response::ResResult;
|
use library::model::response::ResResult;
|
||||||
use library::validator::body_validator::JsonBody;
|
use library::validator::body_extractor::JsonBody;
|
||||||
use library::validator::query_validator::QueryParams;
|
use library::validator::query_extractor::QueryParams;
|
||||||
|
|
||||||
use crate::service;
|
use crate::service;
|
||||||
|
|
||||||
@ -13,7 +12,7 @@ use crate::service;
|
|||||||
///
|
///
|
||||||
/// 添加反馈信息
|
/// 添加反馈信息
|
||||||
pub async fn add_feedback(
|
pub async fn add_feedback(
|
||||||
Extension(context): Extension<Context>,
|
context: Context,
|
||||||
JsonBody(req): JsonBody<FeedbackAdd>,
|
JsonBody(req): JsonBody<FeedbackAdd>,
|
||||||
) -> ResResult<()> {
|
) -> ResResult<()> {
|
||||||
service::feedback_service::add_feedback(context, req).await
|
service::feedback_service::add_feedback(context, req).await
|
||||||
@ -23,7 +22,7 @@ pub async fn add_feedback(
|
|||||||
///
|
///
|
||||||
/// 获取反馈信息列表
|
/// 获取反馈信息列表
|
||||||
pub async fn get_feedback_list_by_page(
|
pub async fn get_feedback_list_by_page(
|
||||||
Extension(context): Extension<Context>,
|
context: Context,
|
||||||
QueryParams(page_params): QueryParams<PageParams>,
|
QueryParams(page_params): QueryParams<PageParams>,
|
||||||
) -> ResResult<FeedbackPageable> {
|
) -> ResResult<FeedbackPageable> {
|
||||||
service::feedback_service::get_feedback_list_by_page(
|
service::feedback_service::get_feedback_list_by_page(
|
||||||
|
Loading…
Reference in New Issue
Block a user