Skip to content

Commit 3313ef4

Browse files
committed
Build API to fetch recent albums from past 1 year #32
1 parent 07c9f26 commit 3313ef4

2 files changed

Lines changed: 62 additions & 9 deletions

File tree

gallery/handler.js

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,19 @@ export const updatePhotoViewCount = async (event) => {
4646
ExpressionAttributeValues: { ":inc": 1 },
4747
ReturnValues: "UPDATED_NEW"
4848
}
49-
const updatedPhoto = (!userId || userId === undefined) ? {} : await database.update(params);
49+
const updatedPhoto = (!userId || false) ? {} : await database.update(params);
5050
return response.createResponse(updatedPhoto, 200);
5151
};
5252

53-
export const findAlbumCategories = async (event) => {
54-
let historicalDate = new Date(new Date().setFullYear(new Date().getFullYear() - 1));
53+
export const findRecentAlbumNames = async (event) => {
54+
const historicalDate = new Date(new Date().setFullYear(new Date().getFullYear() - 1));
5555
const params = {
5656
TableName: table,
5757
IndexName: "category-index",
58-
ProjectionExpression: "category, #name, startDateTime, endDateTime",
59-
FilterExpression: '#startDateTime < :historicalDate and #production = :production',
58+
ProjectionExpression: "category, subCategory, #collection, #name, startDateTime, endDateTime, photoCount, viewCount",
59+
FilterExpression: '#startDateTime >= :historicalDate and #production = :production',
6060
ExpressionAttributeNames: {
61+
'#collection': 'collection',
6162
'#name': 'name',
6263
'#startDateTime': "startDateTime",
6364
'#production': 'production'
@@ -68,11 +69,29 @@ export const findAlbumCategories = async (event) => {
6869
}
6970
};
7071
const albums = await database.scan(params);
72+
albums.sort((a,b) => (a.startDateTime > b.startDateTime) ? 1 : ((b.startDateTime > a.startDateTime) ? -1 : 0));
7173

72-
let categories = array.distinctValues(albums, "category");
74+
const signerPrivateKey = await secret.getSecretValue({SecretId: process.env['SIGNER_PRIVATE_KEY']});
7375

74-
return response.createResponse(categories, 200);
75-
}
76+
const albumList = albums.map((album) => {
77+
const prefix = getPrefix(true, 'images', source, album.category, album.subCategory, album.collection);
78+
const signatureParams = distribution.getSignatureParameters(
79+
process.env['CLOUDFRONT_PUBLIC_KEY_ID'],
80+
signerPrivateKey,
81+
true,
82+
prefix,
83+
1440);
84+
return {
85+
...album,
86+
signedUrl: distribution.getSignedUrlWithPolicy(
87+
{...signatureParams, url: prefix + album.name.replace(/&/g, 'and').replace(/ /g, '-').toLowerCase() + '.jpg'}
88+
)
89+
}
90+
});
91+
92+
return response.createResponse(albumList, 200);
93+
94+
};
7695

7796
export const findHistoricalAlbumCategories = async (event) => {
7897
const historicalDate = new Date(new Date().setFullYear(new Date().getFullYear() - 1));
@@ -101,14 +120,38 @@ export const findHistoricalAlbumCategories = async (event) => {
101120
return {
102121
name: category,
103122
signedUrl: distribution.getSignedUrlWithPolicy(
104-
{...signatureParams, url: prefix + category.replace(/&/g, 'and').replace(/ /g, '-').toLowerCase() + '.jpg'}
123+
{...signatureParams, url: prefix + category.replace(/&/g, 'and').replace(/ /g, '-').toLowerCase() + '.jpg'}
105124
)
106125
}
107126
});
108127

109128
return response.createResponse(albumCategoryList, 200);
110129
};
111130

131+
export const findAlbumCategories = async (event) => {
132+
let historicalDate = new Date(new Date().setFullYear(new Date().getFullYear() - 1));
133+
const params = {
134+
TableName: table,
135+
IndexName: "category-index",
136+
ProjectionExpression: "category, #name, startDateTime, endDateTime",
137+
FilterExpression: '#startDateTime < :historicalDate and #production = :production',
138+
ExpressionAttributeNames: {
139+
'#name': 'name',
140+
'#startDateTime': "startDateTime",
141+
'#production': 'production'
142+
},
143+
ExpressionAttributeValues: {
144+
':historicalDate': date.dateTimeFullFormatToString(historicalDate),
145+
':production': true
146+
}
147+
};
148+
const albums = await database.scan(params);
149+
150+
let categories = array.distinctValues(albums, "category");
151+
152+
return response.createResponse(categories, 200);
153+
};
154+
112155
export const findHistoricalAlbumSubCategories = async (event) => {
113156
const category = decodeURI(event.pathParameters.category);
114157
const historicalDate = new Date(new Date().setFullYear(new Date().getFullYear() - 1));

gallery/serverless.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ functions:
106106
environment:
107107
TABLE_NAME: "photo"
108108

109+
findRecentAlbumNames:
110+
handler: handler.findRecentAlbumNames
111+
events:
112+
- http:
113+
path: albumRecentNames
114+
method: get
115+
cors: true
116+
environment:
117+
TABLE_NAME: "gallery"
118+
109119
findHistoricalAlbumCategories:
110120
handler: handler.findHistoricalAlbumCategories
111121
events:

0 commit comments

Comments
 (0)