generated from Hebilicious/serverless-esbuild-template
-
Notifications
You must be signed in to change notification settings - Fork 69
/
like.ts
115 lines (102 loc) · 3.3 KB
/
like.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
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 Like extends Item {
likingUsername: string
photoId: string
likeId: string
constructor(likingUsername: string, photoId: string, likeId: string = ulid()) {
super()
this.likingUsername = likingUsername
this.photoId = photoId
this.likeId = likeId
}
static fromItem(item?: DynamoDB.AttributeMap): Like {
if (!item) throw new Error("No item!")
return new Like(item.likingUsername.S, item.photoId.S, item.likeId.S)
}
get pk(): string {
return `PL#${this.photoId}`
}
get sk(): string {
return `LIKE#${this.likingUsername}`
}
get gsi1pk(): string {
return this.pk
}
get gsi1sk(): string {
return `LIKE#${this.likeId}`
}
toItem(): Record<string, unknown> {
return {
...this.keys(),
GSI1PK: { S: this.gsi1pk },
GSI1SK: { S: this.gsi1sk },
likingUsername: { S: this.likingUsername },
photoId: { S: this.photoId },
likeId: { S: this.likeId }
}
}
}
export const likePhoto = async (photo: Photo, likingUsername: string): Promise<Like> => {
const client = getClient()
const like = new Like(likingUsername, photo.photoId)
try {
await executeTransactWrite({
client,
params: {
TransactItems: [
{
Put: {
TableName: process.env.TABLE_NAME,
Item: like.toItem(),
ConditionExpression: "attribute_not_exists(PK)"
}
},
{
Update: {
TableName: process.env.TABLE_NAME,
Key: photo.keys(),
ConditionExpression: "attribute_exists(PK)",
UpdateExpression: "SET #likesCount = #likesCount + :inc",
ExpressionAttributeNames: {
"#likesCount": "likesCount"
},
ExpressionAttributeValues: {
":inc": { N: "1" }
}
}
}
]
}
})
return like
} catch (error) {
console.log(error)
throw error
}
}
export const listLikesForPhoto = async (photoId: string): Promise<Like[]> => {
const client = getClient()
const like = new Like("", photoId)
try {
const resp = await client
.query({
TableName: process.env.TABLE_NAME,
IndexName: "GSI1",
KeyConditionExpression: "GSI1PK = :gsi1pk",
ExpressionAttributeValues: {
":gsi1pk": { S: like.gsi1pk }
},
ScanIndexForward: false
})
.promise()
return resp.Items.map((item) => Like.fromItem(item))
} catch (error) {
console.log(error)
throw error
}
}