Skip to content

Commit

Permalink
引入merge_web_param macro rules,兼容支持命名空间操作接口参数从url取 #127
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Aug 16, 2024
1 parent 4803ca7 commit 6d74474
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/common/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[macro_export]
macro_rules! merge_web_param {
($param:expr,$payload:expr) => {{
let _body = match crate::common::web_utils::get_req_body($payload).await {
Ok(v) => v,
Err(err) => {
return actix_web::HttpResponse::InternalServerError().body(err.to_string());
}
};
let _b = match serde_urlencoded::from_bytes(&_body) {
Ok(v) => v,
Err(err) => {
return actix_web::HttpResponse::InternalServerError().body(err.to_string());
}
};
$param.merge(_b)
}};
}
1 change: 1 addition & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod datetime_utils;
pub mod delay_notify;
pub mod hash_utils;
pub mod limiter_utils;
pub mod macros;
pub mod model;
pub mod option_utils;
pub mod protobuf_utils;
Expand Down
38 changes: 32 additions & 6 deletions src/openapi/v1/console/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::common::string_utils::StringUtils;
use crate::config::core::ConfigActor;
use crate::console::model::{ConsoleResult, NamespaceInfo};
use crate::console::NamespaceUtils;
use crate::merge_web_param;
use actix::Addr;
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -58,6 +59,25 @@ pub struct NamespaceParam {
pub namespace_desc: Option<String>,
}

impl NamespaceParam {
pub fn merge(self, other: Self) -> Self {
Self {
custom_namespace_id: OptionUtils::select(
self.custom_namespace_id,
other.custom_namespace_id,
),
namespace: OptionUtils::select(self.namespace, other.namespace),
namespace_id: OptionUtils::select(self.namespace_id, other.namespace_id),
namespace_name: OptionUtils::select(self.namespace_name, other.namespace_name),
namespace_show_name: OptionUtils::select(
self.namespace_show_name,
other.namespace_show_name,
),
namespace_desc: OptionUtils::select(self.namespace_desc, other.namespace_desc),
}
}
}

impl From<NamespaceParam> for NamespaceInfo {
fn from(value: NamespaceParam) -> Self {
Self {
Expand All @@ -83,10 +103,12 @@ pub async fn query_namespace_list(config_addr: web::Data<Addr<ConfigActor>>) ->
}

pub async fn add_namespace(
param: web::Form<NamespaceParam>,
web::Query(param): web::Query<NamespaceParam>,
payload: web::Payload,
app_data: web::Data<Arc<AppShareData>>,
) -> impl Responder {
let mut param: NamespaceInfo = param.0.into();
let param = merge_web_param!(param, payload);
let mut param: NamespaceInfo = param.into();
if StringUtils::is_option_empty(&param.namespace_id) {
param.namespace_id = Some(Uuid::new_v4().to_string());
}
Expand All @@ -101,10 +123,12 @@ pub async fn add_namespace(
}

pub async fn update_namespace(
param: web::Form<NamespaceParam>,
web::Query(param): web::Query<NamespaceParam>,
payload: web::Payload,
app_data: web::Data<Arc<AppShareData>>,
) -> impl Responder {
match NamespaceUtils::update_namespace(&app_data, param.0.into()).await {
let param = merge_web_param!(param, payload);
match NamespaceUtils::update_namespace(&app_data, param.into()).await {
Ok(_) => HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body("true"),
Expand All @@ -115,10 +139,12 @@ pub async fn update_namespace(
}

pub async fn remove_namespace(
param: web::Form<NamespaceParam>,
web::Query(param): web::Query<NamespaceParam>,
payload: web::Payload,
app_data: web::Data<Arc<AppShareData>>,
) -> impl Responder {
match NamespaceUtils::remove_namespace(&app_data, param.0.namespace_id).await {
let param = merge_web_param!(param, payload);
match NamespaceUtils::remove_namespace(&app_data, param.namespace_id).await {
Ok(_) => HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body("true"),
Expand Down

0 comments on commit 6d74474

Please sign in to comment.