comment.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use server';
  2. import CommentListRequest from '@/dtos/request/forum/comment/commentListRequest';
  3. import CommentListResponse from '@/dtos/response/forum/comment/commentListResponse';
  4. import CommentCreateResponse from '@/dtos/response/forum/comment/commentCreateResponse';
  5. import CommentUpdateResponse from '@/dtos/response/forum/comment/commentUpdateResponse';
  6. import { ResultDto } from '@/dtos/response/common';
  7. import { fetchJson } from '@/lib/utils/server';
  8. // 댓글 목록 조회
  9. export async function fetchCommentList(params: CommentListRequest): Promise<ResultDto<CommentListResponse>> {
  10. const queryParams = new URLSearchParams();
  11. queryParams.set('postID', String(params.postID));
  12. queryParams.set('pageNum', String(params.page));
  13. if (params.perPage) queryParams.set('perPage', String(params.perPage));
  14. if (params.sort !== undefined) queryParams.set('sort', String(params.sort));
  15. return await fetchJson<CommentListResponse>(`/api/forum/comments?${queryParams.toString()}`, {
  16. method: 'GET',
  17. headers: {'Accept': 'application/json'}
  18. });
  19. }
  20. // 댓글/답글 등록
  21. export async function fetchCommentCreate(formData: FormData): Promise<ResultDto<CommentCreateResponse>> {
  22. return await fetchJson<CommentCreateResponse>('/api/forum/comments', {
  23. method: 'POST',
  24. headers: {'Accept': 'application/json'},
  25. body: formData
  26. });
  27. }
  28. // 댓글/답글 수정
  29. export async function fetchCommentUpdate(commentID: number, formData: FormData): Promise<ResultDto<CommentUpdateResponse>> {
  30. return await fetchJson<CommentUpdateResponse>(`/api/forum/comments/${commentID}`, {
  31. method: 'PUT',
  32. headers: {'Accept': 'application/json'},
  33. body: formData
  34. });
  35. }
  36. // 댓글/답글 삭제
  37. export async function fetchCommentDelete(commentID: number): Promise<ResultDto> {
  38. return await fetchJson(`/api/forum/comments/${commentID}`, {
  39. method: 'DELETE',
  40. headers: {'Accept': 'application/json'}
  41. });
  42. }