member_referrer is modified
This commit is contained in:
parent
6e79dd869b
commit
08041ce356
|
@ -31,7 +31,7 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
|||
tokio-cron-scheduler = { version = "0" }
|
||||
uuid = { version = "0", features = ["serde", "v4", "v5"] }
|
||||
|
||||
beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.67-snapshot" }
|
||||
beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.51-snapshot" }
|
||||
beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.68-snapshot" }
|
||||
beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.52-snapshot" }
|
||||
|
||||
[build-dependencies]
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
use super::models;
|
||||
use crate::repositories;
|
||||
use diesel::{result::Error, sql_query, RunQueryDsl};
|
||||
use std::fmt::Write;
|
||||
|
||||
static MEMBER_QUERY: &str = "
|
||||
SELECT
|
||||
|
@ -67,7 +68,7 @@ impl Composition {
|
|||
"
|
||||
{}
|
||||
WHERE
|
||||
m.id = $1
|
||||
mr.id = $1
|
||||
",
|
||||
MEMBER_QUERY
|
||||
);
|
||||
|
@ -84,14 +85,81 @@ impl Composition {
|
|||
}
|
||||
}
|
||||
|
||||
///
|
||||
pub fn selects(
|
||||
&self,
|
||||
conn: &diesel::PgConnection,
|
||||
ids: Vec<uuid::Uuid>,
|
||||
) -> Result<Vec<models::MemberReferrerModel>, Error> {
|
||||
if ids.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let mut query_where_id_in = String::new();
|
||||
|
||||
for id in ids {
|
||||
if !query_where_id_in.is_empty() {
|
||||
write!(&mut query_where_id_in, " , ")
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
|
||||
write!(&mut query_where_id_in, " '{}' ", id)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
}
|
||||
|
||||
let query = format!(
|
||||
"
|
||||
{}
|
||||
WHERE
|
||||
mr.id IN ({})
|
||||
",
|
||||
MEMBER_QUERY, query_where_id_in
|
||||
);
|
||||
|
||||
println!("query: {}", query);
|
||||
|
||||
match sql_query(query).get_results::<models::MemberReferrerModel>(conn) {
|
||||
Ok(m) => Ok(m),
|
||||
Err(e) => match e {
|
||||
diesel::result::Error::NotFound => Ok(vec![]),
|
||||
_ => Err(e),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
pub fn select_by_username(
|
||||
&self,
|
||||
conn: &diesel::PgConnection,
|
||||
username: &str,
|
||||
) -> Result<Option<models::MemberReferrerModel>, Error> {
|
||||
let query = format!(
|
||||
"
|
||||
{}
|
||||
WHERE
|
||||
_m.username = $1
|
||||
",
|
||||
MEMBER_QUERY
|
||||
);
|
||||
|
||||
match sql_query(query)
|
||||
.bind::<diesel::sql_types::Text, _>(username)
|
||||
.get_result::<models::MemberReferrerModel>(conn)
|
||||
{
|
||||
Ok(m) => Ok(Some(m)),
|
||||
Err(e) => match e {
|
||||
diesel::result::Error::NotFound => Ok(None),
|
||||
_ => Err(e),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
pub fn select_all(
|
||||
&self,
|
||||
conn: &diesel::PgConnection,
|
||||
find_all: &repositories::member_referrer::models::FindAll,
|
||||
) -> Result<Vec<models::MemberReferrerModel>, Error> {
|
||||
use std::fmt::Write;
|
||||
|
||||
let mut query = String::new();
|
||||
write!(&mut query, "{}", MEMBER_QUERY)
|
||||
.map_err(|e| diesel::result::Error::QueryBuilderError(e.to_string().into()))?;
|
||||
|
|
|
@ -31,10 +31,29 @@ impl Repository {
|
|||
&self,
|
||||
conn: &diesel::PgConnection,
|
||||
new_resource: &models::NewMemberReferrer,
|
||||
) -> Result<models::MemberReferrer, Error> {
|
||||
) -> Result<uuid::Uuid, Error> {
|
||||
use member_referrers::dsl;
|
||||
|
||||
let inserted = diesel::insert_into(member_referrers::table)
|
||||
.values(new_resource)
|
||||
.get_result::<models::MemberReferrer>(conn)?;
|
||||
.returning(dsl::id)
|
||||
.get_result(conn)?;
|
||||
|
||||
Ok(inserted)
|
||||
}
|
||||
|
||||
///
|
||||
pub fn inserts(
|
||||
&self,
|
||||
conn: &diesel::PgConnection,
|
||||
new_referrers: &Vec<models::NewMemberReferrer>,
|
||||
) -> Result<Vec<uuid::Uuid>, Error> {
|
||||
use member_referrers::dsl;
|
||||
|
||||
let inserted = diesel::insert_into(member_referrers::table)
|
||||
.values(new_referrers)
|
||||
.returning(dsl::id)
|
||||
.get_results(conn)?;
|
||||
|
||||
Ok(inserted)
|
||||
}
|
||||
|
@ -192,4 +211,13 @@ impl Repository {
|
|||
.execute(conn)
|
||||
.map(|c| c as u64)
|
||||
}
|
||||
|
||||
///
|
||||
pub fn deletes(&self, conn: &diesel::PgConnection, ids: Vec<uuid::Uuid>) -> Result<u64, Error> {
|
||||
use member_referrers::dsl;
|
||||
|
||||
diesel::delete(member_referrers::table.filter(dsl::id.eq_any(ids)))
|
||||
.execute(conn)
|
||||
.map(|c| c as u64)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,10 +50,13 @@ impl Service {
|
|||
pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
|
||||
futures::try_join!(
|
||||
self.create_member_referrer(),
|
||||
self.create_member_referrers(),
|
||||
self.list_member_referrers(),
|
||||
self.get_member_referrer(),
|
||||
self.get_member_referrer_by_code(),
|
||||
self.update_member_referrer(),
|
||||
self.delete_member_referrer(),
|
||||
self.delete_member_referrers(),
|
||||
)
|
||||
.map(|_| ())
|
||||
}
|
||||
|
@ -179,7 +182,7 @@ impl Service {
|
|||
|
||||
let mr = self
|
||||
.member_referrer_composition
|
||||
.select(&conn, s.id)
|
||||
.select(&conn, s)
|
||||
.map_err(|e| {
|
||||
bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
|
||||
code: bpr::protobuf::rpc::Error::SERVER_00,
|
||||
|
@ -230,6 +233,165 @@ impl Service {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_member_referrers(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let s = self
|
||||
.connection_broker
|
||||
.queue_subscribe(
|
||||
bpr::ss::member_referrer::SUBJECT_CREATE_MEMBER_REFERRERS,
|
||||
self.queue_broker.as_str(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
while let Some(message) = s.next().await {
|
||||
if let Err(e) = async {
|
||||
let req =
|
||||
bpr::ss::member_referrer::CreateMemberReferrersRequest::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 mut member_ids: Vec<uuid::Uuid> = vec![];
|
||||
for i in request.member_ids {
|
||||
let u = match uuid::Uuid::from_str(i.as_str()) {
|
||||
Ok(u) => u,
|
||||
Err(e) => {
|
||||
return Err(bcr::error::rpc::Error::InvalidParams(
|
||||
bcr::error::rpc::InvalidParams {
|
||||
message: "invalid member_ids param".to_string(),
|
||||
detail: bcr::error::rpc::InvalidParamsDetail {
|
||||
location: "request".to_string(),
|
||||
param: "member_ids".to_string(),
|
||||
value: i.clone(),
|
||||
error_type: bcr::error::rpc::InvalidParamsType::Required,
|
||||
message: e.to_string(),
|
||||
},
|
||||
},
|
||||
))
|
||||
}
|
||||
};
|
||||
member_ids.push(u);
|
||||
}
|
||||
|
||||
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 new_member_referrers: Vec<repositories::member_referrer::models::NewMemberReferrer> =
|
||||
member_ids
|
||||
.iter()
|
||||
.map(
|
||||
|member_id| repositories::member_referrer::models::NewMemberReferrer {
|
||||
member_id: member_id.clone(),
|
||||
},
|
||||
)
|
||||
.collect();
|
||||
|
||||
let s = self
|
||||
.member_referrer_repository
|
||||
.inserts(&conn, &new_member_referrers)
|
||||
.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 mr = self
|
||||
.member_referrer_composition
|
||||
.selects(&conn, s)
|
||||
.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::member_referrer::CreateMemberReferrersResponse {
|
||||
error: None,
|
||||
result: Some(
|
||||
bpr::ss::member_referrer::create_member_referrers_response::Result {
|
||||
member_referrers: mr
|
||||
.iter()
|
||||
.map(bpr::models::member_referrer::MemberReferrerModel::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::member_referrer::CreateMemberReferrersResponse {
|
||||
error: Some(bpr::protobuf::rpc::Error::from(e)),
|
||||
result: None,
|
||||
}
|
||||
.encode_to_vec(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn list_member_referrers(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let s = self
|
||||
.connection_broker
|
||||
|
@ -528,6 +690,119 @@ impl Service {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_member_referrer_by_code(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let s = self
|
||||
.connection_broker
|
||||
.queue_subscribe(
|
||||
bpr::ss::member_referrer::SUBJECT_GET_MEMBER_REFERRER_BY_CODE,
|
||||
self.queue_broker.as_str(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
while let Some(message) = s.next().await {
|
||||
if let Err(e) = async {
|
||||
let req =
|
||||
bpr::ss::member_referrer::GetMemberReferrerByCodeRequest::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 s = self
|
||||
.member_referrer_composition
|
||||
.select_by_username(&conn, &request.code)
|
||||
.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::member_referrer::GetMemberReferrerByCodeResponse {
|
||||
error: None,
|
||||
result: Some(
|
||||
bpr::ss::member_referrer::get_member_referrer_by_code_response::Result {
|
||||
member_referrer: s
|
||||
.map(|d| bpr::models::member_referrer::MemberReferrerModel::from(&d)),
|
||||
},
|
||||
),
|
||||
}
|
||||
.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::member_referrer::GetMemberReferrerByCodeResponse {
|
||||
error: Some(bpr::protobuf::rpc::Error::from(e)),
|
||||
result: None,
|
||||
}
|
||||
.encode_to_vec(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_member_referrer(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let s = self
|
||||
.connection_broker
|
||||
|
@ -788,4 +1063,134 @@ impl Service {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_member_referrers(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let s = self
|
||||
.connection_broker
|
||||
.queue_subscribe(
|
||||
bpr::ss::member_referrer::SUBJECT_DELETE_MEMBER_REFERRERS,
|
||||
self.queue_broker.as_str(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
while let Some(message) = s.next().await {
|
||||
if let Err(e) = async {
|
||||
let req =
|
||||
bpr::ss::member_referrer::DeleteMemberReferrersRequest::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 mut ids: Vec<uuid::Uuid> = vec![];
|
||||
for i in request.ids {
|
||||
let u = match uuid::Uuid::from_str(i.as_str()) {
|
||||
Ok(u) => u,
|
||||
Err(e) => {
|
||||
return Err(bcr::error::rpc::Error::InvalidParams(
|
||||
bcr::error::rpc::InvalidParams {
|
||||
message: "invalid ids param".to_string(),
|
||||
detail: bcr::error::rpc::InvalidParamsDetail {
|
||||
location: "request".to_string(),
|
||||
param: "ids".to_string(),
|
||||
value: i.clone(),
|
||||
error_type: bcr::error::rpc::InvalidParamsType::Required,
|
||||
message: e.to_string(),
|
||||
},
|
||||
},
|
||||
))
|
||||
}
|
||||
};
|
||||
ids.push(u);
|
||||
}
|
||||
|
||||
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 _affected = self
|
||||
.member_referrer_repository
|
||||
.deletes(&conn, ids)
|
||||
.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::member_referrer::DeleteMemberReferrersResponse {
|
||||
error: None,
|
||||
result: Some(bpr::ss::member_referrer::delete_member_referrers_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::ss::member_referrer::DeleteMemberReferrersResponse {
|
||||
error: Some(bpr::protobuf::rpc::Error::from(e)),
|
||||
result: None,
|
||||
}
|
||||
.encode_to_vec(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user