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:
76
server/mappers/solutionMapper.ts
Normal file
76
server/mappers/solutionMapper.ts
Normal file
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* 将 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 ?? '',
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user