草稿数据添加关联查询default_release_data_name
All checks were successful
build / build-rust (push) Successful in 2m30s
All checks were successful
build / build-rust (push) Successful in 2m30s
This commit is contained in:
parent
ef38fe0566
commit
c1a4a3701e
@ -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::Engine;
|
||||
use chrono::NaiveDateTime;
|
||||
@ -10,7 +11,8 @@ use serde_json::Value;
|
||||
use crate::apis::PageQueryDto;
|
||||
|
||||
use super::common::{DataOptions, IscsDataOptions};
|
||||
use super::PageDto;
|
||||
use super::release_data::ReleaseDataId;
|
||||
use super::{PageDto, RtssDbLoader};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DraftDataQuery;
|
||||
@ -256,6 +258,7 @@ impl From<DraftDataFilterDto> for rtss_db::DraftDataQuery {
|
||||
}
|
||||
|
||||
#[derive(Debug, SimpleObject)]
|
||||
#[graphql(complex)]
|
||||
pub struct DraftDataDto {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
@ -270,6 +273,22 @@ pub struct DraftDataDto {
|
||||
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 {
|
||||
fn from(value: rtss_db::model::DraftDataModel) -> Self {
|
||||
Self {
|
||||
|
@ -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)]
|
||||
pub struct ReleaseDataVersionId {
|
||||
pub id: i32,
|
||||
|
@ -45,6 +45,11 @@ pub trait ReleaseDataAccessor {
|
||||
&self,
|
||||
release_id: i32,
|
||||
) -> 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(
|
||||
&self,
|
||||
@ -422,6 +427,24 @@ impl ReleaseDataAccessor for RtssDbAccessor {
|
||||
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(
|
||||
&self,
|
||||
release_id: i32,
|
||||
@ -841,16 +864,25 @@ mod tests {
|
||||
assert_eq!(page_result.total, 8);
|
||||
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
|
||||
let version_ids: Vec<i32> = page_result
|
||||
.data
|
||||
.into_iter()
|
||||
.iter()
|
||||
.map(|d| d.used_version_id.unwrap())
|
||||
.collect();
|
||||
let description_map = accessor
|
||||
.query_release_data_version_descriptions(version_ids.as_slice())
|
||||
.await?;
|
||||
println!("{:?}", description_map);
|
||||
assert_eq!(description_map.len(), page_result.data.len());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user