2024-01-08 19:15:51 +00:00
|
|
|
use crate::model_ext::{AliasSourceExt, AliasSuffixExt, EntityPrefixExt, MagIden};
|
|
|
|
use crate::{model_ext::SelectColumnsExt, CalckeyModel};
|
|
|
|
use ck::{drive_file, user};
|
|
|
|
use ext_calckey_model_migration::{IntoIden, SelectStatement};
|
|
|
|
use sea_orm::sea_query::Alias;
|
|
|
|
use sea_orm::{DbErr, FromQueryResult, Iden, JoinType, QueryResult};
|
2024-01-07 22:28:53 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct UserData {
|
|
|
|
pub user: user::Model,
|
|
|
|
pub avatar: Option<drive_file::Model>,
|
|
|
|
pub banner: Option<drive_file::Model>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UserResolveOptions {
|
|
|
|
pub with_avatar_and_banner: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
const AVATAR: &str = "avatar.";
|
|
|
|
const BANNER: &str = "banner.";
|
|
|
|
|
|
|
|
impl FromQueryResult for UserData {
|
|
|
|
fn from_query_result(res: &QueryResult, prefix: &str) -> Result<Self, DbErr> {
|
2024-01-08 19:15:51 +00:00
|
|
|
let prefix = if prefix.is_empty() {
|
|
|
|
user::Entity.base_prefix()
|
|
|
|
} else {
|
|
|
|
MagIden::alias(prefix)
|
|
|
|
};
|
2024-01-07 22:28:53 +00:00
|
|
|
|
|
|
|
Ok(UserData {
|
2024-01-08 19:15:51 +00:00
|
|
|
user: user::Model::from_query_result(res, &prefix.to_string())?,
|
2024-01-07 22:28:53 +00:00
|
|
|
avatar: drive_file::Model::from_query_result_optional(
|
|
|
|
res,
|
2024-01-08 19:15:51 +00:00
|
|
|
&prefix.join_str_as_str(AVATAR),
|
2024-01-07 22:28:53 +00:00
|
|
|
)?,
|
|
|
|
banner: drive_file::Model::from_query_result_optional(
|
|
|
|
res,
|
2024-01-08 19:15:51 +00:00
|
|
|
&prefix.join_str_as_str(BANNER),
|
2024-01-07 22:28:53 +00:00
|
|
|
)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UserResolver {
|
|
|
|
db: CalckeyModel,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserResolver {
|
|
|
|
pub fn new(db: CalckeyModel) -> Self {
|
|
|
|
Self { db }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve(
|
|
|
|
&self,
|
|
|
|
q: &mut SelectStatement,
|
2024-01-08 19:15:51 +00:00
|
|
|
user_tbl: &MagIden,
|
2024-01-07 22:28:53 +00:00
|
|
|
UserResolveOptions {
|
|
|
|
with_avatar_and_banner,
|
|
|
|
}: &UserResolveOptions,
|
|
|
|
) {
|
2024-01-08 19:15:51 +00:00
|
|
|
q.add_aliased_columns::<user::Entity>(user_tbl);
|
2024-01-07 22:28:53 +00:00
|
|
|
|
|
|
|
if *with_avatar_and_banner {
|
2024-01-08 19:15:51 +00:00
|
|
|
let avatar_tbl = user_tbl.join_str(AVATAR);
|
|
|
|
let banner_tbl = user_tbl.join_str(BANNER);
|
|
|
|
|
|
|
|
q.add_aliased_columns::<drive_file::Entity>(&avatar_tbl)
|
|
|
|
.add_aliased_columns::<drive_file::Entity>(&banner_tbl);
|
2024-01-07 22:28:53 +00:00
|
|
|
|
|
|
|
q.join_columns(
|
|
|
|
JoinType::LeftJoin,
|
2024-01-08 19:15:51 +00:00
|
|
|
user::Relation::DriveFile2.with_from_alias(user_tbl),
|
|
|
|
&avatar_tbl,
|
2024-01-07 22:28:53 +00:00
|
|
|
)
|
|
|
|
.join_columns(
|
|
|
|
JoinType::LeftJoin,
|
2024-01-08 19:15:51 +00:00
|
|
|
user::Relation::DriveFile1.with_from_alias(user_tbl),
|
|
|
|
&banner_tbl,
|
2024-01-07 22:28:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_followers(&self, user_id: &str) -> Vec<UserData> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|