195 lines
5.7 KiB
Rust
195 lines
5.7 KiB
Rust
use crate::repositories;
|
|
use beteran_common_rust as bcr;
|
|
use beteran_protobuf_rust as bpr;
|
|
use diesel::{
|
|
r2d2::{ConnectionManager, Pool},
|
|
PgConnection,
|
|
};
|
|
use prost::Message;
|
|
|
|
///
|
|
pub struct Service {
|
|
connection_broker: nats::asynk::Connection,
|
|
queue_broker: String,
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
game_repository: repositories::game::repository::Repository,
|
|
}
|
|
|
|
impl std::fmt::Debug for Service {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
f.debug_struct("Service of api.kgon.identity").finish()
|
|
}
|
|
}
|
|
|
|
impl Service {
|
|
///
|
|
pub fn new(
|
|
connection_broker: nats::asynk::Connection,
|
|
queue_broker: String,
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
) -> Service {
|
|
Service {
|
|
connection_broker,
|
|
queue_broker,
|
|
pool,
|
|
game_repository: repositories::game::repository::Repository::new(),
|
|
}
|
|
}
|
|
|
|
pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
|
|
futures::try_join!(self.list_games()).map(|_| ())
|
|
}
|
|
|
|
async fn list_games(&self) -> Result<(), Box<dyn std::error::Error>> {
|
|
let s = self
|
|
.connection_broker
|
|
.queue_subscribe(
|
|
bpr::ss::api::game::SUBJECT_LIST_GAMES,
|
|
self.queue_broker.as_str(),
|
|
)
|
|
.await?;
|
|
|
|
while let Some(message) = s.next().await {
|
|
if let Err(e) = async {
|
|
let req =
|
|
bpr::ss::api::game::ListGamesRequest::decode(message.data.as_slice()).map_err(|e| {
|
|
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
|
|
message: format!("invalid request: {}", e),
|
|
})
|
|
})?;
|
|
|
|
let client = match req.client {
|
|
Some(c) => c,
|
|
None => {
|
|
return Err(bcr::error::rpc::Error::InvalidParams(
|
|
bcr::error::rpc::InvalidParams {
|
|
message: "invalid client information".to_string(),
|
|
detail: bcr::error::rpc::InvalidParamsDetail {
|
|
location: "request".to_string(),
|
|
param: "client".to_string(),
|
|
value: "".to_string(),
|
|
error_type: bcr::error::rpc::InvalidParamsType::Required,
|
|
message: "".to_string(),
|
|
},
|
|
},
|
|
));
|
|
}
|
|
};
|
|
let request = match req.request {
|
|
Some(r) => r,
|
|
None => {
|
|
return Err(bcr::error::rpc::Error::InvalidParams(
|
|
bcr::error::rpc::InvalidParams {
|
|
message: "invalid request information".to_string(),
|
|
detail: bcr::error::rpc::InvalidParamsDetail {
|
|
location: "request".to_string(),
|
|
param: "request".to_string(),
|
|
value: "".to_string(),
|
|
error_type: bcr::error::rpc::InvalidParamsType::Required,
|
|
message: "".to_string(),
|
|
},
|
|
},
|
|
));
|
|
}
|
|
};
|
|
|
|
let conn = self.pool.get().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 find_all = repositories::game::models::FindAll {
|
|
search: match request.search {
|
|
Some(s) => Some(repositories::game::models::FindAllSearch {
|
|
vendor_id: s.vendor_id.map(|d| d as i64),
|
|
key_like: s.key_like,
|
|
category_like: s.category_like,
|
|
platform_like: s.platform_like,
|
|
game_type_like: s.game_type_like,
|
|
}),
|
|
None => None,
|
|
},
|
|
pagination: request
|
|
.pagination
|
|
.as_ref()
|
|
.map(bcr::pagination::Pagination::from),
|
|
sorts: Some(
|
|
request
|
|
.sorts
|
|
.iter()
|
|
.map(bcr::pagination::Sort::from)
|
|
.collect(),
|
|
),
|
|
};
|
|
|
|
let count = self
|
|
.game_repository
|
|
.select_all_count(&conn, &find_all)
|
|
.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 list = self
|
|
.game_repository
|
|
.select_all(&conn, &find_all)
|
|
.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,
|
|
})
|
|
})?;
|
|
|
|
message
|
|
.respond(
|
|
bpr::ss::api::game::ListGamesResponse {
|
|
error: None,
|
|
result: Some(bpr::ss::api::game::list_games_response::Result {
|
|
games: list
|
|
.iter()
|
|
.map(bpr::models::api::game::Game::from)
|
|
.collect(),
|
|
}),
|
|
}
|
|
.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::ss::api::game::ListGamesResponse {
|
|
error: Some(bpr::protobuf::rpc::Error::from(e)),
|
|
result: None,
|
|
}
|
|
.encode_to_vec(),
|
|
)
|
|
.await?;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn execute_game(&self) -> Result<(), Box<dyn std::error::Error>> {
|
|
Ok(())
|
|
}
|
|
}
|