-
Notifications
You must be signed in to change notification settings - Fork 0
Homework 4 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Homework 4 #2
Changes from all commits
b92f3bf
3b6c96c
d12e015
8109d4d
f75c6dc
1aa53bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| var mongoose = require('mongoose'); | ||
| var path = require('path'); | ||
| var express = require('express'); | ||
| var logger = require('morgan'); | ||
| var exphbs = require('express-handlebars'); | ||
| var bodyParser = require('body-parser'); | ||
| var ingredients = require('./routes/ingredients') | ||
| var order = require('./routes/order') | ||
| var kitchen = require('./routes/kitchen') | ||
| // var ingredient = require('./models/ingredientModel') | ||
|
|
||
| var app = express(); | ||
| app.engine('handlebars', exphbs({defaultLayout: 'main'})); | ||
| app.set('view engine', 'handlebars'); | ||
|
|
||
| var mongoURI = process.env.MONGOURI || "mongodb://localhost/test"; | ||
| var PORT = process.env.PORT || 3000; | ||
| mongoose.connect(mongoURI); | ||
|
|
||
|
|
||
| app.use(logger('dev')); | ||
| app.use(bodyParser.json()); | ||
| app.use(bodyParser.urlencoded({ extended: false })); | ||
| app.use(express.static(path.join(__dirname, 'public'))); | ||
|
|
||
| app.get('/', ingredients.addIngredients); | ||
| app.post('/newIngredient', ingredients.addIngredientsPOST); | ||
| app.get('/ingredients', ingredients.ingredientsRender); | ||
| app.post('/edit', ingredients.editIngredientsPOST); | ||
| app.post('/outOfStock', ingredients.outOfStockPOST) | ||
| app.get('/order', order.orderRender); | ||
| app.post('/orderSubmit', order.orderSubmit); | ||
| app.get('/kitchen',kitchen.kitchenRender); | ||
| app.post('/kitchenButton', kitchen.orderComplete) | ||
|
|
||
|
|
||
| app.listen(PORT, function() { | ||
| console.log("Application running on port:", PORT); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| var mongoose = require('mongoose'); | ||
|
|
||
| var ingredientSchema = mongoose.Schema({ | ||
| name: String, | ||
| price: Number, | ||
| available: String | ||
| }); | ||
| var Ingredient = mongoose.model('Ingredient', ingredientSchema); | ||
|
|
||
| module.exports = Ingredient; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| var mongoose = require('mongoose'); | ||
| var orderSchema = mongoose.Schema({ | ||
| ingredients: Array | ||
| }); | ||
|
|
||
| var Order = mongoose.model('Order', orderSchema); | ||
| module.exports = Order; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "name": "hw", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "engines": { | ||
| "node": "0.10.x" | ||
| }, | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "body-parser": "^1.11.0", | ||
| "express": "^4.11.1", | ||
| "express-handlebars": "^1.1.0", | ||
| "mongoose": "^3.8.22", | ||
| "morgan": "^1.5.1" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| var $form = $("#ajax-form1"); | ||
|
|
||
| var onSuccess = function(data, status){ | ||
| console.log('success'); | ||
| console.log(data); | ||
| var out = "<form id= 'ajax-form1' action='outOfStock' method= 'POST'>" + | ||
| "<input type='text' name ='id' value ='"+data._id+"' readonly/><br/>"+ | ||
| "<input type='text' name='name' value='"+data.name+"'/><br/>" + | ||
| "<input type='text' name='price' value='"+data.price+"'/><br/>"+ | ||
| "<input id ='edit' type ='submit' value='Edit' formaction = 'edit'>"+ | ||
| "<input id ='outOfStock' type ='submit' value='Out of Stock'>" + | ||
| "</form>"; | ||
| console.log(out) | ||
| var id = data._id; | ||
| $("#"+id).html(out); | ||
| }; | ||
|
|
||
| var onError = function(data, status){ | ||
| console.log("status",status); | ||
| console.log("error",data); | ||
| }; | ||
|
|
||
| var onSuccess2 = function(data, status){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. name this more relevant to what it does. |
||
| console.log(data); | ||
| var id = data._id; | ||
| $("#"+id).remove(); | ||
| }; | ||
|
|
||
| $('#edit').click(function(event){ | ||
| event.preventDefault(); | ||
| var name = $form.find("[name='name']").val(); | ||
| var price = $form.find("[name='price']").val(); | ||
| var id = $form.find("[name='id']").val(); | ||
| $.post("edit", { | ||
| _id: id, | ||
| name: name, | ||
| price: price, | ||
| available: "In Stock" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Editing the name makes it "In Stock" again? |
||
| }) | ||
| .done(onSuccess) | ||
| .error(onError); | ||
| }); | ||
|
|
||
| $('#outOfStock').click(function foo(event){ | ||
| event.preventDefault(); | ||
| var id = $form.find("[name ='id']").val(); | ||
| $.post("outOfStock",{ | ||
| _id: id, | ||
| available: "Out of Stock" | ||
| }) | ||
| .done(onSuccess2) | ||
| .error(onError); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| $("[type='button']").click(function(event){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would give these buttons a class to target rather than finding any element with |
||
| console.log($(this)); | ||
| var orderID = $(this).attr("id"); | ||
| $.post("kitchenButton",{ | ||
| order: orderID | ||
| }).done(onSuccess).error(onError); | ||
| }); | ||
|
|
||
| var onError = function(data, status){ | ||
| console.log(data); | ||
| console.log(status); | ||
| }; | ||
|
|
||
| var onSuccess = function(data,status){ | ||
| var divId = data | ||
| $("#"+divId).remove(); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| var $form1 = $("#addForm"); | ||
|
|
||
| var onSuccess1 = function(data, status){ | ||
| console.log('main success'); | ||
| console.log(data); | ||
| var out = "<form id= 'ajax-form1' action='outOfStock' method= 'POST'>" + | ||
| "<input type='text' name='id' value = "+data._id+" readonly/><br/>"+ | ||
| "<input type='text' name='name' value='"+data.name+"'/><br/>" + | ||
| "<input type='text' name='price' value='"+data.price+"'/><br/>"+ | ||
| "<input type ='submit' value='Edit' formaction = 'edit'>"+ | ||
| "<input type ='submit' value='Out of Stock'>" + | ||
| "</form>"; | ||
| $("#result").html(out); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You probably want to append new ingredients rather than setting the contents of a div to it. |
||
| }; | ||
|
|
||
| var onError = function(data, status){ | ||
| console.log("status",status); | ||
| console.log("error",data); | ||
| }; | ||
|
|
||
| $form1.submit(function(event){ | ||
| event.preventDefault(); | ||
| var name = $form1.find("[name='name']").val(); | ||
| var price = $form1.find("[name='price']").val(); | ||
|
|
||
| $.post("newIngredient", { | ||
| name: name, | ||
| price: price, | ||
| available: "In Stock" | ||
| }) | ||
| .done(onSuccess1) | ||
| .error(onError); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| var $form = $("#orderForm"); | ||
|
|
||
| $("[type='checkbox']").click(function foo(pass){ | ||
| var cost = $("#cost").text(); | ||
| console.log("cost", cost); | ||
| var name = $( this ).attr("value"); | ||
| var price = $("#"+name).text(); | ||
| console.log("name", name); | ||
| if (this.checked){ | ||
| console.log('Was Checked'); | ||
| var priceInt = parseInt(price); | ||
| if (priceInt <= 0){ | ||
| priceInt = 0; | ||
| }; | ||
| var total = priceInt + parseInt(cost); | ||
| var costString = total.toString(); | ||
| $("#cost").html("<h3>" + costString+ "</h3>"); | ||
| } | ||
| else{ | ||
| console.log("price",price); | ||
| var priceInt = parseInt(price); | ||
| if (priceInt <= 0){ | ||
| priceInt = 0; | ||
| }; | ||
| var total = parseInt(cost) - priceInt; | ||
| var costString = total.toString(); | ||
| $("#cost").html("<h3>" + costString+ "</h3>"); | ||
| }; | ||
| }); | ||
|
|
||
| var onError = function(data, status){ | ||
| console.log("status",status); | ||
| console.log("error",data); | ||
| }; | ||
|
|
||
| var onSuccess = function(data, status){ | ||
| console.log(data); | ||
| var out = "<h3>Order Submitted!!</h3>"; | ||
| $("#result").html(out); | ||
| }; | ||
|
|
||
| $form.submit(function(event){ | ||
| event.preventDefault(); | ||
| // var values = $("[type='checkbox']:checked").map(function() { | ||
| // return this.val(); | ||
| // }); | ||
| var elems = document.querySelectorAll('select option:checked'); | ||
| var values = [].map.call(elems, function(obj) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting way of doing this. jQuery would probably be easier. |
||
| return obj.value; | ||
| }); | ||
| console.log("shit", values); | ||
| $.post("orderSubmit", { | ||
| ingredients: values | ||
| }) | ||
| .done(onSuccess) | ||
| .error(onError); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| var $form = $("#ajax-form2"); | ||
|
|
||
| var onSuccess = function(data, status){ | ||
| console.log('success'); | ||
| console.log(data); | ||
| var out = ""; | ||
| var id = data._id; | ||
| $("#"+id).remove(); | ||
| }; | ||
|
|
||
| var onError = function(data, status){ | ||
| console.log("status",status); | ||
| console.log("error",data); | ||
| }; | ||
|
|
||
| $form.submit(function(event){ | ||
| event.preventDefault(); | ||
| var name = $form.find("[name='name']").val(); | ||
| var price = $form.find("[name='price']").val(); | ||
| var id = $form.find("[name='id']").val(); | ||
| $.post("edit", { | ||
| _id: id, | ||
| name: name, | ||
| price: price, | ||
| available: "In Stock" | ||
| }) | ||
| .done(onSuccess) | ||
| .error(onError); | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| var routes = {}; | ||
| // var mongoose = require('mongoose'); | ||
| var Ingredient = require('../models/ingredientModel') | ||
| // var ingredientSchema = mongoose.Schema({ | ||
| // name: String, | ||
| // price: Number, | ||
| // available: String | ||
| // }); | ||
| // var Ingredient = mongoose.model('Ingredient', ingredientSchema); | ||
|
|
||
| routes.ingredientsRender = function(req, res){ | ||
| Ingredient.find({available: 'In Stock'},function(err, ingredients){ | ||
| res.render('ingredients', {'ingredients': ingredients}); | ||
| console.log(ingredients); | ||
| }); | ||
| }; | ||
|
|
||
| routes.addIngredients = function(req,res){ | ||
| res.render('home') | ||
| }; | ||
|
|
||
| routes.addIngredientsPOST = function(req, res){ | ||
| console.log('add') | ||
| console.log(req.body); | ||
| res.send(makeIngredient(req.body)); | ||
| }; | ||
|
|
||
| var makeIngredient = function(params){ | ||
| var ingredient = new Ingredient({name: params.name, | ||
| price: params.price, | ||
| available: params.available | ||
| }); | ||
| ingredient.save(function(err){ | ||
| if (err){ | ||
| console.log("Problem adding Ingredient", err); | ||
| }; | ||
| }); | ||
| return ingredient; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return inside the callback. |
||
| }; | ||
|
|
||
| routes.editIngredientsPOST = function(req, res){ | ||
| console.log(req.body); | ||
| Ingredient.findOne({_id: req.body._id}, function(err, doc){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it should work fine. Ingredient.update would as well. |
||
| console.log(doc); | ||
| doc.name = req.body.name; | ||
| doc.price = req.body.price; | ||
| doc.save(); | ||
| res.send(doc); | ||
| }); | ||
|
|
||
| }; | ||
|
|
||
|
|
||
| routes.outOfStockPOST = function(req, res){ | ||
| console.log(req.body); | ||
| Ingredient.findOne({_id: req.body._id}, function(err, doc){ | ||
| console.log(doc); | ||
| if (doc===null){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| Ingredient.remove({id:req.body._id},function(err,doc2){ | ||
| console.log(doc2); | ||
| res.end("Error bad Doc"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the ingredient doesn't exist, try to delete it, then tell the client there was an error? This seems strange. |
||
| }); | ||
| res.end(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. respond after successful removal. |
||
| } | ||
| else{ | ||
| doc.available = ""; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would probably use a boolean for this instead of string vs. empty string. |
||
| doc.save(); | ||
| res.send(doc); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| module.exports = routes; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| var routes = {}; | ||
| var Order = require('../models/orderModel'); | ||
|
|
||
| routes.kitchenRender = function(req,res){ | ||
| Order.find({},function(err, orders){ | ||
| if (err){ | ||
| console.log(err); | ||
| }; | ||
| res.render('kitchen', {order: orders}) | ||
| console.log('orders',orders) | ||
| }); | ||
| }; | ||
|
|
||
| routes.orderComplete = function(req,res){ | ||
| console.log('id', req.body.order) | ||
| Order.findOneAndRemove({ _id: req.body.order }, function(){ | ||
| console.log('Done'); | ||
| res.send(req.body.order); | ||
| }); | ||
| }; | ||
|
|
||
| module.exports = routes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should probably be named differently.