Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added diagram.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion server/config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};
43 changes: 40 additions & 3 deletions server/controllers/listings.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand All @@ -80,4 +117,4 @@ exports.listingByID = function(req, res, next, id) {
next();
}
});
};
};