File size: 1,679 Bytes
d97b8f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { API_BASE_URL } from '@/config';
import apiClient from '@/lib/api/axios-instance';
import { createHeaders } from '@/lib/api';
import axios from 'axios';

export interface Comment {
  commentId: number;
  entityId: number;
  entityType: string;
  commentText: string;
  userId: number;
  createdBy: string | null;
  createdAt: string;
  updatedBy: string | null;
  updatedAt: string | null;
  entity: any | null;
}

class CommentsApi {
  private baseUrl: string;

  constructor() {
    this.baseUrl = `/api/Comments`;
  }

  async getByEntity(entityType: string, entityId: number): Promise<Comment[]> {
    try {
      const response = await apiClient.get(
        `${this.baseUrl}/GetByEntity?entityName=${entityType}&entityId=${entityId}`
      );
      
      return response.data;
    } catch (error) {
      console.error('Error fetching comments:', error);
      throw error;
    }
  }

  async create(comment: Omit<Comment, 'commentId' | 'entity'>): Promise<Comment> {
    try {
      const response = await apiClient.post(this.baseUrl, { 
        commentId: 0,
        ...comment
      }, {
        headers: {
          'Content-Type': 'application/json',
        }
      });
      
      return response.data;
    } catch (error) {
      console.error('Error creating comment:', error);
      throw error;
    }
  }

  async delete(commentId: number): Promise<void> {
    try {
      await apiClient.delete(`${this.baseUrl}/${commentId}`);
    } catch (error) {
      console.error('Error deleting comment:', error);
      throw error;
    }
  }
}

export const commentsApi = new CommentsApi();