bug fixed

This commit is contained in:
병준 박 2022-08-12 09:51:58 +00:00
parent c4da7de2b5
commit ba825a641a
19 changed files with 1268 additions and 32 deletions

View File

@ -30,7 +30,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.40-snapshot" }
beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.23-snapshot" }
beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.43-snapshot" }
beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.26-snapshot" }
[build-dependencies]

View File

@ -5,7 +5,6 @@ CREATE TABLE IF NOT EXISTS member_levels (
sort_order INTEGER NOT NULL,
created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
deleted_at BIGINT,
PRIMARY KEY (id),
UNIQUE (name)
);

View File

@ -5,7 +5,6 @@ CREATE TABLE IF NOT EXISTS member_classes (
parent_id UUID,
created_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
updated_at BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
deleted_at BIGINT,
PRIMARY KEY (id),
UNIQUE (name)
);

View File

@ -41,14 +41,12 @@ SELECT
mc.name as mc_name,
mc.created_at as mc_created_at,
mc.updated_at as mc_updated_at,
mc.deleted_at as mc_deleted_at,
ml.id as ml_id,
ml.name as ml_name,
ml.sort_order as ml_sort_order,
ml.created_at as ml_created_at,
ml.updated_at as ml_updated_at,
ml.deleted_at as ml_deleted_at,
_m.id as _m_id,
_m.site_id as _m_site_id,

View File

@ -126,7 +126,6 @@ impl QueryableByName<diesel::pg::Pg> for MemberModel {
name: row.get("mc_name")?,
created_at: row.get("mc_created_at")?,
updated_at: row.get("mc_updated_at")?,
deleted_at: row.get("mc_deleted_at")?,
},
member_level: _MemberLevel {
id: row.get("ml_id")?,
@ -134,7 +133,6 @@ impl QueryableByName<diesel::pg::Pg> for MemberModel {
sort_order: row.get("ml_sort_order")?,
created_at: row.get("ml_created_at")?,
updated_at: row.get("ml_updated_at")?,
deleted_at: row.get("ml_deleted_at")?,
},
username: row.get("m_username")?,
password: row.get("m_password")?,

View File

@ -15,8 +15,26 @@ pub struct MemberClass {
pub created_at: i64,
///
pub updated_at: i64,
}
///
pub deleted_at: Option<i64>,
#[derive(Insertable, Debug, Clone)]
#[table_name = "member_classes"]
pub struct NewMemberClass {
///
pub parent_id: Option<uuid::Uuid>,
///
pub name: String,
}
///
#[derive(AsChangeset, Debug, Clone)]
#[table_name = "member_classes"]
pub struct ModifyMemberClass {
///
pub parent_id: Option<uuid::Uuid>,
///
pub name: String,
}
///

View File

@ -26,6 +26,19 @@ impl Repository {
Repository {}
}
///
pub fn insert(
&self,
conn: &diesel::PgConnection,
new_member_class: &models::NewMemberClass,
) -> Result<models::MemberClass, Error> {
let inserted = diesel::insert_into(member_classes::table)
.values(new_member_class)
.get_result::<models::MemberClass>(conn)?;
Ok(inserted)
}
///
pub fn select(
&self,
@ -41,7 +54,7 @@ impl Repository {
pub fn select_all_count(
&self,
conn: &diesel::PgConnection,
find_all: models::FindAll,
find_all: &models::FindAll,
) -> Result<i64, Error> {
let mut q = member_classes::table.into_boxed();
@ -56,7 +69,7 @@ impl Repository {
pub fn select_all(
&self,
conn: &diesel::PgConnection,
find_all: models::FindAll,
find_all: &models::FindAll,
) -> Result<Vec<models::MemberClass>, Error> {
let mut q = member_classes::table.into_boxed();
@ -85,9 +98,7 @@ impl Repository {
"updated_at" => {
q = q.order_by(member_classes::updated_at.asc());
}
"deleted_at" => {
q = q.order_by(member_classes::deleted_at.asc());
}
_ => {}
},
bcr::models::pagination::Sort::DESC(property) => match property.as_str() {
@ -100,9 +111,7 @@ impl Repository {
"updated_at" => {
q = q.order_by(member_classes::updated_at.desc());
}
"deleted_at" => {
q = q.order_by(member_classes::deleted_at.desc());
}
_ => {}
},
};
@ -111,4 +120,28 @@ impl Repository {
q.load::<models::MemberClass>(conn)
}
///
pub fn update(
&self,
conn: &diesel::PgConnection,
id: uuid::Uuid,
modify: &models::ModifyMemberClass,
) -> Result<u64, Error> {
use member_classes::dsl;
diesel::update(dsl::member_classes.filter(dsl::id.eq(id)))
.set(modify)
.execute(conn)
.map(|c| c as u64)
}
///
pub fn delete(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result<u64, Error> {
use member_classes::dsl;
diesel::delete(member_classes::table.filter(dsl::id.eq(id)))
.execute(conn)
.map(|c| c as u64)
}
}

View File

@ -14,7 +14,5 @@ table! {
created_at -> BigInt,
///
updated_at -> BigInt,
///
deleted_at -> Nullable<BigInt>,
}
}

View File

@ -15,8 +15,26 @@ pub struct MemberLevel {
pub created_at: i64,
///
pub updated_at: i64,
}
///
pub deleted_at: Option<i64>,
#[derive(Insertable, Debug, Clone)]
#[table_name = "member_levels"]
pub struct NewMemberLevel {
///
pub name: String,
///
pub sort_order: i32,
}
///
#[derive(AsChangeset, Debug, Clone)]
#[table_name = "member_levels"]
pub struct ModifyMemberLevel {
///
pub name: String,
///
pub sort_order: i32,
}
///

View File

@ -26,6 +26,19 @@ impl Repository {
Repository {}
}
///
pub fn insert(
&self,
conn: &diesel::PgConnection,
new_member_level: &models::NewMemberLevel,
) -> Result<models::MemberLevel, Error> {
let inserted = diesel::insert_into(member_levels::table)
.values(new_member_level)
.get_result::<models::MemberLevel>(conn)?;
Ok(inserted)
}
///
pub fn select(
&self,
@ -41,7 +54,7 @@ impl Repository {
pub fn select_all_count(
&self,
conn: &diesel::PgConnection,
find_all: models::FindAll,
find_all: &models::FindAll,
) -> Result<i64, Error> {
let mut q = member_levels::table.into_boxed();
@ -56,7 +69,7 @@ impl Repository {
pub fn select_all(
&self,
conn: &diesel::PgConnection,
find_all: models::FindAll,
find_all: &models::FindAll,
) -> Result<Vec<models::MemberLevel>, Error> {
let mut q = member_levels::table.into_boxed();
@ -88,9 +101,6 @@ impl Repository {
"updated_at" => {
q = q.order_by(member_levels::updated_at.asc());
}
"deleted_at" => {
q = q.order_by(member_levels::deleted_at.asc());
}
_ => {}
},
bcr::models::pagination::Sort::DESC(property) => match property.as_str() {
@ -106,9 +116,6 @@ impl Repository {
"updated_at" => {
q = q.order_by(member_levels::updated_at.desc());
}
"deleted_at" => {
q = q.order_by(member_levels::deleted_at.desc());
}
_ => {}
},
};
@ -117,4 +124,28 @@ impl Repository {
q.load::<models::MemberLevel>(conn)
}
///
pub fn update(
&self,
conn: &diesel::PgConnection,
id: uuid::Uuid,
modify: &models::ModifyMemberLevel,
) -> Result<u64, Error> {
use member_levels::dsl;
diesel::update(dsl::member_levels.filter(dsl::id.eq(id)))
.set(modify)
.execute(conn)
.map(|c| c as u64)
}
///
pub fn delete(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result<u64, Error> {
use member_levels::dsl;
diesel::delete(member_levels::table.filter(dsl::id.eq(id)))
.execute(conn)
.map(|c| c as u64)
}
}

View File

@ -14,7 +14,5 @@ table! {
created_at -> BigInt,
///
updated_at -> BigInt,
///
deleted_at -> Nullable<BigInt>,
}
}

View File

@ -36,7 +36,6 @@ impl From<&repositories::member_class::models::MemberClass> for bpr::models::mem
name: d.name.clone(),
created_at: d.created_at as u64,
updated_at: d.updated_at as u64,
deleted_at: d.deleted_at.map(|d| d as u64),
}
}
}
@ -49,7 +48,6 @@ impl From<&repositories::member_level::models::MemberLevel> for bpr::models::mem
sort_order: d.sort_order as u32,
created_at: d.created_at as u64,
updated_at: d.updated_at as u64,
deleted_at: d.deleted_at.map(|d| d as u64),
}
}
}

View File

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

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,591 @@
//!
//!
use std::str::FromStr;
use super::models;
use crate::compositions;
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>>,
site_composition: compositions::site::composition::Composition,
member_class_repository: repositories::member_class::repository::Repository,
}
impl std::fmt::Debug for Service {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Service of service.member.service.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,
site_composition: compositions::site::composition::Composition::new(),
member_class_repository: repositories::member_class::repository::Repository::new(),
}
}
pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
futures::try_join!(
self.list_member_classes(),
self.create_member_class(),
self.update_member_class(),
self.delete_member_class(),
)
.map(|_| ())
}
fn check_site(
&self,
conn: &diesel::PgConnection,
url: Option<String>,
site_id: uuid::Uuid,
) -> Result<repositories::site::models::Site, bcr::error::rpc::Error> {
match self
.site_composition
.select_by_url(conn, url, site_id)
.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,
})
})? {
Some(s) => Ok(s),
None => Err(bcr::error::rpc::Error::InvalidParams(
bcr::error::rpc::InvalidParams {
message: "invalid site_url information".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "client.site_url".to_string(),
value: "".to_string(),
error_type: bcr::error::rpc::InvalidParamsType::None,
message: "".to_string(),
},
},
)),
}
}
async fn create_member_class(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::member::member_class::SUBJECT_CREATE_MEMBER_CLASS,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req =
bpr::ss::member::member_class::CreateMemberClassRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let parent_id = match req.parent_id {
Some(parent_id) => match uuid::Uuid::from_str(parent_id.as_str()) {
Ok(parent_id) => Some(parent_id),
Err(e) => {
return Err(bcr::error::rpc::Error::InvalidParams(
bcr::error::rpc::InvalidParams {
message: "invalid parent_id param".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "parent_id".to_string(),
value: parent_id.clone(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: e.to_string(),
},
},
));
}
},
None => None,
};
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 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_class_repository
.insert(
&conn,
&repositories::member_class::models::NewMemberClass {
parent_id,
name: req.name,
},
)
.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::member_class::CreateMemberClassResponse {
error: None,
result: Some(
bpr::ss::member::member_class::create_member_class_response::Result {
member_class: Some(bpr::models::member::MemberClass::from(&s)),
},
),
}
.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::member_class::CreateMemberClassResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn list_member_classes(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::member::member_class::SUBJECT_LIST_MEMBER_CLASSES,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req =
bpr::ss::member::member_class::ListMemberClassesRequest::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 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::member_class::models::FindAll {
name_like: None,
pagination: req
.pagination
.as_ref()
.map(|d| bcr::models::pagination::Pagination::from(d)),
sorts: Some(
req
.sorts
.iter()
.map(|d| beteran_common_rust::models::pagination::Sort::from(d))
.collect(),
),
};
let count = self
.member_class_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
.member_class_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::member::member_class::ListMemberClassesResponse {
error: None,
result: Some(
bpr::ss::member::member_class::list_member_classes_response::Result {
member_classes: list
.iter()
.map(|d| bpr::models::member::MemberClass::from(d))
.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::member_class::ListMemberClassesResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn update_member_class(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::member::member_class::SUBJECT_UPDATE_MEMBER_CLASS,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req =
bpr::ss::member::member_class::UpdateMemberClassRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| {
bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams {
message: "invalid id param".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "id".to_string(),
value: req.id.clone(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: e.to_string(),
},
})
})?;
let parent_id = match req.parent_id {
Some(parent_id) => match uuid::Uuid::from_str(parent_id.as_str()) {
Ok(parent_id) => Some(parent_id),
Err(e) => {
return Err(bcr::error::rpc::Error::InvalidParams(
bcr::error::rpc::InvalidParams {
message: "invalid parent_id param".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "parent_id".to_string(),
value: parent_id.clone(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: e.to_string(),
},
},
));
}
},
None => None,
};
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 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_class_repository
.update(
&conn,
id,
&repositories::member_class::models::ModifyMemberClass {
parent_id,
name: req.name,
},
)
.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_class_repository
.select(&conn, id)
.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::member_class::UpdateMemberClassResponse {
error: None,
result: Some(
bpr::ss::member::member_class::update_member_class_response::Result {
member_class: Some(bpr::models::member::MemberClass::from(&s)),
},
),
}
.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::member_class::UpdateMemberClassResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn delete_member_class(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::member::member_class::SUBJECT_DELETE_MEMBER_CLASS,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req =
bpr::ss::member::member_class::DeleteMemberClassRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| {
bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams {
message: "invalid id param".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "id".to_string(),
value: req.id.clone(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: e.to_string(),
},
})
})?;
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 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_class_repository
.delete(&conn, id)
.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::member_class::DeleteMemberClassResponse {
error: None,
result: Some(bpr::ss::member::member_class::delete_member_class_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::member_class::DeleteMemberClassResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}

View File

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

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,549 @@
//!
//!
use std::str::FromStr;
use super::models;
use crate::compositions;
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>>,
site_composition: compositions::site::composition::Composition,
member_level_repository: repositories::member_level::repository::Repository,
}
impl std::fmt::Debug for Service {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Service of service.member.service.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,
site_composition: compositions::site::composition::Composition::new(),
member_level_repository: repositories::member_level::repository::Repository::new(),
}
}
pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
futures::try_join!(
self.list_member_levels(),
self.create_member_level(),
self.update_member_level(),
self.delete_member_level(),
)
.map(|_| ())
}
fn check_site(
&self,
conn: &diesel::PgConnection,
url: Option<String>,
site_id: uuid::Uuid,
) -> Result<repositories::site::models::Site, bcr::error::rpc::Error> {
match self
.site_composition
.select_by_url(conn, url, site_id)
.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,
})
})? {
Some(s) => Ok(s),
None => Err(bcr::error::rpc::Error::InvalidParams(
bcr::error::rpc::InvalidParams {
message: "invalid site_url information".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "client.site_url".to_string(),
value: "".to_string(),
error_type: bcr::error::rpc::InvalidParamsType::None,
message: "".to_string(),
},
},
)),
}
}
async fn create_member_level(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::member::member_level::SUBJECT_CREATE_MEMBER_LEVEL,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req =
bpr::ss::member::member_level::CreateMemberLevelRequest::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 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_level_repository
.insert(
&conn,
&repositories::member_level::models::NewMemberLevel {
name: req.name,
sort_order: req.sort_order as i32,
},
)
.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::member_level::CreateMemberLevelResponse {
error: None,
result: Some(
bpr::ss::member::member_level::create_member_level_response::Result {
member_level: Some(bpr::models::member::MemberLevel::from(&s)),
},
),
}
.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::member_level::CreateMemberLevelResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn list_member_levels(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::member::member_level::SUBJECT_LIST_MEMBER_LEVELS,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req =
bpr::ss::member::member_level::ListMemberLevelsRequest::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 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::member_level::models::FindAll {
name_like: None,
pagination: req
.pagination
.as_ref()
.map(|d| bcr::models::pagination::Pagination::from(d)),
sorts: Some(
req
.sorts
.iter()
.map(|d| beteran_common_rust::models::pagination::Sort::from(d))
.collect(),
),
};
let count = self
.member_level_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
.member_level_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::member::member_level::ListMemberLevelsResponse {
error: None,
result: Some(
bpr::ss::member::member_level::list_member_levels_response::Result {
member_levels: list
.iter()
.map(|d| bpr::models::member::MemberLevel::from(d))
.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::member_level::ListMemberLevelsResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn update_member_level(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::member::member_level::SUBJECT_UPDATE_MEMBER_LEVEL,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req =
bpr::ss::member::member_level::UpdateMemberLevelRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| {
bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams {
message: "invalid id param".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "id".to_string(),
value: req.id.clone(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: e.to_string(),
},
})
})?;
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 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_level_repository
.update(
&conn,
id,
&repositories::member_level::models::ModifyMemberLevel {
name: req.name,
sort_order: req.sort_order as i32,
},
)
.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_level_repository
.select(&conn, id)
.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::member_level::UpdateMemberLevelResponse {
error: None,
result: Some(
bpr::ss::member::member_level::update_member_level_response::Result {
member_level: Some(bpr::models::member::MemberLevel::from(&s)),
},
),
}
.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::member_level::UpdateMemberLevelResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn delete_member_level(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::member::member_level::SUBJECT_DELETE_MEMBER_LEVEL,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req =
bpr::ss::member::member_level::DeleteMemberLevelRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| {
bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams {
message: "invalid id param".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "id".to_string(),
value: req.id.clone(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: e.to_string(),
},
})
})?;
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 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_level_repository
.delete(&conn, id)
.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::member_level::DeleteMemberLevelResponse {
error: None,
result: Some(bpr::ss::member::member_level::delete_member_level_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::member_level::DeleteMemberLevelResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}

View File

@ -1,3 +1,5 @@
pub mod identity;
pub mod member;
pub mod member_class;
pub mod member_level;
pub mod site;