-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Netlify serverless functions for adding/retrieving lists (#10)
* Add Netlify serverless functions for adding and retrieving a list from MongoDB * Added randomstring node module as a dependency * Added MongoDB as a dependency
- Loading branch information
1 parent
f2447ee
commit ccb88cb
Showing
5 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
.env | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const { MongoClient } = require("mongodb"); | ||
const randomstring = require("randomstring"); | ||
|
||
const mongoClient = new MongoClient(process.env.MONGODB_URI); | ||
|
||
const clientPromise = mongoClient.connect(); | ||
|
||
const handler = async (event) => { | ||
const input = JSON.parse(event.body); | ||
|
||
const database = (await clientPromise).db(process.env.MONGODB_DATABASE); | ||
const collection = database.collection(process.env.MONGODB_COLLECTION); | ||
let newURI = randomstring.generate(8); | ||
try { | ||
await collection.insertOne({ | ||
[newURI]: input.items, | ||
views: 0, | ||
rating: 0, | ||
title: input.title, | ||
type: input.type, | ||
unlisted: input.unlisted, | ||
tags: input.tags, | ||
}); | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify(newURI), | ||
}; | ||
} catch (error) { | ||
return { statusCode: 500, body: error.toString() }; | ||
} | ||
}; | ||
|
||
module.exports = { handler }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { MongoClient } = require("mongodb"); | ||
|
||
const mongoClient = new MongoClient(process.env.MONGODB_URI); | ||
|
||
const clientPromise = mongoClient.connect(); | ||
|
||
const handler = async (event) => { | ||
const input = event.queryStringParameters.uri; | ||
try { | ||
const database = (await clientPromise).db(process.env.MONGODB_DATABASE); | ||
const collection = database.collection(process.env.MONGODB_COLLECTION); | ||
const results = await collection | ||
.find({ [input]: { $exists: true } }) | ||
.toArray(); | ||
if (results.length > 0) { | ||
let viewCount = results[0].views; | ||
collection.updateOne( | ||
{ [input]: { $exists: true } }, | ||
{ | ||
$set: { | ||
views: viewCount + 1, | ||
}, | ||
} | ||
); | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify(results[0]), | ||
}; | ||
} else { | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify("not_found"), | ||
}; | ||
} | ||
} catch (error) { | ||
return { statusCode: 500, body: error.toString() }; | ||
} | ||
}; | ||
|
||
module.exports = { handler }; |
Oops, something went wrong.