refactor: 重构产品相关Mapper
- 空值处理与类型控制:为相关关系型字段的数据处理添加空值处理与类型控制 - 调整目录结构:将文件目录按照实际查询划分为Product和ProductList两个文件
This commit is contained in:
54
server/mappers/productListMapper.ts
Normal file
54
server/mappers/productListMapper.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import { isObject } from '../../server/utils/object';
|
||||
|
||||
/**
|
||||
* 将Directus返回的ProductType数据转换为ProductTypeView视图模型
|
||||
*
|
||||
* @param raw: 原始的ProductType数据
|
||||
* @returns 转换后的ProductTypeView对象
|
||||
*
|
||||
* @example
|
||||
* const view = toProductTypeView(rawProductType);
|
||||
*/
|
||||
export function toProductTypeView(
|
||||
raw: ProductType | string | null
|
||||
): ProductTypeView {
|
||||
if (typeof raw === 'string' || raw === null) {
|
||||
return {
|
||||
id: '-1',
|
||||
name: '',
|
||||
sort: 999,
|
||||
} satisfies ProductTypeView;
|
||||
}
|
||||
const trans = raw.translations?.[0] ?? { name: '' };
|
||||
|
||||
return {
|
||||
id: raw.id.toString(),
|
||||
name: trans.name,
|
||||
sort: raw?.sort ?? 999,
|
||||
} satisfies ProductTypeView;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 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 = toProductTypeView(raw.product_type ?? null);
|
||||
|
||||
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,
|
||||
} satisfies ProductListView;
|
||||
}
|
||||
Reference in New Issue
Block a user