Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
simonaco committed Feb 5, 2018
0 parents commit 15fe67e
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions CreateHero/function.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
29 changes: 29 additions & 0 deletions CreateHero/index.js
Original file line number Diff line number Diff line change
@@ -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();
}
);
}
);
};
3 changes: 3 additions & 0 deletions CreateHero/sample.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Azure"
}
18 changes: 18 additions & 0 deletions DeleteHero/function.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
25 changes: 25 additions & 0 deletions DeleteHero/index.js
Original file line number Diff line number Diff line change
@@ -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();
});
}
);
};
3 changes: 3 additions & 0 deletions DeleteHero/sample.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Azure"
}
18 changes: 18 additions & 0 deletions GetHeroes/function.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
28 changes: 28 additions & 0 deletions GetHeroes/index.js
Original file line number Diff line number Diff line change
@@ -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();
});
}
);
};
3 changes: 3 additions & 0 deletions GetHeroes/sample.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Azure"
}
18 changes: 18 additions & 0 deletions UpdateHero/function.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
29 changes: 29 additions & 0 deletions UpdateHero/index.js
Original file line number Diff line number Diff line change
@@ -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();
}
);
}
);
};
3 changes: 3 additions & 0 deletions UpdateHero/sample.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Azure"
}
5 changes: 5 additions & 0 deletions host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"http": {
"routePrefix": "api"
}
}
49 changes: 49 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
6 changes: 6 additions & 0 deletions shared/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
auth: {
user: process.env.CosmosDBUser,
password: process.env.CosmosDBPass
}
};

0 comments on commit 15fe67e

Please sign in to comment.