From 15fe67ed277e1fdc46896cb20fe103549e46e990 Mon Sep 17 00:00:00 2001 From: Simona Cotin Date: Mon, 5 Feb 2018 17:49:24 -0500 Subject: [PATCH] Initial commit --- .gitignore | 24 ++++++++++++++++++++ CreateHero/function.json | 18 +++++++++++++++ CreateHero/index.js | 29 ++++++++++++++++++++++++ CreateHero/sample.dat | 3 +++ DeleteHero/function.json | 18 +++++++++++++++ DeleteHero/index.js | 25 ++++++++++++++++++++ DeleteHero/sample.dat | 3 +++ GetHeroes/function.json | 18 +++++++++++++++ GetHeroes/index.js | 28 +++++++++++++++++++++++ GetHeroes/sample.dat | 3 +++ UpdateHero/function.json | 18 +++++++++++++++ UpdateHero/index.js | 29 ++++++++++++++++++++++++ UpdateHero/sample.dat | 3 +++ host.json | 5 ++++ package-lock.json | 49 ++++++++++++++++++++++++++++++++++++++++ package.json | 14 ++++++++++++ shared/index.js | 6 +++++ 17 files changed, 293 insertions(+) create mode 100644 .gitignore create mode 100644 CreateHero/function.json create mode 100644 CreateHero/index.js create mode 100644 CreateHero/sample.dat create mode 100644 DeleteHero/function.json create mode 100644 DeleteHero/index.js create mode 100644 DeleteHero/sample.dat create mode 100644 GetHeroes/function.json create mode 100644 GetHeroes/index.js create mode 100644 GetHeroes/sample.dat create mode 100644 UpdateHero/function.json create mode 100644 UpdateHero/index.js create mode 100644 UpdateHero/sample.dat create mode 100644 host.json create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 shared/index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..79e06ad --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +bin +obj +csx +.vs +edge +Publish +.vscode + +*.user +*.suo +*.cscfg +*.Cache +project.lock.json + +/packages +/TestResults + +/tools/NuGet.exe +/App_Data +/secrets +/data +.secrets +appsettings.json +local.settings.json diff --git a/CreateHero/function.json b/CreateHero/function.json new file mode 100644 index 0000000..cd230fd --- /dev/null +++ b/CreateHero/function.json @@ -0,0 +1,18 @@ +{ + "disabled": false, + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "route": "hero", + "methods": ["post"] + }, + { + "type": "http", + "direction": "out", + "name": "res" + } + ] +} diff --git a/CreateHero/index.js b/CreateHero/index.js new file mode 100644 index 0000000..4166d38 --- /dev/null +++ b/CreateHero/index.js @@ -0,0 +1,29 @@ +const MongoClient = require('mongodb').MongoClient; +const auth = require('../shared/index'); +module.exports = function(context, req) { + MongoClient.connect( + process.env.CosmosDBURL, + { auth: auth }, + (err, database) => { + if (err) throw err; + let hero = ({ id, name, saying } = req.body); + var db = database.db('admin'); + + db.collection('Heroes').insertOne( + { + id: hero.id, + name: hero.name, + saying: hero.saying + }, + (err, result) => { + if (err) throw err; + context.res = { + body: hero + }; + database.close(); + context.done(); + } + ); + } + ); +}; diff --git a/CreateHero/sample.dat b/CreateHero/sample.dat new file mode 100644 index 0000000..2e60943 --- /dev/null +++ b/CreateHero/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file diff --git a/DeleteHero/function.json b/DeleteHero/function.json new file mode 100644 index 0000000..5587cd3 --- /dev/null +++ b/DeleteHero/function.json @@ -0,0 +1,18 @@ +{ + "disabled": false, + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "route": "hero/{id}", + "methods": ["delete"] + }, + { + "type": "http", + "direction": "out", + "name": "res" + } + ] +} diff --git a/DeleteHero/index.js b/DeleteHero/index.js new file mode 100644 index 0000000..94a7a5d --- /dev/null +++ b/DeleteHero/index.js @@ -0,0 +1,25 @@ +const MongoClient = require('mongodb').MongoClient; +const auth = require('../shared/index'); +module.exports = function(context, req) { + context.log('JavaScript HTTP trigger function processed a request.'); + MongoClient.connect( + process.env.CosmosDBURL, + { auth: auth }, + (err, database) => { + if (err) throw err; + const db = database.db('admin'); + let heroId = req.params.id; + db + .collection('Heroes') + .findOneAndDelete({ id: heroId }, (err, result) => { + if (err) throw err; + context.res = { + status: 200, + body: { message: 'Hero deleted successfully!' } + }; + database.close(); + context.done(); + }); + } + ); +}; diff --git a/DeleteHero/sample.dat b/DeleteHero/sample.dat new file mode 100644 index 0000000..2e60943 --- /dev/null +++ b/DeleteHero/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file diff --git a/GetHeroes/function.json b/GetHeroes/function.json new file mode 100644 index 0000000..5eba0e2 --- /dev/null +++ b/GetHeroes/function.json @@ -0,0 +1,18 @@ +{ + "disabled": false, + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "route": "heroes", + "methods": ["get"] + }, + { + "type": "http", + "direction": "out", + "name": "res" + } + ] +} diff --git a/GetHeroes/index.js b/GetHeroes/index.js new file mode 100644 index 0000000..277ad4c --- /dev/null +++ b/GetHeroes/index.js @@ -0,0 +1,28 @@ +const MongoClient = require('mongodb').MongoClient; +const auth = require('../shared/index'); +module.exports = function(context, req) { + context.log('JavaScript HTTP trigger function processed a request.'); + MongoClient.connect( + process.env.CosmosDBURL, + { auth: auth }, + (err, database) => { + if (err) throw err; + console.log('Connected succesfully'); + const db = database.db(process.env.CosmosDB); + db + .collection('Heroes') + .find() + .toArray((err, result) => { + if (err) throw err; + console.log('retrieved succesfully'); + result.forEach(hero => delete hero._id); + context.res = { + //status: 200, + body: result + }; + database.close(); + context.done(); + }); + } + ); +}; diff --git a/GetHeroes/sample.dat b/GetHeroes/sample.dat new file mode 100644 index 0000000..2e60943 --- /dev/null +++ b/GetHeroes/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file diff --git a/UpdateHero/function.json b/UpdateHero/function.json new file mode 100644 index 0000000..ecfbd5b --- /dev/null +++ b/UpdateHero/function.json @@ -0,0 +1,18 @@ +{ + "disabled": false, + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "route": "hero/{id}", + "methods": ["put"] + }, + { + "type": "http", + "direction": "out", + "name": "res" + } + ] +} diff --git a/UpdateHero/index.js b/UpdateHero/index.js new file mode 100644 index 0000000..598f5ca --- /dev/null +++ b/UpdateHero/index.js @@ -0,0 +1,29 @@ +const MongoClient = require('mongodb').MongoClient; +const auth = require('../shared/index'); +module.exports = function(context, req) { + context.log('JavaScript HTTP trigger function processed a request.'); + MongoClient.connect( + process.env.CosmosDBURL, + { auth: auth }, + (err, database) => { + if (err) throw err; + const db = database.db('admin'); + let hero = ({ id, name, saying } = req.body); + let heroId = req.params.id; + db + .collection('Heroes') + .findOneAndUpdate( + { id: heroId }, + { $set: { id: hero.id, name: hero.name, saying: hero.saying } }, + (err, heroes) => { + if (err) throw err; + context.res = { + body: hero + }; + database.close(); + context.done(); + } + ); + } + ); +}; diff --git a/UpdateHero/sample.dat b/UpdateHero/sample.dat new file mode 100644 index 0000000..2e60943 --- /dev/null +++ b/UpdateHero/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file diff --git a/host.json b/host.json new file mode 100644 index 0000000..f8f12a2 --- /dev/null +++ b/host.json @@ -0,0 +1,5 @@ +{ + "http": { + "routePrefix": "api" + } +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..14629b0 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,49 @@ +{ + "name": "serverless", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "bson": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/bson/-/bson-1.0.4.tgz", + "integrity": "sha1-k8ENOeqltYQVy8QFLz5T5WKwtyw=" + }, + "mongodb": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.0.2.tgz", + "integrity": "sha512-E50FmpSQchZAimn2uPIegoNoH9UQYR1yiGHtQPmmg8/Ekc97w6owHoqaBoz+assnd9V5LxMzmQ/VEWMsQMgZhQ==", + "requires": { + "mongodb-core": "3.0.2" + } + }, + "mongodb-core": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.0.2.tgz", + "integrity": "sha512-p1B0qwFQUw6C1OlFJnrOJp8KaX7MuGoogRbTaupRt0y+pPRkMllHWtE9V6i1CDtTvI3/3sy2sQwqWez7zuXEAA==", + "requires": { + "bson": "1.0.4", + "require_optional": "1.0.1" + } + }, + "require_optional": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", + "integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==", + "requires": { + "resolve-from": "2.0.0", + "semver": "5.5.0" + } + }, + "resolve-from": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz", + "integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=" + }, + "semver": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz", + "integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..f43ded0 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "serverless", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "mongodb": "^3.0.2" + } +} diff --git a/shared/index.js b/shared/index.js new file mode 100644 index 0000000..8d2c0e3 --- /dev/null +++ b/shared/index.js @@ -0,0 +1,6 @@ +module.exports = { + auth: { + user: process.env.CosmosDBUser, + password: process.env.CosmosDBPass + } +};