Files
jinshen-website/server/mappers/solutionListMapper.ts
R2m1liA 63c3e2e364 refactor: 重构解决方案相关Mapper
- 空值处理与类型控制:为关系型字段添加空值处理与类型控制逻辑
- 目录结构调整:分离SolutionList与Solution相关的Mapper为不同的文件
2025-12-04 17:40:04 +08:00

59 lines
1.3 KiB
TypeScript

import { isObject } from '../../server/utils/object';
/**
* 将 Directus 返回的 SolutionType 数据转换为 SolutionTypeView 视图模型
*
* @param raw: 原始的 SolutionType 数据
* @returns 转换后的 SolutionTypeView 对象
*
* ---
*
* @example
* const view = toSolutionTypeView(rawSolutionType);
*/
export function toSolutionTypeView(
raw: SolutionType | string | null
): SolutionTypeView {
if (typeof raw === 'string' || raw === null) {
return {
id: '-1',
name: 'uncategory',
sort: 999,
} satisfies SolutionTypeView;
}
const trans = raw?.translations?.[0];
return {
id: raw.id.toString(),
name: trans?.name ?? '',
sort: raw?.sort ?? 999,
};
}
/**
* 将 Directus 返回的 Solution 数据转换为 SolutionListView 视图模型
*
* @param raw: 原始的 Solution 数据
* @returns 转换后的 SolutionListView 对象
*
* ---
*
* @example
* const view = toSolutionListView(rawSolution);
*/
export function toSolutionListView(raw: Solution): SolutionListView {
const trans = raw.translations?.[0];
const type = toSolutionTypeView(raw.type ?? null);
const cover = isObject<DirectusFile>(raw.cover) ? (raw?.cover.id ?? '') : '';
return {
id: raw.id.toString(),
title: trans?.title ?? '',
summary: trans?.summary ?? '',
solution_type: type,
cover: cover,
};
}