124 lines
3.1 KiB
TypeScript
124 lines
3.1 KiB
TypeScript
|
|
/**
|
||
|
|
* 帖子数据映射器
|
||
|
|
* 负责 Post 模型与 API 响应之间的转换
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { PostModel, UserModel } from '../models';
|
||
|
|
import type { Post } from '../../types';
|
||
|
|
|
||
|
|
export class PostMapper {
|
||
|
|
/**
|
||
|
|
* 将 API 响应转换为应用模型
|
||
|
|
*/
|
||
|
|
static fromApiResponse(response: Post): PostModel {
|
||
|
|
return {
|
||
|
|
id: String(response.id || ''),
|
||
|
|
authorId: String(response.author_id || ''),
|
||
|
|
author: response.author ? this.mapAuthorFromApi(response.author) : undefined,
|
||
|
|
title: response.title || '',
|
||
|
|
content: response.content || '',
|
||
|
|
images: response.images || [],
|
||
|
|
likeCount: response.like_count || 0,
|
||
|
|
commentCount: response.comment_count || 0,
|
||
|
|
shareCount: response.share_count || 0,
|
||
|
|
viewCount: response.view_count || 0,
|
||
|
|
favoriteCount: response.favorite_count || 0,
|
||
|
|
isLiked: response.is_liked || false,
|
||
|
|
isFavorited: response.is_favorited || false,
|
||
|
|
isTop: response.is_top || false,
|
||
|
|
status: response.status || 'published',
|
||
|
|
communityId: response.community_id,
|
||
|
|
tags: response.tags || [],
|
||
|
|
createdAt: new Date(response.created_at || Date.now()),
|
||
|
|
updatedAt: new Date(response.updated_at || Date.now()),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 将 API 响应数组转换为应用模型数组
|
||
|
|
*/
|
||
|
|
static fromApiResponseList(responses: Post[]): PostModel[] {
|
||
|
|
return responses.map(r => this.fromApiResponse(r));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 将应用模型转换为 API 请求数据
|
||
|
|
*/
|
||
|
|
static toApiRequest(model: Partial<PostModel>): Record<string, any> {
|
||
|
|
const request: Record<string, any> = {};
|
||
|
|
|
||
|
|
if (model.title !== undefined) {
|
||
|
|
request.title = model.title;
|
||
|
|
}
|
||
|
|
if (model.content !== undefined) {
|
||
|
|
request.content = model.content;
|
||
|
|
}
|
||
|
|
if (model.images !== undefined) {
|
||
|
|
request.images = model.images;
|
||
|
|
}
|
||
|
|
if (model.communityId !== undefined) {
|
||
|
|
request.community_id = model.communityId;
|
||
|
|
}
|
||
|
|
if (model.tags !== undefined) {
|
||
|
|
request.tags = model.tags;
|
||
|
|
}
|
||
|
|
|
||
|
|
return request;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 创建帖子请求
|
||
|
|
*/
|
||
|
|
static createPostRequest(
|
||
|
|
title: string,
|
||
|
|
content: string,
|
||
|
|
images?: string[],
|
||
|
|
communityId?: string
|
||
|
|
): Record<string, any> {
|
||
|
|
const request: Record<string, any> = {
|
||
|
|
title,
|
||
|
|
content,
|
||
|
|
};
|
||
|
|
if (images && images.length > 0) {
|
||
|
|
request.images = images;
|
||
|
|
}
|
||
|
|
if (communityId) {
|
||
|
|
request.community_id = communityId;
|
||
|
|
}
|
||
|
|
return request;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 更新帖子请求
|
||
|
|
*/
|
||
|
|
static updatePostRequest(
|
||
|
|
title?: string,
|
||
|
|
content?: string,
|
||
|
|
images?: string[]
|
||
|
|
): Record<string, any> {
|
||
|
|
const request: Record<string, any> = {};
|
||
|
|
if (title !== undefined) {
|
||
|
|
request.title = title;
|
||
|
|
}
|
||
|
|
if (content !== undefined) {
|
||
|
|
request.content = content;
|
||
|
|
}
|
||
|
|
if (images !== undefined) {
|
||
|
|
request.images = images;
|
||
|
|
}
|
||
|
|
return request;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 映射作者信息
|
||
|
|
*/
|
||
|
|
private static mapAuthorFromApi(author: any): UserModel {
|
||
|
|
return {
|
||
|
|
id: String(author.id || ''),
|
||
|
|
username: author.username || '',
|
||
|
|
nickname: author.nickname,
|
||
|
|
avatar: author.avatar,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|