-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
164 lines (139 loc) · 3.62 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//jshint esversion:6
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/pearlDB", {
useNewUrlParser: true,
useUnifiedTopology: true
});
const itemsSchema = mongoose.Schema({
name: String,
quantity: Number
});
const Item = mongoose.model("Item", itemsSchema);
const Order = mongoose.model("Order", itemsSchema);
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
app.get("/", function(req, res) {
res.render("home", {
headingTitle: "Welcome to Pearl Restaurant!",
headingPara: "A Crazy place for foodies - Heart of Perfect Food Hangout!"
});
});
app.get("/login", function(req, res) {
res.render("login");
});
app.get("/contact", function(req, res) {
res.render("contact", {
headingTitle: "Come talk to us!",
headingPara: "We always value your words."
});
});
app.get("/about", function(req, res) {
res.render("about", {
headingTitle: "Our story",
headingPara: "About Pearl Restaurant"
});
});
app.get("/cart", function(req, res) {
Item.find({}, function(err, foundItems) {
res.render("cart", {
headingTitle: "Here is your Cart!",
headingPara: "You're just one click away from fulfilling your craving.",
listItems: foundItems
});
});
});
app.get("/place", function(req, res) {
res.render("place", {
headingTitle: "Choose a payment option.",
headingPara: ""
});
});
app.post("/add", function(req, res) {
var quantity = req.body.quantity;
var name = req.body.food;
Item.findOne({
name: name
}, function(err, foundItem) {
if (!err) {
if (!foundItem) {
const newItem = new Item({
name: name,
quantity: quantity
});
newItem.save();
} else {
foundItem.quantity = quantity;
foundItem.save();
}
}
});
});
app.post("/remove", function(req, res) {
var item = req.body.removeItem;
Item.findOneAndDelete({
name: item
}, function(err, deletedItem) {
if (!err) {
console.log("Deleted Item: " + deletedItem);
}
});
res.redirect("/cart");
});
app.post("/ordered", function(req, res) {
Item.find({}, function(err, foundItems) {
Order.insertMany(foundItems, function(err) {
console.log("Orders Created");
});
});
Item.deleteMany({}, function(err) {
if(!err){
console.log("Cart emptied");
}
});
var name = req.body.username;
res.render("ordered", {
name: name,
orderID: Math.floor((Math.random() * 100000) + 1)
});
});
app.post("/admin", function(req, res) {
const username = req.body.user;
const password = req.body.pass;
if(username === "admin" && password === "admin123"){
Order.find({}, function(err, foundItems) {
res.render("admin", {
headingTitle: "Welcome, Admin!",
headingPara: "Here are all the orders placed.",
listItems: foundItems
});
});
}
else {
res.redirect("login");
}
});
app.post("/deliver", function(req, res) {
var item = req.body.removeItem;
Order.findByIdAndDelete(item, function(err, deletedItem) {
if (!err) {
console.log("Deleted Item: " + deletedItem);
Order.find({}, function(err, foundItems) {
res.render("admin", {
headingTitle: "Welcome, Admin!",
headingPara: "Here are all the orders placed.",
listItems: foundItems
});
});
}
});
});
app.listen(process.env.PORT || 3000, function() {
console.log("Server started on port 3000.");
});