Last active
October 10, 2021 02:34
-
-
Save wolfordj/3080eeec83bdc3bcc916c3c8fcd3b383 to your computer and use it in GitHub Desktop.
Lodging App V2
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
function define(name, value) { | |
Object.defineProperty(exports, name, { | |
value: value, | |
enumerable: true | |
}); | |
} | |
define("LODGING", "Lodging"); | |
define("GUEST", "Guest"); |
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 Datastore = require('@google-cloud/datastore'); | |
const projectId = 'cs493-node-js-sample'; | |
module.exports.Datastore = Datastore; | |
module.exports.datastore = new Datastore({projectId:projectId}); | |
module.exports.fromDatastore = function fromDatastore(item){ | |
item.id = item[Datastore.KEY].id; | |
return item; | |
} |
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 bodyParser = require('body-parser'); | |
const router = express.Router(); | |
const ds = require('../lib/datastore'); | |
const datastore = ds.datastore; | |
const GUEST = "Guest"; | |
router.use(bodyParser.json()); | |
/* ------------- Begin guest Model Functions ------------- */ | |
function post_guest(name, description, price){ | |
var key = datastore.key(GUEST); | |
const new_guest = {"name": name}; | |
return datastore.save({"key":key, "data":new_guest}).then(() => {return key}); | |
} | |
function get_guests(req){ | |
var q = datastore.createQuery(GUEST).limit(2); | |
const results = {}; | |
var prev; | |
if(Object.keys(req.query).includes("cursor")){ | |
console.log(req.query); | |
prev = req.protocol + "://" + req.get("host") + req.baseUrl + "?cursor=" + req.query.cursor; | |
q = q.start(req.query.cursor); | |
} | |
return datastore.runQuery(q).then( (entities) => { | |
console.log(entities); | |
results.items = entities[0].map(ds.fromDatastore); | |
if(typeof prev !== 'undefined'){ | |
results.previous = prev; | |
} | |
if(entities[1].moreResults !== ds.Datastore.NO_MORE_RESULTS ){ | |
results.next = req.protocol + "://" + req.get("host") + req.baseUrl + "?cursor=" + entities[1].endCursor; | |
} | |
return results; | |
}); | |
} | |
function put_guest(id, name){ | |
const key = datastore.key([GUEST, parseInt(id,10)]); | |
const guest = {"name": name}; | |
return datastore.save({"key":key, "data":guest}); | |
} | |
function delete_guest(id){ | |
const key = datastore.key([GUEST, parseInt(id,10)]); | |
return datastore.delete(key); | |
} | |
/* ------------- End Model Functions ------------- */ | |
/* ------------- Begin Controller Functions ------------- */ | |
router.get('/', function(req, res){ | |
const guests = get_guests(req) | |
.then( (guests) => { | |
res.status(200).json(guests); | |
}); | |
}); | |
router.post('/', function(req, res){ | |
console.log(req.body); | |
post_guest(req.body.name) | |
.then( key => {res.status(200).send('{ "id": ' + key.id + ' }')} ); | |
}); | |
router.put('/:id', function(req, res){ | |
put_guest(req.params.id, req.body.name) | |
.then(res.status(200).end()); | |
}); | |
router.delete('/:id', function(req, res){ | |
delete_guest(req.params.id).then(res.status(200).end()) | |
}); | |
/* ------------- End Controller Functions ------------- */ | |
module.exports = router; |
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 router = module.exports = require('express').Router(); | |
router.use('/lodgings', require('./lodgings')); | |
router.use('/guests', require('./guests')); |
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 bodyParser = require('body-parser'); | |
const router = express.Router(); | |
const ds = require('../lib/datastore'); | |
const datastore = ds.datastore; | |
const LODGING = "Lodging"; | |
const GUEST = "Guest"; | |
router.use(bodyParser.json()); | |
/* ------------- 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(req){ | |
var q = datastore.createQuery(LODGING).limit(2); | |
const results = {}; | |
if(Object.keys(req.query).includes("cursor")){ | |
q = q.start(req.query.cursor); | |
} | |
return datastore.runQuery(q).then( (entities) => { | |
results.items = entities[0].map(ds.fromDatastore); | |
if(entities[1].moreResults !== ds.Datastore.NO_MORE_RESULTS ){ | |
results.next = req.protocol + "://" + req.get("host") + req.baseUrl + "?cursor=" + entities[1].endCursor; | |
} | |
return results; | |
}); | |
} | |
function get_lodging_guests(req, id){ | |
const key = datastore.key([LODGING, parseInt(id,10)]); | |
return datastore.get(key) | |
.then( (lodgings) => { | |
const lodging = lodgings[0]; | |
const guest_keys = lodging.guests.map( (g_id) => { | |
return datastore.key([GUEST, parseInt(g_id,10)]); | |
}); | |
return datastore.get(guest_keys); | |
}) | |
.then((guests) => { | |
guests = guests[0].map(ds.fromDatastore); | |
return guests; | |
}); | |
} | |
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); | |
} | |
function put_reservation(lid, gid){ | |
const l_key = datastore.key([LODGING, parseInt(lid,10)]); | |
return datastore.get(l_key) | |
.then( (lodging) => { | |
if( typeof(lodging[0].guests) === 'undefined'){ | |
lodging[0].guests = []; | |
} | |
lodging[0].guests.push(gid); | |
return datastore.save({"key":l_key, "data":lodging[0]}); | |
}); | |
} | |
/* ------------- End Model Functions ------------- */ | |
/* ------------- Begin Controller Functions ------------- */ | |
router.get('/', function(req, res){ | |
const lodgings = get_lodgings(req) | |
.then( (lodgings) => { | |
res.status(200).json(lodgings); | |
}); | |
}); | |
router.get('/:id/guests', function(req, res){ | |
const lodgings = get_lodging_guests(req, req.params.id) | |
.then( (lodgings) => { | |
res.status(200).json(lodgings); | |
}); | |
}); | |
router.post('/', function(req, res){ | |
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).end()); | |
}); | |
router.put('/:lid/guests/:gid', function(req, res){ | |
put_reservation(req.params.lid, req.params.gid) | |
.then(res.status(200).end()); | |
}); | |
router.delete('/:id', function(req, res){ | |
delete_lodging(req.params.id).then(res.status(200).end()) | |
}); | |
/* ------------- End Controller Functions ------------- */ | |
module.exports = router; |
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(); | |
app.use('/', require('./api/index')); | |
// 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