postgress
This commit is contained in:
@@ -1,127 +1,50 @@
|
||||
use super::{
|
||||
RolesDetailItemDto, RolesDetailQueryDto, RolesListItemDto, RolesRequestCreateDto,
|
||||
RolesRequestUpdateDto,
|
||||
};
|
||||
use crate::ResourceEnum;
|
||||
use imphnen_utils::{get_iso_date, make_thing_from_enum};
|
||||
use super::RolesListItemDto;
|
||||
use imphnen_entities::seaorm::auth::roles::Model as RolesModel;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashSet;
|
||||
use surrealdb::{Uuid, sql::Thing};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct RolesSchema {
|
||||
pub id: Thing,
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub is_deleted: bool,
|
||||
pub permissions: Vec<Thing>,
|
||||
pub created_at: Option<String>,
|
||||
pub updated_at: Option<String>,
|
||||
pub description: String,
|
||||
pub is_system_role: bool,
|
||||
pub is_default: bool,
|
||||
pub permissions: Vec<String>,
|
||||
pub created_at: String,
|
||||
pub updated_at: String,
|
||||
pub deleted_at: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for RolesSchema {
|
||||
fn default() -> Self {
|
||||
RolesSchema {
|
||||
id: make_thing_from_enum(
|
||||
ResourceEnum::Roles,
|
||||
&Uuid::new_v4().to_string(),
|
||||
),
|
||||
permissions: vec![make_thing_from_enum(
|
||||
ResourceEnum::Permissions,
|
||||
&Uuid::new_v4().to_string(),
|
||||
)],
|
||||
name: String::new(),
|
||||
is_deleted: false,
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
impl From<&RolesModel> for RolesSchema {
|
||||
fn from(model: &RolesModel) -> Self {
|
||||
let permissions = model.permissions
|
||||
.as_ref()
|
||||
.and_then(|p| serde_json::from_value(p.clone()).ok())
|
||||
.unwrap_or_default();
|
||||
|
||||
Self {
|
||||
id: model.id,
|
||||
name: model.name.clone(),
|
||||
description: model.description.clone(),
|
||||
is_system_role: model.is_system_role,
|
||||
is_default: model.is_default,
|
||||
permissions,
|
||||
created_at: model.created_at.to_rfc3339(),
|
||||
updated_at: model.updated_at.to_rfc3339(),
|
||||
deleted_at: model.deleted_at.map(|dt| dt.to_rfc3339()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RolesSchema {
|
||||
pub fn from(dto: RolesDetailQueryDto) -> Self {
|
||||
Self {
|
||||
id: dto.id,
|
||||
name: dto.name,
|
||||
permissions: dto
|
||||
.permissions
|
||||
.as_ref()
|
||||
.unwrap_or(&vec![])
|
||||
.iter()
|
||||
.filter_map(|perm| {
|
||||
perm.as_ref().and_then(|p| p.id.as_ref().map(|id| make_thing_from_enum(ResourceEnum::Permissions, &id.id.to_raw())))
|
||||
})
|
||||
.collect(),
|
||||
is_deleted: dto.is_deleted,
|
||||
created_at: dto.created_at,
|
||||
updated_at: dto.updated_at,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create(dto: RolesRequestCreateDto) -> Self {
|
||||
let permissions: Vec<Thing> = dto
|
||||
.permissions
|
||||
.into_iter()
|
||||
.map(|id| make_thing_from_enum(ResourceEnum::Permissions, &id))
|
||||
.collect();
|
||||
Self {
|
||||
id: make_thing_from_enum(
|
||||
ResourceEnum::Roles,
|
||||
&Uuid::new_v4().to_string(),
|
||||
),
|
||||
name: dto.name,
|
||||
permissions,
|
||||
is_deleted: false,
|
||||
created_at: Some(get_iso_date()),
|
||||
updated_at: Some(get_iso_date()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(
|
||||
dto: RolesRequestUpdateDto,
|
||||
id: String,
|
||||
existing: RolesDetailItemDto,
|
||||
) -> Self {
|
||||
let name = dto.name.unwrap_or(existing.name);
|
||||
let permissions: Vec<Thing> =
|
||||
match (dto.permissions, dto.overwrite.unwrap_or(false)) {
|
||||
(Some(new_ids), true) => new_ids
|
||||
.iter()
|
||||
.map(|id| make_thing_from_enum(ResourceEnum::Permissions, id))
|
||||
.collect(),
|
||||
(Some(new_ids), false) => {
|
||||
let mut all_ids: HashSet<String> =
|
||||
existing.permissions.iter().map(|p| p.id.clone()).collect();
|
||||
for id in new_ids {
|
||||
all_ids.insert(id);
|
||||
}
|
||||
all_ids
|
||||
.into_iter()
|
||||
.map(|id| make_thing_from_enum(ResourceEnum::Permissions, &id))
|
||||
.collect()
|
||||
},
|
||||
(None, _) => existing
|
||||
.permissions
|
||||
.iter()
|
||||
.map(|p| make_thing_from_enum(ResourceEnum::Permissions, &p.id))
|
||||
.collect(),
|
||||
};
|
||||
Self {
|
||||
id: make_thing_from_enum(ResourceEnum::Roles, &id),
|
||||
name,
|
||||
permissions,
|
||||
is_deleted: existing.is_deleted,
|
||||
created_at: existing.created_at,
|
||||
updated_at: Some(get_iso_date()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn list(&self) -> RolesListItemDto {
|
||||
RolesListItemDto {
|
||||
id: self.id.id.to_raw(),
|
||||
id: self.id.to_string(),
|
||||
name: self.name.clone(),
|
||||
permissions_count: self.permissions.len(),
|
||||
created_at: self.created_at.clone(),
|
||||
updated_at: self.updated_at.clone(),
|
||||
created_at: Some(self.created_at.clone()),
|
||||
updated_at: Some(self.updated_at.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user