Files
jinshen-website/server/mappers/solutionMapper.ts
R2m1liA d7bd034d7d
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m5s
test: 优化测试代码
- 显式导入相关依赖以避免相关问题
2025-11-14 12:23:49 +08:00

79 lines
1.8 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): SolutionTypeView {
const trans = raw?.translations?.[0];
return {
id: raw.id,
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 = isObject<SolutionType>(raw.type)
? toSolutionTypeView(raw.type)
: ({
id: -1,
name: 'uncategory',
sort: 999,
} satisfies SolutionTypeView);
const cover = isObject<DirectusFile>(raw.cover) ? (raw?.cover.id ?? '') : '';
return {
id: raw.id,
title: trans?.title ?? '',
summary: trans?.summary ?? '',
solution_type: type,
cover: cover,
};
}
/**
* 将 Directus 返回的 Solution 数据转换为 SolutionView 视图模型
*
* @param raw: 原始的 Solution 数据
* @returns 转换后的 SolutionView 对象
*
* ---
*
* @example
* const view = toSolutionView(rawSolution);
*/
export function toSolutionView(raw: Solution): SolutionView {
const trans = raw.translations?.[0];
return {
id: raw.id,
title: trans?.title ?? '',
summary: trans?.summary ?? '',
content: trans?.content ?? '',
createAt: raw?.create_at ?? '',
};
}