-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathblog.ts
116 lines (102 loc) · 2.76 KB
/
blog.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
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
import { api } from "actionhero";
const separator = ";";
const postPrefix = "posts";
const commentPrefix = "comments:";
export async function postAdd(
userName: string,
title: string,
content: string,
) {
const key = buildTitleKey(userName, title);
const data = {
content,
title,
userName,
createdAt: new Date().getTime(),
updatedAt: new Date().getTime(),
};
await redis().hmset(key, data);
}
export async function postView(userName: string, title?: string) {
const key = buildTitleKey(userName, title);
return redis().hgetall(key);
}
export async function postsList(userName: string) {
const search = [postPrefix, userName, "*"].join(separator);
const keys = await redis().keys(search);
const titles = keys.map((key) => {
const parts = key.split(separator);
return parts[parts.length - 1];
});
titles.sort();
return titles;
}
export async function postEdit(
userName: string,
title: string,
content: string,
) {
const key = buildTitleKey(userName, title);
const data = await postView(key);
const newData = {
content,
title,
userName,
createdAt: data.createdAt,
updatedAt: new Date().getTime(),
};
await redis().hmset(key, newData);
}
export async function postDelete(userName: string, title: string) {
const key = buildTitleKey(userName, title);
await redis().del(key);
const commentKey = buildCommentKey(userName, title);
await redis().del(commentKey);
}
export async function commentAdd(
userName: string,
title: string,
commenterName: string,
comment: string,
) {
const key = buildCommentKey(userName, title);
const commentId = buildCommentId(commenterName);
const data = {
comment,
commenterName,
createdAt: new Date().getTime(),
commentId: commentId,
};
await redis().hset(key, commentId, JSON.stringify(data));
}
export async function commentsView(userName: string, title: string) {
const key = buildCommentKey(userName, title);
const data = await redis().hgetall(key);
const comments = Object.keys(data).map((key) => {
const comment = data[key];
return JSON.parse(comment);
});
return comments;
}
export async function commentDelete(
userName: string,
title: string,
commentId: string,
) {
const key = buildCommentKey(userName, title);
await redis().hdel(key, commentId);
}
function buildTitleKey(userName: string, title = "") {
// "posts:evan:my first post"
return postPrefix + separator + userName + separator + title;
}
function buildCommentKey(userName: string, title: string) {
// "comments:evan:my first post"
return commentPrefix + separator + userName + separator + title;
}
function buildCommentId(commenterName: string) {
return commenterName + new Date().getTime();
}
function redis() {
return api.redis.clients.client;
}