Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
await redis.del(commentKey)
}
api.blog.commentAdd = async (userName, title, commenterName, comment) => {
const key = api.blog.buildCommentKey(userName, title)
const commentId = api.blog.buildCommentId(commenterName)
const data = {
comment,
commenterName,
createdAt: new Date().getTime(),
commentId: commentId
}
await redis.hset(key, commentId, JSON.stringify(data))
}
api.blog.commentsView = async (userName, title) => {
const key = api.blog.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
}
api.blog.commentDelete = async (userName, title, commentId) => {
const key = api.blog.buildCommentKey(userName, title)
await redis.hdel(key, commentId)
}
api.blog.buildTitleKey = (userName, title) => {
// "posts:evan:my first post"
api.blog.commentDelete = async (userName, title, commentId) => {
const key = api.blog.buildCommentKey(userName, title)
await redis.hdel(key, commentId)
}
api.blog.buildTitleKey = (userName, title) => {
// "posts:evan:my first post"
return api.blog.postPrefix + api.blog.separator + userName + api.blog.separator + title
}
api.blog.buildCommentKey = (userName, title) => {
// "comments:evan:my first post"
return api.blog.commentPrefix + api.blog.separator + userName + api.blog.separator + title
}
api.blog.buildCommentId = (commenterName) => {
return commenterName + new Date().getTime()
}
}
return JSON.parse(comment)
})
return comments
}
api.blog.commentDelete = async (userName, title, commentId) => {
const key = api.blog.buildCommentKey(userName, title)
await redis.hdel(key, commentId)
}
api.blog.buildTitleKey = (userName, title) => {
// "posts:evan:my first post"
return api.blog.postPrefix + api.blog.separator + userName + api.blog.separator + title
}
api.blog.buildCommentKey = (userName, title) => {
// "comments:evan:my first post"
return api.blog.commentPrefix + api.blog.separator + userName + api.blog.separator + title
}
api.blog.buildCommentId = (commenterName) => {
return commenterName + new Date().getTime()
}
}
api.blog.buildTitleKey = (userName, title) => {
// "posts:evan:my first post"
return api.blog.postPrefix + api.blog.separator + userName + api.blog.separator + title
}
async initialize () {
const redis = api.redis.clients.client
api.blog = {
separator: ';',
postPrefix: 'posts',
commentPrefix: 'comments:'
}
api.blog.postAdd = async (userName, title, content) => {
const key = api.blog.buildTitleKey(userName, title)
const data = {
content,
title,
userName,
createdAt: new Date().getTime(),
updatedAt: new Date().getTime()
}
await redis.hmset(key, data)
}
api.blog.postView = async (userName, title) => {
const key = api.blog.buildTitleKey(userName, title)
return redis.hgetall(key)
}
async run ({ response, params }) {
response.post = await api.blog.postView(params.userName, params.title)
}
}
api.users.delete = async (userName, password) => {
await redis.del(this.usersHash, userName)
const titles = await api.blog.postsList(userName)
for (const i in titles) {
await api.blog.deletePost(userName, titles[i])
}
}
async run ({ params }) {
await api.blog.commentDelete(params.userName, params.title, params.commentId)
}
}
async run ({ params }) {
await api.blog.commentAdd(params.userName, params.title, params.commenterName, params.comment)
}
}
async run ({ params }) {
await api.blog.postDelete(params.userName, params.title)
}
}