-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
156 lines (135 loc) · 4.98 KB
/
app.js
File metadata and controls
156 lines (135 loc) · 4.98 KB
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
const express = require('express')
const mongo = require('./db_connection/mongo')
const bodyParser = require('body-parser')
const trainlist = require('./utils/trainlist')
// const { response } = require('express')
const port = process.env.PORT || 3000
const app = express()
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
app.post('/register',(req,res)=>{
var response_text = ''
if(getAge(req.body.dob)>=15){
mongo.fetch_user(req.body.email,(err,body)=>{
if(err){
response_text='Something went wrong. Please try again later'
res.status(502).send(response_text)
}
else{
if(body==null){
mongo.add_user(req.body,(err,result)=>{
if(err){
response_text='Something went wrong. Please try again later'
res.status(502).send(response_text)
}
else{
response_text='Register_Success'
res.status(202).send(response_text)
}
})
}
else{
response_text = 'Email already exists. Please enter a Different Email'
res.status(502).send(response_text)
}
}
})
}
else{
console.log('NOT eligible')
res.send('Not Eligible')
}
// res.send(response_text)
//Check if user already exists
})
app.post('/login',(req,res)=>{
var response_text = ''
mongo.fetch_user(req.body.email,(err,data)=>{ //user authentication
if(err){
console.log('Error while fetching user :'+err)
response_text = 'Something went wrong. Please try again later'
res.status(502).send(response_text)
}
else{
if(data==null){ //Email does't exist
response_text = 'Incorrect Email or Password'
res.status(400).send(response_text)
}
else{ //Email exists and checking is password matches
if(req.body.password == data.password){
response_text = 'Loggin_Success'
res.status(202).send(response_text)
}
else{
response_text = 'Incorrect Email or Password'
res.status(400).send(response_text)
}
}
}
})
// res.send(response_text)
})
app.post('/gettrainlist',function await(req,res){
console.log(req.headers)
const source = req.body.source
const destination = req.body.destination
//return list of trains between given two stations
// console.log(trainlist.getTainList(source,destination))
trainlist.getTrainList(source,destination,(err,result)=>{
if(err){
console.log('Error while fetching train list'+err)
res.status(500).send('Error while fetching train details'+err)
}
else{
res.status(202).send(result)
}
})
})
app.post('/buyticket',({body},res)=>{
mongo.add_tickets(body,(err,pass)=>{
if(err){
res.status(500).send("Server Error")
}
else{
res.status(202).send('Transaction Successfull')
}
})
})
app.post('/getticket',({query},res)=>{
mongo.fetch_tickets(query.email,(err,tickets)=>{
if(error){
res.status(502).send('Error while fetching Tickets')
}
else{
res.status(202).send(tickets)
}
})
})
app.post('/recharge',({body},res)=>{
mongo.recharge(body,(err,data)=>{
if(err){
console.log(err)
res.status(500).send('Recharge unsuccessfull')
}
else{
console.log('recharge success')
res.status(202).send('Recharge successfull')
}
})
})
// app.post('/authenticate',(req,res)=>{
// })
// app.post()
app.listen(port,()=>{
console.log('server up and running on port '+ port)
})