diff --git a/backend/src/modules/comment/comment.dto.ts b/backend/src/modules/comment/comment.dto.ts index 222a220..390be45 100644 --- a/backend/src/modules/comment/comment.dto.ts +++ b/backend/src/modules/comment/comment.dto.ts @@ -6,7 +6,9 @@ import { IsInt, Min, Max, + MaxLength, } from 'class-validator'; +import { Type } from 'class-transformer'; interface CommentEntity { id: string; @@ -29,7 +31,7 @@ interface CommentEntity { export class CreateCommentDto { @IsString() - @Max(5000) + @MaxLength(5000) content: string; @IsUUID() @@ -47,7 +49,7 @@ export class CreateCommentDto { export class UpdateCommentDto { @IsString() - @Max(5000) + @MaxLength(5000) @IsOptional() content?: string; @@ -118,12 +120,14 @@ export class FindCommentsDto { @IsInt() @Min(1) @IsOptional() + @Type(() => Number) page?: number = 1; @IsInt() @Min(1) @Max(100) @IsOptional() + @Type(() => Number) limit?: number = 20; } diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index e887337..99bc8e3 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -625,7 +625,14 @@ export interface CreateReactionDto { export async function fetchComments(params: FindCommentsParams = {}): Promise { const searchParams = new URLSearchParams(); - Object.entries(params).forEach(([key, value]) => { + // Map parentCommentId to parentId for backend compatibility + const backendParams = { ...params }; + if (backendParams.parentCommentId) { + backendParams.parentId = backendParams.parentCommentId; + delete backendParams.parentCommentId; + } + + Object.entries(backendParams).forEach(([key, value]) => { if (value !== undefined && value !== null) { if (typeof value === 'number') { searchParams.append(key, value.toString()); @@ -639,18 +646,55 @@ export async function fetchComments(params: FindCommentsParams = {}): Promise ({ + ...comment, + parentCommentId: comment.parentId, + // Ensure reactions object exists + reactions: { + likes: comment.likeCount || 0, + dislikes: comment.dislikeCount || 0, + }, + })); + + return { + data: mappedData, + total: mappedData.length, + }; } export async function createComment(dto: CreateCommentDto): Promise { + // Map parentCommentId to parentId for backend compatibility + const backendDto = { + content: dto.content, + articleId: dto.articleId, + liveBlogId: dto.liveBlogId, + parentId: dto.parentCommentId, + }; + const response = await authFetch(`${API_BASE_URL}/comments`, { method: 'POST', - body: JSON.stringify(dto), + body: JSON.stringify(backendDto), }); if (!response.ok) { throw new Error('Failed to create comment'); } - return response.json(); + + const comment = await response.json(); + + // Map backend response to frontend interface + return { + ...comment, + parentCommentId: comment.parentId, + // Ensure reactions object exists + reactions: { + likes: comment.likeCount || 0, + dislikes: comment.dislikeCount || 0, + }, + }; } export async function updateComment(id: string, dto: UpdateCommentDto): Promise {