Skip to content

Commit 51fa32b

Browse files
committed
update page-views api
1 parent 39f4046 commit 51fa32b

File tree

2 files changed

+1220
-8
lines changed

2 files changed

+1220
-8
lines changed

api/index.ts

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,51 @@ const viewsEndpoint = new awsx.apigateway.API("bmpi-dev-post-views", {
2929

3030
const client = new aws.sdk.DynamoDB.DocumentClient();
3131

32-
const result = await client.update({
33-
TableName: counterTable.name.get(),
34-
Key: { id: route },
35-
UpdateExpression: "SET hit = if_not_exists(hit, :zero) + :incr",
36-
ExpressionAttributeValues: { ":zero": 0, ":incr": incr },
37-
ReturnValues:"UPDATED_NEW",
38-
}).promise();
32+
var count = 0;
3933

40-
let count = result.Attributes!.hit;
34+
if (route == 'bmpi-dev-dev-page-views') {
35+
const result = await client.scan({
36+
TableName: counterTable.name.get(),
37+
FilterExpression: "begins_with(id, :q) AND hit > :hit_filter",
38+
ExpressionAttributeValues: {":q": "bmpi.dev/dev/", ":hit_filter": 100},
39+
ProjectionExpression: "hit"
40+
}).promise();
41+
let items = result.Items!;
42+
for (let item of items) {
43+
count = count + item.hit;
44+
}
45+
} else if (route == 'bmpi-dev-self-page-views') {
46+
const result = await client.scan({
47+
TableName: counterTable.name.get(),
48+
FilterExpression: "begins_with(id, :q) AND hit > :hit_filter",
49+
ExpressionAttributeValues: {":q": "bmpi.dev/self/", ":hit_filter": 100},
50+
ProjectionExpression: "hit"
51+
}).promise();
52+
let items = result.Items!;
53+
for (let item of items) {
54+
count = count + item.hit;
55+
}
56+
} else if (route == 'bmpi-dev-money-page-views') {
57+
const result = await client.scan({
58+
TableName: counterTable.name.get(),
59+
FilterExpression: "begins_with(id, :q) AND hit > :hit_filter",
60+
ExpressionAttributeValues: {":q": "bmpi.dev/money/", ":hit_filter": 100},
61+
ProjectionExpression: "hit"
62+
}).promise();
63+
let items = result.Items!;
64+
for (let item of items) {
65+
count = count + item.hit;
66+
}
67+
} else {
68+
const result = await client.update({
69+
TableName: counterTable.name.get(),
70+
Key: { id: route },
71+
UpdateExpression: "SET hit = if_not_exists(hit, :zero) + :incr",
72+
ExpressionAttributeValues: { ":zero": 0, ":incr": incr },
73+
ReturnValues:"UPDATED_NEW",
74+
}).promise();
75+
count = result.Attributes!.hit;
76+
}
4177

4278
return {
4379
statusCode: 200,

0 commit comments

Comments
 (0)