-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
210 lines (203 loc) · 6.75 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const express = require('express');
const app = express();
const bodyparser = require('body-parser');
const mongoose = require('mongoose');
const ejs = require('ejs');
const Entities = require('html-entities').AllHtmlEntities;
const entities = new Entities();
app.set('view engine', 'ejs');
app.use(express.static("public"));
app.use(bodyparser.json());//parsing the application/json
app.use(bodyparser.urlencoded({ extended: true }));//parses the x-www-form-urlencoded
Material = require('./models/material');
Suppliers = require('./models/supplier');
mongoose.connect('mongodb+srv://admin-jeel:Jul@2018@todolistdatabase.d7cky.mongodb.net/inventory');
var db = mongoose.connection;
function today(){
var date = new Date();
var todaydate = date.toDateString();
var minutes = date.getUTCMinutes();
var hours = date.getHours();
var seconds = date.getSeconds();
var format = hours+':'+minutes+':'+seconds+' '+todaydate;
return format ;
};
function sanitizer(string){
string = entities.encode(string);
return string;
}
//getting to the index
app.get('/',(req ,res)=>{
res.render('index');
});
//getting addmaterial page
app.get('/addmaterial' , (req ,res)=>{
res.render('addmaterial');
});
//getting editmaterial page
app.get('/editmaterial/:id',(req, res)=>{
var id = req.params.id;
res.render('editmaterial',{ id:id });
});
//getting materials
app.get('/materials',(req , res)=>{
Material.getMaterials((err , materials)=>{
if(err){
res.render('material' , {
msg:'Error in getting details..'
});
}
else{
var obj = materials;
res.render('material', { obj:obj } );
}
})
});
// add the materials
app.post('/addmaterial',(req, res)=>{
var name = entities.encode(req.body.name);
var price = entities.encode(req.body.price);
var qty = entities.encode(req.body.qty);
var state = entities.encode(req.body.state);
var on = today();
Material.addMaterial({
name:name,price:price,qty:qty,state:state,created_on:on
} , (err , material)=>{
if(err)
{ res.render('addmaterial', {
msg:'Please fill required details!'
})}
var obj = material;
// res.render('addmaterial' , {
// msg:'successfully submit!!'
//});
res.redirect('/materials');
});
});
// update the material data
app.post('/editmaterial/:id',(req ,res)=>{
var id = req.params.id;
var name = entities.encode(req.body.name);
var price = entities.encode(req.body.price);
var qty = entities.encode(req.body.qty);
var state = entities.encode(req.body.state);
//for creating date
var on = today();
Material.updateMaterial(id , {
name:name,price:price,qty:qty,state:state,created_on:on
},{}, (err , callback)=>{
if(err){
res.render('editmaterial', {msg:"Error Occured."});
}
// res.render('editmaterial' ,{ id:id ,msg:"Successfully updated!!"} );
res.redirect('/materials');
});
});
//delete the data of materials
app.post('/deletematerial/:_id',(req , res)=>{
var id = req.params._id;
Material.removeMaterial(id , (err ,callback)=>{
if(err){throw err}
res.redirect('/materials');
} );
});
app.post('/showmaterials/:id',(req ,res)=>{
var id = req.params.id;
Material.findbyid(id , (err , materials)=>{
if(err){throw err;}
var obj = materials;
res.render('showmaterials', {obj:obj});
});
});
//--------------------------------------------------
app.get('/addsupplier',(req,res)=>{
res.render('addsupplier');
});
app.get('/editsupplier/:id',(req,res)=>{
var id = req.params.id;
res.render('editsupplier', {id:id});
});
// finds the suppliers
app.get('/suppliers',(req, res)=>{
Suppliers.getSuppliers((err , suppliers)=>{
if(err)
{
res.render('supplier' , {
msg:'some error occured!!'
});
}
var obj = suppliers;
res.render('supplier' , {obj:obj});
});
});
app.post('/addsupplier' , (req ,res)=>{
var cmpname = sanitizer(req.body.cmpname);
var materialname = sanitizer(req.body.materialname);
var state = sanitizer(req.body.state);
var emailid = sanitizer(req.body.emailid);
var contactno = sanitizer(req.body.contactno);
var address = sanitizer(req.body.address);
var costprice = sanitizer(req.body.costprice);
var qty = sanitizer(req.body.qty);
var on = today();
var supplier = {cmpname:cmpname, materialname:materialname, state:state, emailid:emailid,
contactno:contactno, address:address, costprice:costprice, qty:qty ,created_on:on };
Suppliers.addSupplier(supplier , (err , supplier)=>{
if(err){throw err}
res.redirect('/suppliers');
})
});
// //add suppliers
// app.post('/addsupplier',(req ,res)=>{
// var supplier = req.body;
// Suppliers.addSupplier(supplier , (err, supplier)=>{
// if(err){
// throw err;
// }
// res.json(supplier);
// });
// });
//update the supplier data
app.get('/showsupplier/:id',(req , res)=>{
var id = req.params.id;
Suppliers.getSuppliersById(id , (err , supplier)=>{
if(err){throw err}
var obj = supplier;
res.render('showsupplier', {obj:obj});
});
});
app.post('/editsupplier/:_id',(req ,res)=>{
var id = req.params._id;
var cmpname = sanitizer(req.body.cmpname);
var materialname = sanitizer(req.body.materialname);
var state = sanitizer(req.body.state);
var emailid = sanitizer(req.body.emailid);
var contactno = sanitizer(req.body.contactno);
var address = sanitizer(req.body.address);
var costprice = sanitizer(req.body.costprice);
var qty = sanitizer(req.body.qty);
var on = today();
var supplier = {cmpname:cmpname, materialname:materialname, state:state, emailid:emailid,
contactno:contactno, address:address, costprice:costprice, qty:qty ,created_on:on };
Suppliers.updateSupplier(id , supplier ,{},(err , supplier)=>{
if(err){
throw err
}
res.redirect('/suppliers');
});
} );
//delete the data from supplier ...
app.post('/deletesupplier/:_id',(req,res)=>{
var id = req.params._id;
Suppliers.removeSupplier(id, (err , callback)=>{
if(err){throw err}
res.redirect('/suppliers');
});
});
let port = process.env.PORT;
if (port == null || port ==""){
port = 8080;
}
app.listen(port, ()=>{
console.log('Server started on port 8080 successfully');
});