diff --git a/Cargo.toml b/Cargo.toml index 8ec6d52..f13c1dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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.33-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.16-snapshot" } +beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.34-snapshot" } +beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.17-snapshot" } [build-dependencies] diff --git a/src/member/service.rs b/src/member/service.rs index 99da44c..41a1e4a 100644 --- a/src/member/service.rs +++ b/src/member/service.rs @@ -29,6 +29,8 @@ impl Service { self.list_members(), self.get_member(), self.get_member_by_username(), + self.update_member(), + self.delete_member(), ) .map(|_| ()) } @@ -378,4 +380,204 @@ impl Service { Ok(()) } + + async fn update_member(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::member::SUBJECT_UPDATE_MEMBER, + 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::backend::member::UpdateMemberRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_update_member_req = bpr::ss::member::member::UpdateMemberRequest { + client: Some(client), + site_id: req.site_id, + member_level_id: req.member_level_id, + password: req.password, + mobile_phone_number: req.mobile_phone_number, + state: req.state, + }; + + let ss_update_member_res_msg = self + .connection_broker + .request( + bpr::ss::member::member::SUBJECT_UPDATE_MEMBER, + ss_update_member_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_update_member_res = bpr::ss::member::member::UpdateMemberResponse::decode( + ss_update_member_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_update_member_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_update_member_res.result { + message + .respond( + bpr::c2se::backend::member::UpdateMemberResponse { + error: None, + result: Some(bpr::c2se::backend::member::update_member_response::Result { + member: r.member, + }), + } + .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::backend::member::UpdateMemberResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn delete_member(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::member::SUBJECT_DELETE_MEMBER, + 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::backend::member::DeleteMemberRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_delete_member_req = bpr::ss::member::member::DeleteMemberRequest { + client: Some(client), + id: req.id, + }; + + let ss_delete_member_res_msg = self + .connection_broker + .request( + bpr::ss::member::member::SUBJECT_DELETE_MEMBER, + ss_delete_member_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_delete_member_res = bpr::ss::member::member::DeleteMemberResponse::decode( + ss_delete_member_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_delete_member_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_delete_member_res.result { + message + .respond( + bpr::c2se::backend::member::DeleteMemberResponse { + error: None, + result: Some(bpr::c2se::backend::member::delete_member_response::Result {}), + } + .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::backend::member::DeleteMemberResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } }