-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
112 lines (101 loc) · 3.34 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
server = require('http').Server(app),
io = require('socket.io')(server);
const uri = "mongodb://cooking:[email protected]:23268/wsurop_cooking";
mongoose.connect(uri, {useNewUrlParser: true});
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + '/public'));
app.set("view engine", "ejs");
// SCHEMA setup
var Step = require("./models/step"),
Recipe = require("./models/recipe"),
Temperature = require("./models/temp");
// SOCKET setup
io.on('connection', function(socket) {
function getTemp() {
Temperature.findOne({}, {}, {sort: {"_id": -1}}, function(err, data) {
if(err){
console.log(err);
}
else {
socket.emit('newTemp', data.temperature);
}
});
}
setInterval(getTemp, 1000);
});
//ROUTES
app.get("/", function(req, res){
res.render("landing");
});
app.get("/recipes", function(req, res){
Recipe.find({}, function(err, recipes){
if(err){
console.log(err);
} else {
res.render("index", {recipes:recipes});
}
})
});
app.get("/recipes/new", function(req, res){
res.render("new");
});
// SHOW - shows more info about a recipe
app.get("/recipes/:id", function(req,res){
Recipe.findById(req.params.id).populate("instruction").exec(function(err, foundRecipe){
if(err){
console.log(err);
} else {
res.render("show", {recipe: foundRecipe});
}
});
});
// SHOW STEPS - shows more info about a recipe
app.get("/recipe/:id/:stepid", function(req,res){
Recipe.findById(req.params.id).populate("instruction").exec(function(err, foundRecipe){
if(err){
console.log(err);
} else {
res.render("step", {recipe: foundRecipe, stepid: req.params.stepid});
}
});
});
app.post("/recipes", function(req,res){
var name = req.body.name,
image = req.body.image,
servings = req.body.servings,
instruction = req.body.instruction,
cooking_time = req.body.cooking_time,
ingredients = req.body.ingredients,
nof_steps = req.body.steps;
var newRecipe = {name: name, image: image, cooking_time: cooking_time, servings: servings, ingredients: ingredients, instruction: []};
for (var i=0;i<nof_steps;i++){
var tempstep = req.body['step' + i];
var tempimg = req.body['img' + i];
var tempsteptemp = req.body['temperature' + i]; // temporary step object temperature parameter
var tempsteptime = req.body['time' + i];
newRecipe.instruction.push({
stepnumber: i+1,
step: tempstep,
image: tempimg,
time: tempsteptime,
temperature: tempsteptemp
});
};
Recipe.create(newRecipe
, function(err, recipe){
if(err){
console.log(err);
} else {
console.log(recipe);
res.redirect("/recipes");
}
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log("BuddyCook server started listening on port ${ PORT }");
});