diff --git a/diagram.jpg b/diagram.jpg new file mode 100644 index 0000000..52c2cfd Binary files /dev/null and b/diagram.jpg differ diff --git a/server/config/express.js b/server/config/express.js index 27145f0..a3121b6 100644 --- a/server/config/express.js +++ b/server/config/express.js @@ -26,12 +26,18 @@ module.exports.init = function() { }); /* serve static files */ + app.use('/', express.static(__dirname + '/../../client')); + app.use('/public', express.static(__dirname + '/../../public')); /* use the listings router for requests to the api */ + app.use('/api/listings', listingsRouter); /* go to homepage for all routes not specified */ + app.all('*', function(req, res) { + res.sendFile(path.resolve('client/index.html')); + }); return app; -}; \ No newline at end of file +}; diff --git a/server/controllers/listings.server.controller.js b/server/controllers/listings.server.controller.js index 7fc96ac..f33286b 100644 --- a/server/controllers/listings.server.controller.js +++ b/server/controllers/listings.server.controller.js @@ -7,7 +7,6 @@ var mongoose = require('mongoose'), In this file, you should use Mongoose queries in order to retrieve/add/remove/update listings. On an error you should send a 404 status code, as well as the error message. On success (aka no error), you should send the listing(s) as JSON in the response. - HINT: if you are struggling with implementing these functions, refer back to this tutorial from assignment 3 https://scotch.io/tutorials/using-mongoosejs-in-node-js-and-mongodb-applications */ @@ -48,25 +47,63 @@ exports.update = function(req, res) { var listing = req.listing; /* Replace the article's properties with the new properties found in req.body */ + listing.name = req.body.name; + listing.code = req.body.code; + listing.address = req.body.address; + /* save the coordinates (located in req.results if there is an address property) */ + if(req.results){ + listing.coordinates.latitude = req.results.lat; + listing.coordinates.longitude = req.results.lng; + } + + /* Save the article */ + listing.save(function(err) { + if(err) { + console.log(err); + res.status(400).send(err); + } else { + res.json(listing); + } + }); + }; /* Delete a listing */ exports.delete = function(req, res) { var listing = req.listing; + /* Remove the article */ + + listing.remove(function(err) { + if(err) { + console.log(err); + res.status(400).send(err); + } else { + res.json(listing); + } + }); + }; /* Retreive all the directory listings, sorted alphabetically by listing code */ exports.list = function(req, res) { /* Your code here */ +Listing.find({}, null, {sort: {code:1}}, function(err, listings){ + if(err) { + console.log(err); + res.status(400).send(err); + } else { + res.json(listings); + } + }); + }; /* Middleware: find a listing by its ID, then pass it to the next request handler. - HINT: Find the listing using a mongoose query, bind it to the request object as the property 'listing', then finally call next @@ -80,4 +117,4 @@ exports.listingByID = function(req, res, next, id) { next(); } }); -}; \ No newline at end of file +};