草稿数据添加关联查询default_release_data_name
All checks were successful
build / build-rust (push) Successful in 2m30s

This commit is contained in:
soul-walker 2024-09-20 17:39:16 +08:00
parent ef38fe0566
commit c1a4a3701e
3 changed files with 86 additions and 3 deletions

View File

@ -1,4 +1,5 @@
use async_graphql::{Context, InputObject, Object, SimpleObject}; use async_graphql::dataloader::DataLoader;
use async_graphql::{ComplexObject, Context, InputObject, Object, SimpleObject};
use base64::prelude::*; use base64::prelude::*;
use base64::Engine; use base64::Engine;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
@ -10,7 +11,8 @@ use serde_json::Value;
use crate::apis::PageQueryDto; use crate::apis::PageQueryDto;
use super::common::{DataOptions, IscsDataOptions}; use super::common::{DataOptions, IscsDataOptions};
use super::PageDto; use super::release_data::ReleaseDataId;
use super::{PageDto, RtssDbLoader};
#[derive(Default)] #[derive(Default)]
pub struct DraftDataQuery; pub struct DraftDataQuery;
@ -256,6 +258,7 @@ impl From<DraftDataFilterDto> for rtss_db::DraftDataQuery {
} }
#[derive(Debug, SimpleObject)] #[derive(Debug, SimpleObject)]
#[graphql(complex)]
pub struct DraftDataDto { pub struct DraftDataDto {
pub id: i32, pub id: i32,
pub name: String, pub name: String,
@ -270,6 +273,22 @@ pub struct DraftDataDto {
pub updated_at: NaiveDateTime, pub updated_at: NaiveDateTime,
} }
#[ComplexObject]
impl DraftDataDto {
async fn default_release_data_name(
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<Option<String>> {
if let Some(version_id) = self.default_release_data_id {
let loader = ctx.data_unchecked::<DataLoader<RtssDbLoader>>();
let name = loader.load_one(ReleaseDataId::new(version_id)).await?;
Ok(name)
} else {
Ok(None)
}
}
}
impl From<rtss_db::model::DraftDataModel> for DraftDataDto { impl From<rtss_db::model::DraftDataModel> for DraftDataDto {
fn from(value: rtss_db::model::DraftDataModel) -> Self { fn from(value: rtss_db::model::DraftDataModel) -> Self {
Self { Self {

View File

@ -245,6 +245,38 @@ impl ReleaseDataDto {
} }
} }
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct ReleaseDataId {
pub id: i32,
}
impl ReleaseDataId {
pub fn new(id: i32) -> Self {
Self { id }
}
}
impl Loader<ReleaseDataId> for RtssDbLoader {
type Value = String;
type Error = Arc<DbAccessError>;
async fn load(
&self,
keys: &[ReleaseDataId],
) -> Result<HashMap<ReleaseDataId, Self::Value>, Self::Error> {
let ids: Vec<i32> = keys.iter().map(|k| k.id).collect();
let rows = self
.db_accessor
.query_release_data_names(ids.as_slice())
.await?;
let map: HashMap<ReleaseDataId, String> = rows
.into_iter()
.map(|r| (ReleaseDataId { id: r.0 }, r.1))
.collect();
Ok(map)
}
}
#[derive(Clone, Copy, Hash, PartialEq, Eq)] #[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct ReleaseDataVersionId { pub struct ReleaseDataVersionId {
pub id: i32, pub id: i32,

View File

@ -45,6 +45,11 @@ pub trait ReleaseDataAccessor {
&self, &self,
release_id: i32, release_id: i32,
) -> Result<ReleaseDataModel, DbAccessError>; ) -> Result<ReleaseDataModel, DbAccessError>;
/// 根据id列表查询发布数据name
async fn query_release_data_names(
&self,
release_ids: &[i32],
) -> Result<Vec<(i32, String)>, DbAccessError>;
/// 查询发布数据所有版本信息 /// 查询发布数据所有版本信息
async fn query_release_data_version_list( async fn query_release_data_version_list(
&self, &self,
@ -422,6 +427,24 @@ impl ReleaseDataAccessor for RtssDbAccessor {
Ok(rd) Ok(rd)
} }
async fn query_release_data_names(
&self,
release_ids: &[i32],
) -> Result<Vec<(i32, String)>, DbAccessError> {
// 查询发布数据
let rd_table = ReleaseDataColumn::Table.name();
let rd_id = ReleaseDataColumn::Id.name();
let rd_name = ReleaseDataColumn::Name.name();
let select_columns = format!("{rd_id}, {rd_name}");
let rd_query_clause =
format!("SELECT {select_columns} FROM {rd_table} WHERE {rd_id} = ANY($1)",);
let rd = sqlx::query_as::<_, (i32, String)>(&rd_query_clause)
.bind(release_ids)
.fetch_all(&self.pool)
.await?;
Ok(rd)
}
async fn query_release_data_version_list( async fn query_release_data_version_list(
&self, &self,
release_id: i32, release_id: i32,
@ -841,16 +864,25 @@ mod tests {
assert_eq!(page_result.total, 8); assert_eq!(page_result.total, 8);
println!("分页查询发布数据测试成功"); println!("分页查询发布数据测试成功");
// 测试根据id列表查询发布数据name
let release_ids: Vec<i32> = page_result.data.iter().map(|d| d.id).collect();
let names = accessor
.query_release_data_names(release_ids.as_slice())
.await?;
println!("{:?}", names);
assert_eq!(names.len(), page_result.data.len());
// 测试根据数据版本id查询descriptions // 测试根据数据版本id查询descriptions
let version_ids: Vec<i32> = page_result let version_ids: Vec<i32> = page_result
.data .data
.into_iter() .iter()
.map(|d| d.used_version_id.unwrap()) .map(|d| d.used_version_id.unwrap())
.collect(); .collect();
let description_map = accessor let description_map = accessor
.query_release_data_version_descriptions(version_ids.as_slice()) .query_release_data_version_descriptions(version_ids.as_slice())
.await?; .await?;
println!("{:?}", description_map); println!("{:?}", description_map);
assert_eq!(description_map.len(), page_result.data.len());
Ok(()) Ok(())
} }