forked from BrandonVC29/CRUD_TYPESCRIPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
43 lines (35 loc) · 1.25 KB
/
index.ts
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
import { Canister, query, text, update, Void } from 'azle';
interface MessageLikes {
message: string;
likes: string;
}
const messages: { [messageId: string]: MessageLikes } = {};
export default Canister({
getMessage: query([text], text, (messageId) => {
return messages[messageId] ? messages[messageId].message : '';
}),
getLikes: query([text], text, (messageId) => {
return messages[messageId] ? messages[messageId].likes : '0';
}),
setMessage: update([text, text], Void, (messageId, newMessage) => {
messages[messageId] = { message: newMessage, likes: '0' };
}),
updateMessage: update([text, text], Void, (messageId, updatedMessage) => {
if (messages[messageId]) {
messages[messageId].message = updatedMessage;
}
}),
deleteMessage: update([text], Void, (messageId) => {
if (messages[messageId]) {
delete messages[messageId];
}
}),
addLike: update([text], text, (messageId) => {
if (messages[messageId]) {
messages[messageId].likes = (parseInt(messages[messageId].likes) + 1).toString();
return messages[messageId].likes;
} else {
return 'Mensaje no encontrado';
}
})
});