-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
272 lines (226 loc) · 7.4 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
require('dotenv').config();
const express = require('express');
const app = express();
const morgan = require('morgan');
const bodyParser = require('body-parser');
const passport = require('passport');
const session = require('express-session');
const handlers = require('./handlers');
const rss = require('./rss.js');
const fbAuth = require('./config/facebook_passport');
// const socket = require('socket.io');
const path = require('path');
const momentTimeZone = require('moment-timezone');
const moment = require('moment');
const router = new express.Router();
require('./dbConnect');
require('./config/passport')(passport);
// Models
const Event = require('../server/models/Event');
const Appointment = require('../server/models/Appointment');
const Message = require('./models/Message');
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// auth
app.use(session({
secret: process.env.SESS_SECRET,
resave: true,
saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
// todo move to router page
app.use('/', express.static('client/'));
app.use('/node_modules', express.static('node_modules/'));
app.use('/client/index.js', express.static('client/index.js'));
app.use('/client/app.module.js', express.static('client/app.module.js'));
app.use('/client/app.routes.js', express.static('client/app.routes.js'));
app.use('/client/login-button.component.js', express.static('client/login-button.component.js'));
app.use('/client/add-event-button.component.js', express.static('client/add-event-button.component.js'));
app.use('/client/login.component.js', express.static('client/login.component.js'));
app.use('/client/event.service.js', express.static('client/event.service.js'));
app.use('/client/event-form.component.js', express.static('client/event-form.component.js'));
app.use('/client/app.component.js', express.static('client/app.component.js'));
app.use('/client/app-layout.component.js', express.static('client/app-layout.component.js'));
app.use('/client/eview.component.js', express.static('client/eview.component.js'));
app.use('/client/evind.service.js', express.static('client/evind.service.js'));
app.use('/client/rxjs-operators.js', express.static('client/rxjs-operators.js'));
app.use('/client/datatypes/event.js', express.static('client/datatypes/event.js'));
// Fuzzy Lobster Added component routes
app.use('/client/profile.component.js', express.static('client/profile.component.js'));
app.use('/client/profile-button.component.js', express.static('client/profile-button.component.js'));
// facebook auth
app.get('/login/facebook',
passport.authenticate('facebook'));
app.route('/auth/facebook/callback')
.get(passport.authenticate('facebook', fbAuth.config));
// post events to page
app.route('/api/events')
.get(handlers.getEvents)
.post(handlers.postEvent);
// .post(handlers.isAuthenticated, handlers.postEvent);
app.get('/profileInfo', (req, res) => {
res.send(req.user);
});
const port = process.env.PORT || 4657;
//these lines will parse the information out of the file where we load the rss fead
const feed = 'http://www.bestofneworleans.com/gambit/Rss.xml?section=1222783';
rss.requestRSS(feed);
// rss.request();
// Get time zones
const getTimeZones = () => {
return momentTimeZone.tz.names();
};
// Appointments
// GET: /appointments
router.get('/', function (req, res, next) {
Appointment.find()
.then(function (appointments) {
res.render('appointments/index', { appointments: appointments });
});
});
// GET: /appointments/create
router.get('/create', function (req, res, next) {
res.render('appointments/create', {
timeZones: getTimeZones(),
appointment: new Appointment({
name: '',
phoneNumber: '',
notification: '',
timeZone: '',
time: ''
})
});
});
// POST: /appointments
router.post('/', function (req, res, next) {
const name = req.body.name;
const phoneNumber = req.body.phoneNumber;
const notification = req.body.notification;
const timeZone = req.body.timeZone;
const time = moment(req.body.time, 'MM-DD-YYYY hh:mma');
const appointment = new Appointment({
name: name,
phoneNumber: phoneNumber,
notification: notification,
timeZone: timeZone,
time: time
});
appointment.save()
.then(function () {
res.redirect('/');
});
});
// GET: /appointments/:id/edit
router.get('/:id/edit', function (req, res, next) {
const id = req.params.id;
Appointment.findOne({ _id: id })
.then(function (appointment) {
res.render('appointments/edit', {
timeZones: getTimeZones(),
appointment: appointment
});
});
});
// POST: /appointments/:id/edit
router.post('/:id/edit', function (req, res, next) {
const id = req.params.id;
const name = req.body.name;
const phoneNumber = req.body.phoneNumber;
const notification = req.body.notification;
const timeZone = req.body.timeZone;
const time = moment(req.body.time, 'MM-DD-YYYY hh:mma');
Appointment.findOne({ _id: id })
.then(function (appointment) {
appointment.name = name;
appointment.phoneNumber = phoneNumber;
appointment.notification = notification;
appointment.timeZone = timeZone;
appointment.time = time;
appointment.save()
.then(function () {
res.redirect('/');
});
});
});
// POST: /appointments/:id/delete
router.post('/:id/delete', function (req, res, next) {
const id = req.params.id;
Appointment.remove({ _id: id })
.then(function () {
res.redirect('/');
});
});
// Get all events
app.get('/events', (req, res) => {
Event.find((err, event) => {
if (err) {
console.error(err);
return res.send(err);
} else {
res.json(event);
}
});
});
// Get events by id
app.get('/events/:id', (req, res) => {
Event.findById(req.params.id, (err, event) => {
if (err) {
console.error(err);
return res.send(err);
} else {
res.json(event);
}
});
});
//add user to event
app.post('/adduser', handlers.addToAttending);
// Add to message board
app.post('/messages', handlers.addMessage);
app.get('/messages', handlers.getMessages);
// Catch all other routes and return the index file
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../client/index.html'));
});
const server = app.listen(port, () => {
console.log(`Listening on port: ${port}`);
});
// Socket (Eventually...)
// const io = socket(server);
// io.on('connection', (socket) => {
// console.log('made socket connection');
// console.log('socket', socket.id);
// socket.on('open', (data) => {
// Message.find({ event: data.event }, (err, messages) => {
// if (err) {
// console.log(err);
// } else {
// messages.forEach((message) => {
// const messageToSend = {
// handle: message.handle,
// message: message.message,
// event: message.event,
// };
// socket.emit('chat', messageToSend);
// });
// }
// });
// });
// socket.on('chat', (data) => {
// Message.create({
// handle: data.handle,
// message: data.message,
// event: data.event
// }, (err, message) => {
// if (err) {
// console.log(err);
// } else {
// io.sockets.emit('chat', data);
// }
// });
// });
// socket.on('typing', (data) => {
// socket.broadcast.emit('typing', data);
// });
// });