refactor: 重构产品相关Mapper
- 空值处理与类型控制:为相关关系型字段的数据处理添加空值处理与类型控制 - 调整目录结构:将文件目录按照实际查询划分为Product和ProductList两个文件
This commit is contained in:
@ -1,55 +1,41 @@
|
||||
import { toProductQuestionView } from './questionMapper';
|
||||
import { toProductDocumentView } from './documentMapper';
|
||||
import { isObject } from '../../server/utils/object';
|
||||
|
||||
/**
|
||||
* 将Directus返回的ProductType数据转换为ProductTypeView视图模型
|
||||
* 将 Directus 返回的 ProductImage 数据转换为 ProductImageView 视图模型
|
||||
*
|
||||
* @param raw: 原始的ProductType数据
|
||||
* @returns 转换后的ProductTypeView对象
|
||||
* @param raw: 原始的 ProductsProductImage 数据
|
||||
* @returns 转换后的 ProductImageView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toProductTypeView(rawProductType);
|
||||
* const view = toProductImageView(rawProductImage);
|
||||
*/
|
||||
export function toProductTypeView(raw: ProductType): ProductTypeView {
|
||||
const trans = raw.translations?.[0] ?? { name: '' };
|
||||
export function toProductImageView(
|
||||
raw: (ProductsProductImage | string)[]
|
||||
): ProductImageView[] {
|
||||
return (raw ?? []).map((item) => {
|
||||
if (!isObject<ProductsProductImage>(item))
|
||||
return {
|
||||
id: item,
|
||||
image: '',
|
||||
caption: '',
|
||||
} satisfies ProductImageView;
|
||||
|
||||
return {
|
||||
id: raw.id.toString(),
|
||||
name: trans.name,
|
||||
sort: raw?.sort ?? 999,
|
||||
};
|
||||
}
|
||||
const image = item.product_images_id;
|
||||
if (!isObject<ProductImage>(image))
|
||||
return {
|
||||
id: item.id.toString(),
|
||||
image: '',
|
||||
caption: '',
|
||||
} satisfies ProductImageView;
|
||||
|
||||
/**
|
||||
* 将 Directus返回的 Product 数据转换为 ProductListView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Product 数据
|
||||
* @returns 转换后的 ProductListView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toProductListView(rawProduct);
|
||||
*/
|
||||
export function toProductListView(raw: Product): ProductListView {
|
||||
const trans = raw.translations?.[0];
|
||||
const trans = image.translations?.[0];
|
||||
|
||||
const type = isObject<ProductType>(raw.product_type)
|
||||
? toProductTypeView(raw.product_type)
|
||||
: ({
|
||||
id: '',
|
||||
name: '',
|
||||
sort: 999,
|
||||
} satisfies ProductTypeView);
|
||||
|
||||
const cover = isObject<DirectusFile>(raw.cover) ? raw.cover.id : '';
|
||||
|
||||
return {
|
||||
id: raw.id.toString(),
|
||||
product_type: type,
|
||||
name: trans?.name ?? '',
|
||||
summary: trans?.summary ?? '',
|
||||
cover: cover,
|
||||
};
|
||||
return {
|
||||
id: item.id.toString(),
|
||||
image: isObject<DirectusFile>(image.image) ? image.image.id : '',
|
||||
caption: trans?.caption || '',
|
||||
} satisfies ProductImageView;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -62,19 +48,26 @@ export function toProductListView(raw: Product): ProductListView {
|
||||
* const view = toProductSpecGroupView(rawSpecGroup);
|
||||
*/
|
||||
export function toProductSpecGroupView(
|
||||
raw: ProductSpecGroup
|
||||
): ProductSpecGroupView {
|
||||
const trans = raw.translations?.[0];
|
||||
raw: (ProductSpecGroup | string)[]
|
||||
): ProductSpecGroupView[] {
|
||||
return (raw ?? []).map((item) => {
|
||||
if (!isObject<ProductSpecGroup>(item)) {
|
||||
return {
|
||||
id: item,
|
||||
name: '',
|
||||
specs: [],
|
||||
} satisfies ProductSpecGroupView;
|
||||
}
|
||||
const trans = item.translations?.[0];
|
||||
|
||||
const specs = raw.specs ?? [];
|
||||
const specs = toProductSpecView(item?.specs ?? []);
|
||||
|
||||
return {
|
||||
id: raw.id.toString(),
|
||||
name: trans?.name ?? '',
|
||||
specs: specs
|
||||
.filter(isObject<ProductSpec>)
|
||||
.map((item) => toProductSpecView(item)),
|
||||
};
|
||||
return {
|
||||
id: item.id.toString(),
|
||||
name: trans?.name ?? '',
|
||||
specs: specs,
|
||||
} satisfies ProductSpecGroupView;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,14 +79,120 @@ export function toProductSpecGroupView(
|
||||
* @example
|
||||
* const view = toProductSpecView(rawSpecGroup);
|
||||
*/
|
||||
export function toProductSpecView(raw: ProductSpec): ProductSpecView {
|
||||
const trans = raw.translations?.[0];
|
||||
export function toProductSpecView(
|
||||
raw: (ProductSpec | string)[]
|
||||
): ProductSpecView[] {
|
||||
return (raw ?? []).map((item) => {
|
||||
if (!isObject<ProductSpec>(item)) {
|
||||
return {
|
||||
id: item,
|
||||
key: '',
|
||||
value: '',
|
||||
} satisfies ProductSpecView;
|
||||
}
|
||||
const trans = item.translations?.[0];
|
||||
|
||||
return {
|
||||
id: raw.id.toString(),
|
||||
key: trans?.key ?? '',
|
||||
value: trans?.value ?? '',
|
||||
};
|
||||
return {
|
||||
id: item.id.toString(),
|
||||
key: trans?.key ?? '',
|
||||
value: trans?.value ?? '',
|
||||
} satisfies ProductSpecView;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 ProductQuestion 数据转换为 ProductQuestionView 视图模型
|
||||
*
|
||||
* @param raw: 原始的ProductQuestion 数据
|
||||
* @returns 转换后的 ProductQuestionView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toProductQuestionView(rawQuestion);
|
||||
*/
|
||||
export function toProductQuestionView(
|
||||
raw: (ProductsQuestion | string)[]
|
||||
): ProductQuestionView[] {
|
||||
return (raw ?? []).map((item) => {
|
||||
if (!isObject<ProductsQuestion>(item)) {
|
||||
return {
|
||||
id: item,
|
||||
title: '',
|
||||
content: '',
|
||||
} satisfies ProductQuestionView;
|
||||
}
|
||||
|
||||
const question = item.questions_id;
|
||||
if (!isObject<Question>(question)) {
|
||||
return {
|
||||
id: item.id.toString(),
|
||||
title: '',
|
||||
content: '',
|
||||
} satisfies ProductQuestionView;
|
||||
}
|
||||
|
||||
const trans = question.translations?.[0];
|
||||
|
||||
return {
|
||||
id: item.id.toString(),
|
||||
title: trans?.title ?? '',
|
||||
content: trans?.content ?? '',
|
||||
} satisfies ProductQuestionView;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 ProductDocument 数据转换为 ProductDocumentView 视图模型
|
||||
*
|
||||
* @param raw: 原始的ProductDocument 数据
|
||||
* @returns 转换后的 ProductDocumentView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toProductDocumentView(rawDocument);
|
||||
*/
|
||||
export function toProductDocumentView(
|
||||
raw: (ProductsProductDocument | string)[]
|
||||
): ProductDocumentView[] {
|
||||
return (raw ?? []).map((item) => {
|
||||
if (!isObject<ProductsProductDocument>(item)) {
|
||||
return {
|
||||
id: item,
|
||||
fileId: '',
|
||||
filename: '',
|
||||
size: 0,
|
||||
title: '',
|
||||
url: '',
|
||||
} satisfies ProductDocumentView;
|
||||
}
|
||||
|
||||
const document = item.product_documents_id;
|
||||
if (!isObject<ProductDocument>(document)) {
|
||||
return {
|
||||
id: item.id.toString(),
|
||||
fileId: '',
|
||||
filename: '',
|
||||
size: 0,
|
||||
title: '',
|
||||
url: '',
|
||||
} satisfies ProductDocumentView;
|
||||
}
|
||||
|
||||
const file = isObject<DirectusFile>(document.file)
|
||||
? document.file
|
||||
: undefined;
|
||||
|
||||
const url = file ? `/api/assets/${file.id}` : '';
|
||||
|
||||
const trans = document.translations?.[0];
|
||||
|
||||
return {
|
||||
id: item.id.toString(),
|
||||
fileId: file?.id ?? '',
|
||||
filename: file?.filename_download ?? '',
|
||||
size: file?.filesize ?? 0,
|
||||
title: trans?.title ?? '',
|
||||
url: url,
|
||||
} satisfies ProductDocumentView;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,42 +207,16 @@ export function toProductSpecView(raw: ProductSpec): ProductSpecView {
|
||||
export function toProductView(raw: Product): ProductView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
const images = (raw.images ?? [])
|
||||
.filter(isObject<ProductsProductImage>)
|
||||
.map((item) => item.product_images_id)
|
||||
.filter(isObject<ProductImage>)
|
||||
.map((item) => {
|
||||
const image = isObject<DirectusFile>(item.image) ? item.image.id : '';
|
||||
return {
|
||||
id: item.id.toString(),
|
||||
image: image,
|
||||
caption: item.translations?.[0]?.caption || '',
|
||||
};
|
||||
});
|
||||
const images = toProductImageView(raw.images ?? []);
|
||||
|
||||
const type = isObject<ProductType>(raw.product_type)
|
||||
? (raw.product_type.translations?.[0]?.name ?? '')
|
||||
: '';
|
||||
const specs = toProductSpecGroupView(raw.specs ?? []);
|
||||
|
||||
const specs = (raw.specs ?? [])
|
||||
.filter(isObject<ProductSpecGroup>)
|
||||
.map((item) => toProductSpecGroupView(item));
|
||||
const faqs = toProductQuestionView(raw.faqs ?? []);
|
||||
|
||||
const faqs = (raw.faqs ?? [])
|
||||
.filter(isObject<ProductsQuestion>)
|
||||
.map((item) => item.questions_id)
|
||||
.filter(isObject<Question>)
|
||||
.map((item) => toProductQuestionView(item));
|
||||
|
||||
const documents = (raw.documents ?? [])
|
||||
.filter(isObject<ProductsProductDocument>)
|
||||
.map((item) => item.product_documents_id)
|
||||
.filter(isObject<ProductDocument>)
|
||||
.map((item) => toProductDocumentView(item));
|
||||
const documents = toProductDocumentView(raw.documents ?? []);
|
||||
|
||||
return {
|
||||
id: raw.id.toString(),
|
||||
product_type: type,
|
||||
name: trans?.name ?? '',
|
||||
summary: trans?.summary ?? '',
|
||||
images: images,
|
||||
@ -151,5 +224,5 @@ export function toProductView(raw: Product): ProductView {
|
||||
specs: specs,
|
||||
faqs: faqs,
|
||||
documents: documents,
|
||||
};
|
||||
} satisfies ProductView;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user