comment func implemented

This commit is contained in:
echo 2026-02-04 23:08:47 +01:00
parent 3718882f5b
commit d7b82c0ec9
2 changed files with 54 additions and 6 deletions

View File

@ -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;
}

View File

@ -625,7 +625,14 @@ export interface CreateReactionDto {
export async function fetchComments(params: FindCommentsParams = {}): Promise<CommentsResponse> {
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<Co
if (!response.ok) {
throw new Error('Failed to fetch comments');
}
return response.json();
const data = await response.json();
// Map backend response to frontend interface
const mappedData = data.map((comment: any) => ({
...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<Comment> {
// 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<Comment> {