Skip to content

Commit

Permalink
feat(backend): Handle removal of photos from collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
darkobits committed Oct 12, 2018
1 parent 5c4956b commit 5b70520
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
"clean": "del dist",
"check:styles": "tslint --project tsconfig.json --format codeFrame",
"check:types": "tsc --noEmit",
"2predeploy": "npm run clean && run-p check:*",
"predeploy": "npm run clean && run-p check:*",
"prebump": "npm run predeploy",
"bump": "standard-version",
"bump:beta": "npm run predeploy -- --prerelease=beta",
"deploy": "dotenv serverless deploy",
"deploy:prod": "dotenv serverless deploy -- --stage prod",
"logs": "dotenv serverless logs -- --tail --function",
"invoke": "dotenv serverless invoke -- --function",
"metrics": "dotenv serverless metrics -- --function"
},
"dependencies": {
Expand Down
32 changes: 27 additions & 5 deletions packages/backend/src/sync-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export default AWSLambdaFunction({
const db = new DynamoDBFactory();
const table = db.table(`inspirat-${process.env.STAGE}`);

const results = await Promise.all(unsplashPhotoCollection.map(async (photo: any) => {

// ----- Handle Additions --------------------------------------------------

const additionResults = await Promise.all(unsplashPhotoCollection.map(async (photo: any) => {
const existingItem = await table.where('id').eq(photo.id).consistent_read().get();

// We will get back a value like {} if the table doesn't have a record.
Expand All @@ -69,11 +72,30 @@ export default AWSLambdaFunction({
return true;
}));

const numInsertions = R.filter<boolean>(R.identity, results).length;
const numAdditions = R.filter<boolean>(R.identity, additionResults).length;

console.log(`[syncCollection] Added ${chalk.green(numAdditions.toString())} photos.`);


// ----- Handle Deletions --------------------------------------------------

const allExistingItems: Array<any> = await table.consistent_read().scan();

const deletionResults = await Promise.all(allExistingItems.map(async (photo: any) => {
const itemInUnsplashCollection = R.find(R.propEq('id', photo.id), unsplashPhotoCollection);

if (!itemInUnsplashCollection) {
await table.where('id').eq(photo.id).delete();
return true;
}

return false;
}));

const numDeletions = R.filter<boolean>(R.identity, deletionResults).length;

console.log(`[syncCollection] Added ${chalk.green(numInsertions.toString())} photos.`);
console.log(`[syncCollection] Deleted ${chalk.green(numDeletions.toString())} photos.`);

res.body = `Added ${numInsertions} photos.`;
return;
res.body = {added: numAdditions, deleted: numDeletions};
}
});

0 comments on commit 5b70520

Please sign in to comment.