comment func implemented
This commit is contained in:
parent
3718882f5b
commit
d7b82c0ec9
@ -6,7 +6,9 @@ import {
|
|||||||
IsInt,
|
IsInt,
|
||||||
Min,
|
Min,
|
||||||
Max,
|
Max,
|
||||||
|
MaxLength,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
|
import { Type } from 'class-transformer';
|
||||||
|
|
||||||
interface CommentEntity {
|
interface CommentEntity {
|
||||||
id: string;
|
id: string;
|
||||||
@ -29,7 +31,7 @@ interface CommentEntity {
|
|||||||
|
|
||||||
export class CreateCommentDto {
|
export class CreateCommentDto {
|
||||||
@IsString()
|
@IsString()
|
||||||
@Max(5000)
|
@MaxLength(5000)
|
||||||
content: string;
|
content: string;
|
||||||
|
|
||||||
@IsUUID()
|
@IsUUID()
|
||||||
@ -47,7 +49,7 @@ export class CreateCommentDto {
|
|||||||
|
|
||||||
export class UpdateCommentDto {
|
export class UpdateCommentDto {
|
||||||
@IsString()
|
@IsString()
|
||||||
@Max(5000)
|
@MaxLength(5000)
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
content?: string;
|
content?: string;
|
||||||
|
|
||||||
@ -118,12 +120,14 @@ export class FindCommentsDto {
|
|||||||
@IsInt()
|
@IsInt()
|
||||||
@Min(1)
|
@Min(1)
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
|
@Type(() => Number)
|
||||||
page?: number = 1;
|
page?: number = 1;
|
||||||
|
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@Min(1)
|
@Min(1)
|
||||||
@Max(100)
|
@Max(100)
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
|
@Type(() => Number)
|
||||||
limit?: number = 20;
|
limit?: number = 20;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -625,7 +625,14 @@ export interface CreateReactionDto {
|
|||||||
export async function fetchComments(params: FindCommentsParams = {}): Promise<CommentsResponse> {
|
export async function fetchComments(params: FindCommentsParams = {}): Promise<CommentsResponse> {
|
||||||
const searchParams = new URLSearchParams();
|
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 (value !== undefined && value !== null) {
|
||||||
if (typeof value === 'number') {
|
if (typeof value === 'number') {
|
||||||
searchParams.append(key, value.toString());
|
searchParams.append(key, value.toString());
|
||||||
@ -639,18 +646,55 @@ export async function fetchComments(params: FindCommentsParams = {}): Promise<Co
|
|||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Failed to fetch comments');
|
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> {
|
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`, {
|
const response = await authFetch(`${API_BASE_URL}/comments`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify(dto),
|
body: JSON.stringify(backendDto),
|
||||||
});
|
});
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Failed to create comment');
|
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> {
|
export async function updateComment(id: string, dto: UpdateCommentDto): Promise<Comment> {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user