Created
October 7, 2018 00:45
-
-
Save wolfordj/e4aa4c936311110940b62feb54108989 to your computer and use it in GitHub Desktop.
Rest Lodgings
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
{ | |
"GCLOUD_PROJECT": "[YOUR-PROJECT-ID]", | |
"DATA_BACKEND": "datastore" | |
} |
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
{ | |
"name": "class_sample", | |
"version": "1.0.0", | |
"description": "", | |
"main": "server.js", | |
"dependencies": { | |
"@google-cloud/datastore": "1.3.4", | |
"body-parser": "^1.18.3", | |
"express": "^4.16.3", | |
"google-gax": "^0.20.0" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node server.js" | |
}, | |
"author": "", | |
"license": "ISC" | |
} |
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
const express = require('express'); | |
const app = express(); | |
const Datastore = require('@google-cloud/datastore'); | |
const bodyParser = require('body-parser'); | |
const projectId = 'YOUR-PROJECT-ID'; | |
const datastore = new Datastore({projectId:projectId}); | |
const LODGING = "Lodging"; | |
const router = express.Router(); | |
app.use(bodyParser.json()); | |
function fromDatastore(item){ | |
item.id = item[Datastore.KEY].id; | |
return item; | |
} | |
/* ------------- Begin Lodging Model Functions ------------- */ | |
function post_lodging(name, description, price){ | |
var key = datastore.key(LODGING); | |
const new_lodging = {"name": name, "description": description, "price": price}; | |
return datastore.save({"key":key, "data":new_lodging}).then(() => {return key}); | |
} | |
function get_lodgings(){ | |
const q = datastore.createQuery(LODGING); | |
return datastore.runQuery(q).then( (entities) => { | |
return entities[0].map(fromDatastore); | |
}); | |
} | |
function put_lodging(id, name, description, price){ | |
const key = datastore.key([LODGING, parseInt(id,10)]); | |
const lodging = {"name": name, "description": description, "price": price}; | |
return datastore.save({"key":key, "data":lodging}); | |
} | |
function delete_lodging(id){ | |
const key = datastore.key([LODGING, parseInt(id,10)]); | |
return datastore.delete(key); | |
} | |
/* ------------- End Model Functions ------------- */ | |
/* ------------- Begin Controller Functions ------------- */ | |
router.get('/', function(req, res){ | |
const lodgings = get_lodgings() | |
.then( (lodgings) => { | |
console.log(lodgings); | |
res.status(200).json(lodgings); | |
}); | |
}); | |
router.post('/', function(req, res){ | |
console.log(req.body); | |
post_lodging(req.body.name, req.body.description, req.body.price) | |
.then( key => {res.status(200).send('{ "id": ' + key.id + ' }')} ); | |
}); | |
router.put('/:id', function(req, res){ | |
put_lodging(req.params.id, req.body.name, req.body.description, req.body.price) | |
.then(res.status(200)); | |
}); | |
router.delete('/:id', function(req, res){ | |
delete_lodging(req.params.id).then(res.status(200).end()) | |
}); | |
/* ------------- End Controller Functions ------------- */ | |
app.use('/lodgings', router); | |
// Listen to the App Engine-specified port, or 8080 otherwise | |
const PORT = process.env.PORT || 8080; | |
app.listen(PORT, () => { | |
console.log(`Server listening on port ${PORT}...`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment