feat: pagination
This commit is contained in:
@@ -12,6 +12,9 @@ use super::UsersUpdateRequestDto;
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
security(
|
||||
("Bearer" = [])
|
||||
),
|
||||
path = "/v1/users",
|
||||
params(
|
||||
("page" = Option<i64>, Query, description = "Page number"),
|
||||
@@ -36,6 +39,9 @@ pub async fn get_user_list(
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
security(
|
||||
("Bearer" = [])
|
||||
),
|
||||
path = "/v1/users/detail/{id}",
|
||||
params(
|
||||
("id" = String, Path, description = "User ID")
|
||||
@@ -54,6 +60,9 @@ pub async fn get_user_by_id(
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
security(
|
||||
("Bearer" = [])
|
||||
),
|
||||
path = "/v1/users/create",
|
||||
request_body = UsersCreateRequestDto,
|
||||
responses(
|
||||
@@ -70,6 +79,9 @@ pub async fn post_create_user(
|
||||
|
||||
#[utoipa::path(
|
||||
put,
|
||||
security(
|
||||
("Bearer" = [])
|
||||
),
|
||||
path = "/v1/users/update/{id}",
|
||||
request_body = UsersCreateRequestDto,
|
||||
responses(
|
||||
@@ -87,6 +99,9 @@ pub async fn put_update_user(
|
||||
|
||||
#[utoipa::path(
|
||||
put,
|
||||
security(
|
||||
("Bearer" = [])
|
||||
),
|
||||
path = "/v1/users/activate/{id}",
|
||||
request_body = UsersActiveInactiveRequestDto,
|
||||
responses(
|
||||
@@ -104,6 +119,9 @@ pub async fn patch_user_active_status(
|
||||
|
||||
#[utoipa::path(
|
||||
delete,
|
||||
security(
|
||||
("Bearer" = [])
|
||||
),
|
||||
path = "/v1/users/delete/{id}",
|
||||
responses(
|
||||
(status = 200, description = "Soft delete user", body = MessageResponseDto)
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use surrealdb::sql::Thing;
|
||||
use utoipa::ToSchema;
|
||||
use validator::Validate;
|
||||
|
||||
use crate::RolesItemDto;
|
||||
|
||||
lazy_static! {
|
||||
static ref PASSWORD_REGEX: Regex = Regex::new(r"^[A-Za-z\d@$!%*?&]{8,}$").unwrap();
|
||||
}
|
||||
@@ -82,6 +85,7 @@ pub struct UsersUpdateRequestDto {
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct UsersItemDto {
|
||||
pub id: String,
|
||||
pub role: RolesItemDto,
|
||||
pub fullname: String,
|
||||
pub email: String,
|
||||
pub avatar: Option<String>,
|
||||
@@ -97,6 +101,54 @@ pub struct UsersItemDto {
|
||||
pub birthdate: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct UsersItemDtoRaw {
|
||||
pub id: Thing,
|
||||
pub role: Thing,
|
||||
pub fullname: String,
|
||||
pub email: String,
|
||||
pub avatar: Option<String>,
|
||||
pub phone_number: String,
|
||||
pub referred_by: Option<String>,
|
||||
pub referral_code: Option<String>,
|
||||
pub student_type: String,
|
||||
pub is_active: bool,
|
||||
pub is_profile_completed: bool,
|
||||
pub identity_number: Option<String>,
|
||||
pub religion: Option<String>,
|
||||
pub gender: Option<String>,
|
||||
pub birthdate: Option<String>,
|
||||
}
|
||||
|
||||
impl From<UsersItemDtoRaw> for UsersItemDto {
|
||||
fn from(raw: UsersItemDtoRaw) -> Self {
|
||||
Self {
|
||||
id: raw.id.id.to_string(),
|
||||
role: RolesItemDto {
|
||||
id: raw.role.id.to_string(),
|
||||
name: "".into(),
|
||||
is_deleted: false,
|
||||
permissions: vec![],
|
||||
created_at: None,
|
||||
updated_at: None,
|
||||
},
|
||||
fullname: raw.fullname,
|
||||
email: raw.email,
|
||||
avatar: raw.avatar,
|
||||
phone_number: raw.phone_number,
|
||||
referred_by: raw.referred_by,
|
||||
referral_code: raw.referral_code,
|
||||
student_type: raw.student_type,
|
||||
is_active: raw.is_active,
|
||||
is_profile_completed: raw.is_profile_completed,
|
||||
identity_number: raw.identity_number,
|
||||
religion: raw.religion,
|
||||
gender: raw.gender,
|
||||
birthdate: raw.birthdate,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct UsersActiveInactiveRequestDto {
|
||||
pub is_active: bool,
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use super::{UsersActiveInactiveSchema, UsersSchema, UsersSetNewPasswordSchema};
|
||||
use super::{
|
||||
UsersActiveInactiveSchema, UsersItemDto, UsersItemDtoRaw, UsersSchema,
|
||||
UsersSetNewPasswordSchema,
|
||||
};
|
||||
use crate::{
|
||||
get_id, make_thing, query_list_with_meta, AppState, MetaRequestDto, ResourceEnum,
|
||||
ResponseListSuccessDto,
|
||||
@@ -17,20 +20,80 @@ impl<'a> UsersRepository<'a> {
|
||||
pub async fn query_user_list(
|
||||
&self,
|
||||
meta: MetaRequestDto,
|
||||
) -> Result<ResponseListSuccessDto<Vec<UsersSchema>>> {
|
||||
) -> Result<ResponseListSuccessDto<Vec<UsersItemDto>>> {
|
||||
let db = &self.state.surrealdb_ws;
|
||||
let table = ResourceEnum::Users.to_string();
|
||||
|
||||
let mut conditions = vec!["is_deleted = false".to_string()];
|
||||
if let Some(search) = &meta.search {
|
||||
|
||||
if let Some(search) = meta.search.as_deref() {
|
||||
if !search.is_empty() {
|
||||
conditions.push("string::contains(fullname ?? '', $search)".to_string());
|
||||
}
|
||||
}
|
||||
if meta.filter_by.is_some() && meta.filter.is_some() {
|
||||
let filter_by = meta.filter_by.as_ref().unwrap();
|
||||
|
||||
if let (Some(filter_by), Some(_filter)) =
|
||||
(meta.filter_by.as_ref(), meta.filter.as_ref())
|
||||
{
|
||||
conditions.push(format!("{} = $filter", filter_by));
|
||||
}
|
||||
query_list_with_meta::<UsersSchema>(db, &table, &meta, conditions).await
|
||||
|
||||
let where_clause = if !conditions.is_empty() {
|
||||
format!("WHERE {}", conditions.join(" AND "))
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
let limit = meta.per_page.unwrap_or(10);
|
||||
let start = (meta.page.unwrap_or(1) - 1) * limit;
|
||||
|
||||
let select_query = format!(
|
||||
"
|
||||
SELECT
|
||||
id,
|
||||
role,
|
||||
fullname,
|
||||
email,
|
||||
avatar,
|
||||
phone_number,
|
||||
referred_by,
|
||||
referral_code,
|
||||
student_type,
|
||||
is_active,
|
||||
is_profile_completed,
|
||||
identity_number,
|
||||
religion,
|
||||
gender,
|
||||
birthdate
|
||||
FROM {}
|
||||
{}
|
||||
LIMIT {} START {}
|
||||
FETCH role, role.permissions
|
||||
",
|
||||
ResourceEnum::Users.to_string(),
|
||||
where_clause,
|
||||
limit,
|
||||
start
|
||||
);
|
||||
|
||||
let raw_result = query_list_with_meta::<UsersItemDtoRaw>(
|
||||
db,
|
||||
&ResourceEnum::Users.to_string(),
|
||||
&meta,
|
||||
vec![],
|
||||
Some(select_query),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let converted = raw_result
|
||||
.data
|
||||
.into_iter()
|
||||
.map(UsersItemDto::from)
|
||||
.collect();
|
||||
|
||||
Ok(ResponseListSuccessDto {
|
||||
data: converted,
|
||||
meta: raw_result.meta,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn query_user_by_email(&self, email: String) -> Result<UsersSchema> {
|
||||
|
||||
Reference in New Issue
Block a user