forked from github/copilot-cli-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserService.js
More file actions
60 lines (48 loc) · 1.16 KB
/
Copy pathuserService.js
File metadata and controls
60 lines (48 loc) · 1.16 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
/**
* User service for business logic
*/
const userCache = {};
async function getUser(userId) {
// Check cache first
if (userCache[userId]) {
return userCache[userId];
}
const response = await fetch(`/api/users/${userId}`);
if (!response.ok) {
throw new Error('User not found');
}
const user = await response.json();
userCache[userId] = user;
return user;
}
async function updateUser(userId, updates) {
const response = await fetch(`/api/users/${userId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updates)
});
if (!response.ok) {
throw new Error('Failed to update user');
}
const user = await response.json();
userCache[userId] = user; // Update cache
return user;
}
async function deleteUser(userId) {
const response = await fetch(`/api/users/${userId}`, {
method: 'DELETE'
});
if (!response.ok) {
throw new Error('Failed to delete user');
}
delete userCache[userId]; // Clear from cache
}
function clearCache() {
Object.keys(userCache).forEach(key => delete userCache[key]);
}
module.exports = {
getUser,
updateUser,
deleteUser,
clearCache
};