refactor: 将Data到ViewModel的转换由App转移至Server端
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
- 将逻辑转移到Server端后,简化前端逻辑
This commit is contained in:
154
server/mappers/productMapper.ts
Normal file
154
server/mappers/productMapper.ts
Normal file
@ -0,0 +1,154 @@
|
||||
import { toProductQuestionView } from './questionMapper';
|
||||
import { toProductDocumentView } from './documentMapper';
|
||||
|
||||
/**
|
||||
* 将Directus返回的ProductType数据转换为ProductTypeView视图模型
|
||||
*
|
||||
* @param raw: 原始的ProductType数据
|
||||
* @returns 转换后的ProductTypeView对象
|
||||
*
|
||||
* @example
|
||||
* const view = toProductTypeView(rawProductType);
|
||||
*/
|
||||
export function toProductTypeView(raw: ProductType): ProductTypeView {
|
||||
const trans = raw.translations?.[0] ?? { name: '' };
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
name: trans.name,
|
||||
sort: raw?.sort ?? 999,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 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 type = isObject<ProductType>(raw.product_type)
|
||||
? toProductTypeView(raw.product_type)
|
||||
: ({
|
||||
id: -1,
|
||||
name: '',
|
||||
sort: 999,
|
||||
} satisfies ProductTypeView);
|
||||
|
||||
const cover = isObject<DirectusFile>(raw.cover) ? raw.cover.id : '';
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
product_type: type,
|
||||
name: trans?.name ?? '',
|
||||
summary: trans?.summary ?? '',
|
||||
cover: cover,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 ProductSpecGroup 数据转换为 ProductSpecGroupView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 ProductSpecGroup 数据
|
||||
* @returns 转换后的 ProductSpecGroupView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toProductSpecGroupView(rawSpecGroup);
|
||||
*/
|
||||
export function toProductSpecGroupView(
|
||||
raw: ProductSpecGroup
|
||||
): ProductSpecGroupView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
const specs = raw.specs ?? [];
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
name: trans?.name ?? '',
|
||||
specs: specs
|
||||
.filter(isObject<ProductSpec>)
|
||||
.map((item) => toProductSpecView(item)),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 ProductSpec 数据转换为 ProductSpecView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 ProductSpec 数据
|
||||
* @returns 转换后的 ProductSpecView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toProductSpecView(rawSpecGroup);
|
||||
*/
|
||||
export function toProductSpecView(raw: ProductSpec): ProductSpecView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
key: trans?.key ?? '',
|
||||
value: trans?.value ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 Product 数据转换为 ProductView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Product 数据
|
||||
* @returns 转换后的 ProductView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toProductView(rawProduct);
|
||||
*/
|
||||
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,
|
||||
image: image,
|
||||
caption: item.translations?.[0]?.caption || '',
|
||||
};
|
||||
});
|
||||
|
||||
const type = isObject<ProductType>(raw.product_type)
|
||||
? (raw.product_type.translations?.[0]?.name ?? '')
|
||||
: '';
|
||||
|
||||
const specs = (raw.specs ?? [])
|
||||
.filter(isObject<ProductSpecGroup>)
|
||||
.map((item) => toProductSpecGroupView(item));
|
||||
|
||||
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));
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
product_type: type,
|
||||
name: trans?.name ?? '',
|
||||
summary: trans?.summary ?? '',
|
||||
images: images,
|
||||
description: trans?.description ?? '',
|
||||
specs: specs,
|
||||
faqs: faqs,
|
||||
documents: documents,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user