feat: 相关问题查询添加问题类型查询
- 查询语句更改:添加type字段 - mapper增添:添加type字段的mapper - 视图模型更改:QuestionListView添加type字段
This commit is contained in:
@ -1,5 +1,31 @@
|
||||
import { isObject } from '../../server/utils/object';
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 QuestionType 类型转换为 QuestionTypeView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 QuestionType 数据
|
||||
* @returns 转换后的 QuestionTypeView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toQuestionTypeView(rawQuestionType);
|
||||
*/
|
||||
export function toQuestionTypeView(
|
||||
raw: QuestionType | string | null
|
||||
): QuestionTypeView {
|
||||
if (typeof raw === 'string' || raw === null) {
|
||||
return {
|
||||
id: '',
|
||||
type: '',
|
||||
} satisfies QuestionTypeView;
|
||||
}
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
return {
|
||||
id: raw.id.toString(),
|
||||
type: trans?.name ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 Question 数据转换为 ProductQuestionView 视图模型
|
||||
*
|
||||
@ -31,6 +57,8 @@ export function toProductQuestionView(raw: Question): ProductQuestionView {
|
||||
export function toQuestionListView(raw: Question): QuestionListView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
const type = toQuestionTypeView(raw.type ?? null);
|
||||
|
||||
const related_products: QuestionListProduct[] = (raw.products ?? [])
|
||||
.filter(isObject<ProductsQuestion>)
|
||||
.map((item) => item.products_id)
|
||||
@ -57,6 +85,7 @@ export function toQuestionListView(raw: Question): QuestionListView {
|
||||
|
||||
return {
|
||||
id: raw.id.toString(),
|
||||
type: type,
|
||||
title: trans?.title ?? '',
|
||||
content: trans?.content ?? '',
|
||||
products: related_products,
|
||||
|
||||
Reference in New Issue
Block a user