-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-client.js
More file actions
272 lines (248 loc) · 9.64 KB
/
api-client.js
File metadata and controls
272 lines (248 loc) · 9.64 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// ==========================================
// API CLIENT - SQL DATABASE COMMUNICATION
// ==========================================
const API_URL = 'http://localhost:3001/api';
class DatabaseAPI {
// ==========================================
// COMPLAINT OPERATIONS
// ==========================================
static async getAllComplaints() {
try {
const response = await fetch(`${API_URL}/complaints`);
if (!response.ok) throw new Error('Failed to fetch complaints');
const complaints = await response.json();
console.log(`Retrieved ${complaints.length} complaints from database`);
return complaints;
} catch (error) {
console.error('Error getting complaints:', error);
return [];
}
}
static async getComplaintsByStudent(studentId) {
try {
const response = await fetch(`${API_URL}/complaints/student/${studentId}`);
if (!response.ok) throw new Error('Failed to fetch student complaints');
return await response.json();
} catch (error) {
console.error('Error getting student complaints:', error);
return [];
}
}
static async getComplaintsByStatus(status) {
try {
const response = await fetch(`${API_URL}/complaints/status/${status}`);
if (!response.ok) throw new Error('Failed to fetch complaints by status');
return await response.json();
} catch (error) {
console.error('Error getting complaints by status:', error);
return [];
}
}
static async getComplaintsByCategory(category) {
try {
const response = await fetch(`${API_URL}/complaints/category/${category}`);
if (!response.ok) throw new Error('Failed to fetch complaints by category');
return await response.json();
} catch (error) {
console.error('Error getting complaints by category:', error);
return [];
}
}
static async getComplaintById(complaintId) {
try {
const response = await fetch(`${API_URL}/complaints/${complaintId}`);
if (!response.ok) throw new Error('Failed to fetch complaint');
return await response.json();
} catch (error) {
console.error('Error getting complaint by ID:', error);
return null;
}
}
static async addComplaint(complaint) {
try {
const response = await fetch(`${API_URL}/complaints`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(complaint)
});
if (!response.ok) throw new Error('Failed to add complaint');
console.log('Complaint added:', complaint.id);
return await response.json();
} catch (error) {
console.error('Error adding complaint:', error);
return null;
}
}
static async updateComplaint(complaint) {
try {
const response = await fetch(`${API_URL}/complaints/${complaint.id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(complaint)
});
if (!response.ok) throw new Error('Failed to update complaint');
console.log('Complaint updated:', complaint.id);
return await response.json();
} catch (error) {
console.error('Error updating complaint:', error);
return null;
}
}
static async deleteComplaint(complaintId) {
try {
const response = await fetch(`${API_URL}/complaints/${complaintId}`, {
method: 'DELETE'
});
if (!response.ok) throw new Error('Failed to delete complaint');
console.log('Complaint deleted:', complaintId);
return await response.json();
} catch (error) {
console.error('Error deleting complaint:', error);
return null;
}
}
static async countComplaints() {
try {
const response = await fetch(`${API_URL}/complaints/count/total`);
if (!response.ok) throw new Error('Failed to count complaints');
const result = await response.json();
return result.count || 0;
} catch (error) {
console.error('Error counting complaints:', error);
return 0;
}
}
// ==========================================
// USER OPERATIONS
// ==========================================
static async getStudentByUsername(username) {
try {
const response = await fetch(`${API_URL}/users/student/${username}`);
if (!response.ok) throw new Error('Failed to fetch student');
return await response.json();
} catch (error) {
console.error('Error getting student by username:', error);
return null;
}
}
static async getAllStudents() {
try {
const response = await fetch(`${API_URL}/users/students/all`);
if (!response.ok) throw new Error('Failed to fetch students');
return await response.json();
} catch (error) {
console.error('Error getting all students:', error);
return [];
}
}
static async addStudent(student) {
try {
const response = await fetch(`${API_URL}/users/student`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(student)
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Failed to add student');
}
console.log('Student added to database:', student.username);
return await response.json();
} catch (error) {
console.error('Error adding student:', error);
throw error;
}
}
static async updateStudent(username, student) {
try {
const response = await fetch(`${API_URL}/users/student/${username}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(student)
});
if (!response.ok) throw new Error('Failed to update student');
console.log('Student updated in database:', username);
return await response.json();
} catch (error) {
console.error('Error updating student:', error);
return null;
}
}
static async deleteStudent(username) {
try {
const response = await fetch(`${API_URL}/users/student/${username}`, {
method: 'DELETE'
});
if (!response.ok) throw new Error('Failed to delete student');
console.log('Student deleted from database:', username);
return await response.json();
} catch (error) {
console.error('Error deleting student:', error);
return null;
}
}
static async studentExists(username) {
try {
const student = await this.getStudentByUsername(username);
return student && student.username !== undefined;
} catch (error) {
console.error('Error checking if student exists:', error);
return false;
}
}
// ==========================================
// CHAT OPERATIONS
// ==========================================
static async addChatMessage(message) {
try {
const response = await fetch(`${API_URL}/chats`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(message)
});
if (!response.ok) throw new Error('Failed to add chat message');
console.log('✓ Chat message added to database:', message.id);
return await response.json();
} catch (error) {
console.error('Error adding chat message:', error);
throw error;
}
}
static async getChatsByComplaintId(complaintId) {
try {
const response = await fetch(`${API_URL}/chats/complaint/${complaintId}`);
if (!response.ok) throw new Error('Failed to fetch chat messages');
const chats = await response.json();
console.log(`Retrieved ${chats.length} chat messages for complaint ${complaintId}`);
return chats;
} catch (error) {
console.error('Error getting chats by complaint ID:', error);
return [];
}
}
static async getAllChats() {
try {
const response = await fetch(`${API_URL}/chats`);
if (!response.ok) throw new Error('Failed to fetch chats');
return await response.json();
} catch (error) {
console.error('Error getting all chats:', error);
return [];
}
}
static async deleteChatsForComplaint(complaintId) {
try {
const response = await fetch(`${API_URL}/chats/complaint/${complaintId}`, {
method: 'DELETE'
});
if (!response.ok) throw new Error('Failed to delete chats');
console.log('Chats deleted for complaint:', complaintId);
return await response.json();
} catch (error) {
console.error('Error deleting chats:', error);
return null;
}
}
}
// Create global API instance
const dbAPI = DatabaseAPI;