list_vendors is added

This commit is contained in:
병준 박 2022-08-18 07:04:33 +00:00
parent fcc5057e22
commit 6b167b37d8
4 changed files with 187 additions and 2 deletions

View File

@ -19,7 +19,7 @@ prost = { version = "0" }
nats = { version = "0" }
base64 = { version = "0" }
beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.63-snapshot" }
beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.47-snapshot" }
beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.64-snapshot" }
beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.48-snapshot" }
[build-dependencies]

1
src/api/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod service;

179
src/api/service.rs Normal file
View File

@ -0,0 +1,179 @@
use beteran_common_rust as bcr;
use beteran_protobuf_rust as bpr;
use prost::Message;
///
pub struct Service {
connection_broker: nats::asynk::Connection,
queue_broker: String,
}
impl std::fmt::Debug for Service {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("beteran-backend-server-edge::vendor::service::Service")
.finish()
}
}
impl Service {
///
pub fn new(connection_broker: nats::asynk::Connection, queue_broker: String) -> Service {
Service {
connection_broker,
queue_broker,
}
}
pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
futures::try_join!(self.list_vendors(),).map(|_| ())
}
fn get_client_in_header(
&self,
message: &nats::asynk::Message,
) -> Result<bpr::models::core::network::Client, bcr::error::rpc::Error> {
match &message.headers {
Some(headers) => {
let client = match headers.get(bpr::c2se::core::network::HEADER_CLIENT) {
Some(c) => {
let msg = base64::decode(c).map_err(|e| {
bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse {
message: format!("invalid header: {}", e),
})
})?;
bpr::models::core::network::Client::decode(msg.as_slice()).map_err(|e| {
bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse {
message: format!("invalid header: {}", e),
})
})?
}
None => {
return Err(bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse {
message: "invalid client information".to_string(),
}));
}
};
Ok(client)
}
None => Err(bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse {
message: "invalid header".to_string(),
})),
}
}
async fn list_vendors(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::backend::api::vendor::SUBJECT_LIST_VENDORS,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let client = self.get_client_in_header(&message)?;
let req = bpr::c2se::api::vendor::ListVendorsRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let ss_list_vendors_req = bpr::ss::api::vendor::ListVendorsRequest {
client: Some(client),
request: Some(bpr::ss::api::vendor::list_vendors_request::Request {
pagination: req.pagination,
sorts: req.sorts,
search: match req.search {
Some(s) => Some(
bpr::ss::api::vendor::list_vendors_request::request::Search {
name_like: s.name_like,
company_id: s.company_id,
vendor_id: s.vendor_id,
key_like: s.key_like,
category_like: s.category_like,
is_enable: s.is_enable,
},
),
None => None,
},
}),
};
let ss_list_vendors_res_msg = self
.connection_broker
.request(
bpr::ss::api::vendor::SUBJECT_LIST_VENDORS,
ss_list_vendors_req.encode_to_vec(),
)
.await
.map_err(|e| {
bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
code: bpr::protobuf::rpc::Error::SERVER_00,
message: format!("server {}", e),
data: None,
})
})?;
let ss_list_vendors_res = bpr::ss::api::vendor::ListVendorsResponse::decode(
ss_list_vendors_res_msg.data.as_slice(),
)
.map_err(|e| {
bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
code: bpr::protobuf::rpc::Error::SERVER_00,
message: format!("server {}", e),
data: None,
})
})?;
if let Some(e) = ss_list_vendors_res.error {
return Err(bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
code: bpr::protobuf::rpc::Error::SERVER_00,
message: format!("server {}", e),
data: None,
}));
}
if let Some(r) = ss_list_vendors_res.result {
message
.respond(
bpr::c2se::api::vendor::ListVendorsResponse {
error: None,
result: Some(bpr::c2se::api::vendor::list_vendors_response::Result {
vendors: r.vendors,
}),
}
.encode_to_vec(),
)
.await
.map_err(|e| {
bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
code: bpr::protobuf::rpc::Error::SERVER_00,
message: format!("server {}", e),
data: None,
})
})?;
}
Ok::<(), bcr::error::rpc::Error>(())
}
.await
{
message
.respond(
bpr::c2se::api::vendor::ListVendorsResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}

View File

@ -10,6 +10,8 @@ mod member_class;
mod member_level;
mod site;
mod api;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let url_broker = match env::var_os("URL_BROKER") {
@ -41,6 +43,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
member_level::service::Service::new(connection_broker.clone(), queue_broker.clone());
let site_service = site::service::Service::new(connection_broker.clone(), queue_broker.clone());
let api_service = api::service::Service::new(connection_broker.clone(), queue_broker.clone());
println!("Server edge[beteran-backend-server-edge] is started");
futures::try_join!(
@ -53,6 +57,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
member_class_service.subscribe(),
member_level_service.subscribe(),
site_service.subscribe(),
api_service.subscribe(),
)?;
Ok(())