-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
203 lines (166 loc) · 5.31 KB
/
index.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
const express = require('express');
const app = express();
const path = require('path');
const cookieSession = require('cookie-session');
const flash = require('connect-flash');
var session = require('express-session')
const mongoose = require('mongoose');
const users = require('./models/user');
const appointments = require('./models/appointment');
const salons = require('./models/salon');
const cookieParser = require("cookie-parser");
mongoose.connect('mongodb://localhost:27017/salondb')
.then(() => {
console.log("Database connected");
})
.catch(err => {
console.log("OH NO ERROR!!!");
console.log(err);
})
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true
}))
app.use(flash());
app.use(cookieParser());
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', async (req, res) => {
res.clearCookie("appointmentTime");
res.clearCookie("appointmentDate");
let sls = await salons.find({});
let myapps = await appointments.find({ username: req.session.email });
let currDate = new Date();
let validApps = [];
for (let apps of myapps) {
let appDate = stringToDate(apps.date, apps.time);
if (appDate >= currDate) {
validApps.push(apps);
}
}
let loggedIn = false;
if (req.session.email) {
loggedIn = true;
}
res.render("index.ejs", { sls, loggedIn, validApps, message: req.flash('info') });
// res.sendFile(path.join(__dirname, "index.html"));
})
app.get('/contributors', async (req, res) => {
res.render('contributors.ejs');
})
app.post('/login', async (req, res) => {
const { email } = req.body;
const { password } = req.body;
const isExisting = await users.findOne({ email: email });
if (!isExisting) {
// flash the message says no user with this username
req.flash('info', "Invalid Credentials");
res.redirect('/');
}
else if (isExisting.password == password) {
req.session.email = email;
// document.cookie = "email=" + email + ";";
req.flash('info', `Hey, welcome back ${isExisting.name}`);
res.redirect('/')
}
else {
req.flash('info', "Invalid Credentials");
res.redirect('/');
}
});
app.post('/signin', async (req, res) => {
const { username } = req.body;
const { email } = req.body;
const { password } = req.body;
const isExisting = await users.findOne({ email: email });
console.log(isExisting);
if (isExisting) {
req.flash('info', "Account already signedup with email");
}
else {
const user = new users({
name: username,
email: email,
password: password
})
// flash the message says new entry created
req.flash('info', "Successfully signed Up");
await user.save();
console.log("new entry created");
}
res.redirect('/');
})
app.post('/logout', async (req, res) => {
req.session.email = null;
res.redirect('/');
})
app.get('/home', async (req, res) => {
res.redirect('/');
})
app.post('/appointment', async (req, res) => {
let { totalCost, appointmentTime, appointmentDate, services, salonName } = req.cookies;
services = services.split(",");
const isExisting = await appointments.findOne({ salon: salonName, time: appointmentTime, date: appointmentDate });
if (isExisting) {
// flash, sorry time in not available for this salon
req.flash('info', "Sorry, choosen time is already booked");
console.log("appointment already existed");
}
else {
const appoin = new appointments({
username: req.session.email,
totalCost: totalCost,
time: appointmentTime,
date: appointmentDate,
services: services,
salon: salonName
});
await appoin.save();
req.flash('info', "Appointment Booked");
}
res.redirect("/");
});
app.post('/delete', async (req, res) => {
let id = req.body.id;
let app = await appointments.findOneAndDelete({ _id: id });
req.flash("info", `Appoinment at ${app.salon} on ${app.date}, ${app.time} is cancelled.`);
res.redirect('/');
})
app.post('/checkTime', async (req, res) => {
let { salonName, t, d } = req.body;
let isExist = await appointments.find({ salon: salonName, time: t, date: d });
if (isEmpty(isExist)) {
res.send(true);
}
else {
res.send(false);
}
})
app.listen(3000, () => {
console.log("STARTED");
})
// Auxilary functions
function getTodayDate() {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = dd + '-' + mm + '-' + yyyy;
return today;
}
function stringToDate(str, time) {
let arr = str.split('-');
let arr_time = time.split(":");
let t = arr_time[0];
if (time.slice(-2) == "PM" && t != 12) {
t += 12;
}
let newDate = new Date(parseInt(arr[2]), parseInt(arr[1]) - 1, parseInt(arr[0]), t);
return newDate;
}
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}