magnetar/ext_calckey_model/src/user_model.rs

84 lines
2.4 KiB
Rust
Raw Normal View History

2024-01-07 22:28:53 +00:00
use crate::model_ext::EntityPrefixExt;
use crate::{
model_ext::{joined_prefix, joined_prefix_str, SelectColumnsExt},
AliasSourceExt, CalckeyModel,
};
use ck::{drive_file, follow_request, following, user};
use ext_calckey_model_migration::{Alias, SelectStatement};
2024-01-08 00:14:19 +00:00
use sea_orm::{DbErr, FromQueryResult, 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> {
let fallback = user::Entity.base_prefix_str();
let prefix = if prefix.is_empty() { &fallback } else { prefix };
Ok(UserData {
user: user::Model::from_query_result(res, prefix)?,
avatar: drive_file::Model::from_query_result_optional(
res,
&joined_prefix_str(prefix, AVATAR),
)?,
banner: drive_file::Model::from_query_result_optional(
res,
&joined_prefix_str(prefix, BANNER),
)?,
})
}
}
pub struct UserResolver {
db: CalckeyModel,
}
impl UserResolver {
pub fn new(db: CalckeyModel) -> Self {
Self { db }
}
pub fn resolve(
&self,
q: &mut SelectStatement,
prefix: &str,
UserResolveOptions {
with_avatar_and_banner,
}: &UserResolveOptions,
) {
q.add_aliased_columns::<user::Entity>(prefix);
if *with_avatar_and_banner {
q.add_aliased_columns::<drive_file::Entity>(&joined_prefix_str(prefix, AVATAR))
.add_aliased_columns::<drive_file::Entity>(&joined_prefix_str(prefix, BANNER));
q.join_columns(
JoinType::LeftJoin,
user::Relation::DriveFile2.with_alias(Alias::new(prefix)),
joined_prefix(prefix, AVATAR),
)
.join_columns(
JoinType::LeftJoin,
user::Relation::DriveFile1.with_alias(Alias::new(prefix)),
joined_prefix(prefix, BANNER),
);
}
}
pub async fn get_followers(&self, user_id: &str) -> Vec<UserData> {
todo!()
}
}