-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
154 lines (124 loc) · 3.18 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
require('dotenv').config()
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
const port=process.env.PORT || 4500;
app.set("view engine","ejs");
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("public"));
const mongoURI = process.env.mongoURI
const connection=mongoose.connect(mongoURI,{useNewUrlParser:true});
const itemsSchema={
name:String,
gender:String,
age:String,
password:String,
money:Number,
email:String,
address:String
};
const Itema=new mongoose.model("Itema",itemsSchema);
const item1=new Itema({
"name": "sanjeev",
"gender":"male",
"age":"20",
"password":"sanjeev",
"money":345,
"email": "sks@gmail.com",
"address":"abc defghi"
});
const item2=new Itema({
"name": "sanju",
"gender":"male",
"age":"22",
"password":"sanju",
"money":678,
"email": "ss@gmail.com",
"address":"abcddef"
});
const defaultItems=[item1,item2];
//for adding two default customer data
app.get("/manager",function(req,res)
{
Itema.find({},function(err,foundItems){
if(foundItems.length==0){
Itema.insertMany(defaultItems,function(err){
if(err){
console.log(err);
}else{
console.log("Succesfully saved default items to DB.");
}
});
res.redirect("/manager");
}
else{
res.render("manager",{customer:foundItems});
}
});
});
//for adding new customer data
app.post("/register",function(req,res)
{
const item=new Itema({
name:req.body.uname,
gender:req.body.ugender,
age:req.body.uage,
password:req.body.psw,
money:req.body.umoney,
email:req.body.uemail,
address:req.body.uaddress
});
item.save();
console.log("Successfully added.");
res.redirect("/manager");
});
//for deleting customer data/account
app.post("/delete",function(req,res){
const checkedItemId=req.body.checkbox;
Itema.findByIdAndRemove(checkedItemId,function(err){
if(!err){
console.log("Successfully deleted checked item.");
res.redirect("/manager");
}
});
});
//for login authentication of customer
app.post("/login",async(req,res) =>{
try{
const username=req.body.uname;
const password=req.body.psw;
const detail=await Itema.findOne({name:username});
if(detail.password===password){
res.status(201).render("customer",{customer:detail});
}
else{
res.send("password are not matching");
}
}
catch (error) {
res.status(400).send("invalid email")
}
})
//roughly flow of data :
//home >> login customer >> with authentication >> customer account detail
//home >> login manager >> view customer detail >> can delete or add customer account
app.get("/",function(req,res)
{
res.render("home");
})
app.get("/login",function(req,res)
{
res.render("login");
})
app.get("/register",function(req,res)
{
res.render("register");
})
app.get("/customer",function(req,res)
{
res.render("customer");
})
app.listen(port, function(){
console.log("Server started successfully..");
});