postgress
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
use imphnen_entities::{PermissionsItemDto, PermissionsQueryDto};
|
||||
use imphnen_entities::{PermissionsItemDto, PermissionsQueryDto, PermissionsEnum};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use surrealdb::sql::Thing;
|
||||
use utoipa::ToSchema;
|
||||
use uuid::Uuid;
|
||||
use validator::Validate;
|
||||
use strum::IntoEnumIterator;
|
||||
|
||||
use super::RolesSchema;
|
||||
use imphnen_entities::seaorm::auth::roles::Model as RolesModel;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, Validate)]
|
||||
pub struct RolesRequestUpdateDto {
|
||||
@@ -32,35 +36,54 @@ pub struct RolesListItemDto {
|
||||
pub struct RolesDetailItemDto {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub is_deleted: bool,
|
||||
pub description: String,
|
||||
pub is_system_role: bool,
|
||||
pub is_default: bool,
|
||||
pub permissions: Vec<PermissionsItemDto>,
|
||||
pub created_at: Option<String>,
|
||||
pub updated_at: Option<String>,
|
||||
}
|
||||
|
||||
impl RolesDetailItemDto {
|
||||
pub fn from(dto: &RolesDetailQueryDto) -> Self {
|
||||
impl From<&RolesModel> for RolesDetailItemDto {
|
||||
fn from(model: &RolesModel) -> Self {
|
||||
let schema = RolesSchema::from(model);
|
||||
|
||||
let mut permissions_dto = vec![];
|
||||
if let Some(serde_json::Value::Array(perms)) = &model.permissions {
|
||||
for p in perms {
|
||||
if let Some(p_str) = p.as_str() {
|
||||
// Find matching enum
|
||||
for enum_val in PermissionsEnum::iter() {
|
||||
if enum_val.to_string() == p_str {
|
||||
permissions_dto.push(PermissionsItemDto {
|
||||
id: enum_val.id(),
|
||||
name: p_str.to_string(),
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
id: dto.id.id.to_raw(),
|
||||
name: dto.name.clone(),
|
||||
is_deleted: dto.is_deleted,
|
||||
permissions: dto
|
||||
.permissions
|
||||
.as_ref()
|
||||
.unwrap_or(&vec![])
|
||||
.iter()
|
||||
.filter_map(|p| p.as_ref())
|
||||
.map(PermissionsItemDto::from)
|
||||
.collect(),
|
||||
created_at: dto.created_at.clone(),
|
||||
updated_at: dto.updated_at.clone(),
|
||||
id: schema.id.to_string(),
|
||||
name: schema.name.clone(),
|
||||
description: schema.description.clone(),
|
||||
is_system_role: schema.is_system_role,
|
||||
is_default: schema.is_default,
|
||||
permissions: permissions_dto,
|
||||
created_at: Some(schema.created_at.clone()),
|
||||
updated_at: Some(schema.updated_at.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct RolesDetailQueryDto {
|
||||
pub id: Thing,
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub permissions: Option<Vec<Option<PermissionsQueryDto>>>,
|
||||
pub is_deleted: bool,
|
||||
@@ -71,7 +94,7 @@ pub struct RolesDetailQueryDto {
|
||||
impl Default for RolesDetailQueryDto {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: Thing::from(("".to_string(), surrealdb::sql::Id::Number(0))),
|
||||
id: Uuid::new_v4(),
|
||||
name: String::new(),
|
||||
permissions: None,
|
||||
is_deleted: false,
|
||||
@@ -80,3 +103,9 @@ impl Default for RolesDetailQueryDto {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for RolesDetailQueryDto {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.id)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user