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
39 changes: 39 additions & 0 deletions classes/class4/hw/app.js
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);

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.

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);
});
10 changes: 10 additions & 0 deletions classes/class4/hw/models/ingredientModel.js
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;
7 changes: 7 additions & 0 deletions classes/class4/hw/models/orderModel.js
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;
21 changes: 21 additions & 0 deletions classes/class4/hw/package.json
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"
}
}
53 changes: 53 additions & 0 deletions classes/class4/hw/public/javascripts/edit.js
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){

Choose a reason for hiding this comment

The 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"

Choose a reason for hiding this comment

The 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);
});
17 changes: 17 additions & 0 deletions classes/class4/hw/public/javascripts/kitchen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$("[type='button']").click(function(event){

Choose a reason for hiding this comment

The 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 type='button'

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();
};
33 changes: 33 additions & 0 deletions classes/class4/hw/public/javascripts/main.js
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);

Choose a reason for hiding this comment

The 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);
});
58 changes: 58 additions & 0 deletions classes/class4/hw/public/javascripts/order.js
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) {

Choose a reason for hiding this comment

The 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);
});

29 changes: 29 additions & 0 deletions classes/class4/hw/public/javascripts/outOfStock.js
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);
})
73 changes: 73 additions & 0 deletions classes/class4/hw/routes/ingredients.js
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;

Choose a reason for hiding this comment

The 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){

Choose a reason for hiding this comment

The 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){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (doc){

Ingredient.remove({id:req.body._id},function(err,doc2){
console.log(doc2);
res.end("Error bad Doc");

Choose a reason for hiding this comment

The 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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

respond after successful removal.

}
else{
doc.available = "";

Choose a reason for hiding this comment

The 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;
22 changes: 22 additions & 0 deletions classes/class4/hw/routes/kitchen.js
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;
Loading