generated from Hebilicious/serverless-esbuild-template
-
Notifications
You must be signed in to change notification settings - Fork 71
/
comment.ts
107 lines (96 loc) · 3.31 KB
/
comment.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
import { DynamoDB } from "aws-sdk"
import { ulid } from "ulid"
import { Item } from "./base"
import { getClient } from "./client"
import { Photo } from "./photo"
import { executeTransactWrite } from "./utils"
export class Comment extends Item {
commentingUsername: string
photoId: string
commentId: string
content: string
constructor(commentingUsername: string, photoId: string, content: string, commentId: string = ulid()) {
super()
this.commentingUsername = commentingUsername
this.photoId = photoId
this.content = content
this.commentId = commentId
}
static fromItem(item?: DynamoDB.AttributeMap): Comment {
if (!item) throw new Error("No item!")
return new Comment(item.commentingUsername.S, item.photoId.S, item.content.S, item.commentId.S)
}
get pk(): string {
return `PC#${this.photoId}`
}
get sk(): string {
return `COMMENT#${this.commentId}`
}
toItem(): Record<string, unknown> {
return {
...this.keys(),
commentingUsername: { S: this.commentingUsername},
photoId: { S: this.photoId },
content: { S: this.content},
commentId: { S: this.commentId }
}
}
}
export const commentOnPhoto = async (photo: Photo, commentingUsername: string, content: string): Promise<Comment> => {
const client = getClient()
const comment = new Comment(commentingUsername, photo.photoId, content)
try {
await executeTransactWrite({
client,
params: {
TransactItems: [
{
Put: {
TableName: process.env.TABLE_NAME,
Item: comment.toItem(),
ConditionExpression: "attribute_not_exists(PK)"
}
},
{
Update: {
TableName: process.env.TABLE_NAME,
Key: photo.keys(),
ConditionExpression: "attribute_exists(PK)",
UpdateExpression: "SET #commentCount = #commentCount+ :inc",
ExpressionAttributeNames: {
"#commentCount": "commentCount"
},
ExpressionAttributeValues: {
":inc": { N: "1" }
}
}
}
]
}
})
return comment
} catch (error) {
console.log(error)
throw error
}
}
export const listCommentsForPhoto = async (photoId: string): Promise<Comment[]> => {
const client = getClient()
const comment = new Comment("", photoId, "")
try {
const resp = await client
.query({
TableName: process.env.TABLE_NAME,
KeyConditionExpression: "PK = :pk",
ExpressionAttributeValues: {
":pk": { S: comment.pk }
},
ScanIndexForward: false
})
.promise()
return resp.Items.map((item) => Comment.fromItem(item))
} catch (error) {
console.log(error)
throw error
}
}