diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/app.js b/app.js index e69de29..90b7c83 100644 --- a/app.js +++ b/app.js @@ -0,0 +1,22 @@ +/* + * Homework + * JSCRIPT300-Spring2015/Module_5 + * by Diane Zevenbergen + */ + +var express = require('express'); +var app = express(); +var bodyParser = require('body-parser'); +var truckRoutes = require('./routes/truckRoutes'); + +var port = process.env.PORT || 3000; + +app.use(bodyParser.urlencoded({ extended: true })); + +app.use(express.static('public')); + +app.use('/trucks', truckRoutes); + +app.listen(port, function() { + console.log('listening on port 3000'); +}); diff --git a/models/truckModel.js b/models/truckModel.js index 1dc933f..156ee9a 100644 --- a/models/truckModel.js +++ b/models/truckModel.js @@ -3,13 +3,20 @@ // name your model 'Truck' (see the example in the slide deck for 'Book') // this is to match the name of the collection the data was imported into -// var foodTruckSchema = new Schema({ -// name: String, -// foodType: [String], -// schedule: [String], -// payment: [String], -// description: String, -// website: String, -// Facebook: String, -// Twitter: String -// }); +//Diane added these two lines???? +var mongoose = require('mongoose'); +var Schema = mongoose.Schema; + +// Diane changed 'foodTruckSchema' to 'truckModel' +var truckModel = new Schema({ + name: String, + foodType: [String], + schedule: [String], + payment: [String], + description: String, + website: String, + Facebook: String, + Twitter: String + }); + +module.exports = mongoose.model('Truck', truckModel); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..3a3cd37 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "Module_5_HW", + "version": "1.0.0", + "description": "For this assignment, you're going to fork this repository to your own Github account and clone that repository locally. Inside the repo you'll find a placeholder for app.js, a placeholder routes/truckRoutes.js, a model/truckModel.js, and a 'public' directory. Inside the public directory you'll find an index.html file, a trucks.css file, and a client.js file.", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/dianezev/Module_5.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/dianezev/Module_5/issues" + }, + "homepage": "https://github.com/dianezev/Module_5", + "dependencies": { + "body-parser": "^1.12.3", + "express": "^4.12.3", + "mongoose": "^4.0.2", + "underscore": "^1.8.3" + } +} diff --git a/public/client.js b/public/client.js index 882ee1a..ef45eb9 100644 --- a/public/client.js +++ b/public/client.js @@ -2,9 +2,26 @@ $(function () { 'use strict'; + /* + * QUESTION: I wanted to add underscore here in order to use + * _.sortBy to display trucks in alpha order, but I got the + * RefError 'require is not defined'. What am I doing wrong? + * + * var _ = require('underscore'); + */ + var foodTypes = []; $.get('/trucks', function (truckList) { + + /* + * This is where I wanted to sort but couldn't get + * my require statement above to work + * + * // Sort truckList, case-insensitive (from stackoverflow) + * truckList = _.sortBy(truckList, function (i) { return i.name.toLowerCase(); }); + */ + var list = []; if (truckList) { truckList.forEach(function (truck) { @@ -18,10 +35,10 @@ $(function () { e.preventDefault(); var $form = $(this); - +console.log(foodTypes); var truckData = { name: $('[name=name]').val(), - type: foodTypes, + foodType: foodTypes, schedule: getSchedule(), description: $('[name=description]').val(), payment: getPaymentTypes(), @@ -30,7 +47,7 @@ $(function () { Twitter: $('[name=Twitter]').val() }; - $.ajax({ + $.ajax({ method: 'POST', url: '/trucks', data: truckData diff --git a/routes/truckRoutes.js b/routes/truckRoutes.js index a0b0092..c010c70 100644 --- a/routes/truckRoutes.js +++ b/routes/truckRoutes.js @@ -1,3 +1,54 @@ -// connect to mongodb in this module as this is where you'll be making creat/read/delete calls to your database -// use 'mongodb://localhost/foodTruckAPI' for your mongoose connection string -// remember this is a Node module +/* + * Homework + * JSCRIPT300-Spring2015/Module_5 + * by Diane Zevenbergen + */ + +var express = require('express'); +var router = express.Router(); + +var mongoose = require('mongoose'); +var db = mongoose.connect('mongodb://localhost/foodTruckAPI'); +var bodyParser = require('body-parser'); + +// Defining a model for Mongoose to use ('Truck' is instance of truckModel) +var Truck = require('../models/truckModel'); + +router.route('/') + .get(function (request, response) { + Truck.find(function (error, trucks) { + if (error) { + response.status(500).send(error); + } else { + response.json(trucks); + } + }); + }) + .post(function (request, response) { + var truck = new Truck(request.body); + truck.save(); + response.status(201).send(truck); + }); + +router.route('/:truckId') + .get(function (request, response) { + Truck.findById(request.params.truckId, function (error, truck) { + if (error) { + response.status(500).send(error); + } else { + response.json(truck); + } + }); + }) + .delete(function (request, response) { + Truck.findById(request.params.truckId, function (error, truck) { + if (error) { + response.status(500).send(error); + } else { + truck.remove(); + response.status(204).send('removed'); + } + }); + }); + +module.exports = router;