59 lines
1.3 KiB
TypeScript
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,
|
|
};
|
|
}
|