-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
2018 lines (1701 loc) · 63.2 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable max-len */
/* eslint-disable linebreak-style */
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require('express-session');
var mysql = require('mysql');
// var bodyParser = require('body-parser');
var fs = require("fs");
var dbConnectionPool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydatabase'
});
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// app.use(bodyParser.json());
app.use(function (req, res, next) {
req.pool = dbConnectionPool;
next();
});
app.use(session({
resave: false,
saveUninitialized: true,
secret: 'super secret string',
secure: false
}));
// // optional but good for keeping track
// app.use(function (req, res, next) {
// console.log("The current user is: " + req.session.user.userid);
// next();
// });
const setDefaultUserId = (req, res, next) => {
if (!req.session.user) {
req.session.user = {};
}
if (!req.session.user.userid) {
req.session.user.userid = 0;
}
next();
};
app.use(setDefaultUserId);
app.use('/', indexRouter);
app.use('/users', usersRouter);
// ************ SAL'S ROUTES***************** //
// DISPLAY ALL CLUBS
// client receives clubname, clubid
app.get('/clubs', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.log("Error for getting clubs:", cerr);
//res.status(500).send("cerr for get /clubs");
return;
}
let query = `SELECT clubname, clubid FROM club;`;
connection.query(query, function (qerr, rows, fields) {
connection.release();
if (qerr) {
console.log("Error for getting clubs:", qerr);
return;
}
res.json(rows);
});
});
});
//POST NEW EVENT INTO DATABASE
//****NEEDS USERID INPUT */
app.post('/new-post', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.log("newpost cerr error", cerr);
res.sendStatus(500);
return;
}
let postquery = `INSERT INTO post (
userid,
clubid,
posttitle,
content,
dateandtime,
publicity
)
VALUES (
?,
?,
?,
?,
CURRENT_TIMESTAMP,
?
);`;
// insert user input into queries
// NOT DONE; MISSING CLUB ID
connection.query(postquery, [req.session.user.userid,
req.body.clubid,
req.body.posttitle,
req.body.postcontent,
req.body.publicity], function (qerr, rows, fields) {
connection.release();
if (qerr) {
console.log('postquery qerr', qerr);
res.sendStatus(500);
return;
}
res.end();
});
});
});
//POST NEW EVENT INTO DATABASE
app.post('/new-event', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.log("new-event cerr error", cerr);
res.sendStatus(500);
return;
}
let eventquery = `INSERT INTO events (
clubid,
userid,
EventName,
Content,
EventLocation,
EventDate,
EventTime,
Publicity
)
VALUES (
?,
?,
?,
?,
?,
?,
?,
?
);`;
connection.query(eventquery, [
req.body.clubid,
req.session.user.userid,
req.body.eventname,
req.body.content,
req.body.eventlocation,
req.body.eventdate,
req.body.eventtime,
req.body.publicity
], function (qerr, rows, fields) {
connection.release();
if (qerr) {
console.log('eventQuery qerr', qerr);
res.sendStatus(500);
return;
}
const htmlContent = `
<!DOCTYPE html>
<html lang="eng">
<head>
<!--CSS LINK-->
<link rel="stylesheet" type="text/css" href="stylesheets/event-page.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<!--JS LINK-->
<!-- <script src="scripts/event-page.js" defer></script> -->
<!--Symbols library-->
<script src="https://kit.fontawesome.com/979fc57cca.js" crossorigin="anonymous"></script>
<meta charset="UTF-8">
<title>Web Project - Event Page</title>
</head>
<body>
<header>
<div class="page-title">
<div class="login-div">
<div class="login-symbol"><button class="login-button" type="button"
onclick="window.location.href='log-in.html'">
<i class="fa fa-sign-in" aria-hidden="true"></i>
</button>
<p id="login-text">LOGIN</p>
</div>
</div>
<br>
<h1>Adelaide University Clubs</h1>
</div>
<!---Tab with links to connecting pages-->
<nav class="nav-tab">
<ul>
<li><a href="home.html">HOME</a></li>
<li><a href="about.html" onclick>ABOUT</a></li>
<li><a href="find-clubs.html">CLUBS</a></li>
<li><a href="club-feed.html">YOUR CLUBS</a></li>
<li><a href="profile.html">PROFILE</a></li>
</ul>
</nav>
</header>
<main>
<div class="event-info">
<h3 id="event-title" class="event-title">EVENT TITLE</h3>
<h4>Event Details</h4>
<p>
Time: <span id="time"></span><br>
Date: <span id="date"></span><br>
Location: <span id="location"></span><br>
Additional Info: <span id="additional-info"></span>
</p>
</div>
</main>
<button id ="rsvpButton" class="RSVP-button" type="button">RSVP</button>
// <div id="rsvp-list" class="members-list-container">
// <h4>RSVP'D MEMBERS:</h4>
// <hr>
// <ul>
// <li v-for="member in members" :key="member.userid"> {{ member.firstname }} {{ member.lastname }}</li>
// </ul>
// </div>
<br><br><br><br><br><br><br><br>
<!---Footer -->
<footer>
<!----Dark mode toggle-->
<div class="dark-mode">
<button type="button" onclick="switchColourMode('stylesheets/club-feed-light.css')">Light mode</button>
<button type="button" onclick="switchColourMode('stylesheets/club-feed-dark.css')">Dark mode</button>
</div>
<div class="address">
<div id="north">
<p class="campus">North Terrace Campus</p>
<p>Level 4 Hub Central, University of Adelaide</p>
<p>08 8313 5401</p>
</div>
<div id="waite">
<p class="campus">Waite Campus</p>
<p>The Hub, McLeod Building</p>
<p>08 8313 5401</p>
</div>
<div id="roseworthy">
<p class="campus">Roseworthy Campus</p>
<p>Room GO1, College Hall</p>
<p>08 8313 5401</p>
</div>
</div>
<div class="footer-links">
<a href="/contact-us">Contact Us</a>
<a href="/terms-conditions">Terms and Conditions</a>
</div>
<div class="social-links">
<a href="#"><i class="fab fa-facebook-f"></i></a>
<a href="#"><i class="fab fa-twitter"></i></a>
<a href="#"><i class="fab fa-instagram"></i></a>
</div>
</footer>
<script>
// Function to Eventid
// get the club id
function getEventId() {
var filename = window.location.pathname.split("/").pop();
var eventName = filename.replace(/-/g, ' ').replace(/.html$/, '');
console.log(eventName);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
var response = JSON.parse(this.responseText);
globalEventId = response.EventId;
// call functions that need club id
getEventDetails();
} else {
console.log("Error getting clubid: " + this.status);
}
}
};
var url = "/get-eventid?EventName=" + eventName;
xhttp.open("GET", url, true);
xhttp.send();
}
getEventId();
var globalEventId = null;
// Make an AJAX request to retrieve event details
function getEventDetails() {
var eventId = globalEventId // Dynamically change this
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
var eventData = JSON.parse(this.responseText)[0]; // Assuming only one event is returned
function formatDate(date) {
return new Date(date).toLocaleDateString(undefined, { year: 'numeric', month: 'long', day: 'numeric' });
}
// Update the HTML content with event details
document.getElementById("event-title").textContent = eventData.EventName;
document.getElementById("time").textContent = eventData.EventTime;
document.getElementById("date").textContent = formatDate(eventData.EventDate);
document.getElementById("location").textContent = eventData.EventLocation;
document.getElementById("additional-info").textContent = eventData.Content;
}
};
function sendRSVP() {
var postId = eventId;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
// RSVP successful
console.log('RSVP successful');
} else {
// Error handling
console.log('Error RSVPing: ' + this.status);
}
}
};
var url = '/rsvp?postid=' + postId;
xhttp.open('POST', url, true);
xhttp.setRequestHeader('Content-type', 'application/json');
var data = JSON.stringify({ postid: postId });
xhttp.send(data);
}
// Attach a click event listener to the RSVP button
var rsvpButton = document.getElementById('rsvpButton');
rsvpButton.addEventListener('click', sendRSVP);
function getMembers() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
var members = JSON.parse(this.responseText);
updateRSVPMembers(members);
} else {
console.log("Error fetching member data: " + this.status);
}
}
};
var postId = eventId; // Replace 1 with the actual postid value
var url = "/rsvpdlist?postid=" + postId;
xhttp.open("GET", url, true);
xhttp.send();
}
// Function to update the RSVP'd members
function updateRSVPMembers(members) {
var rsvpList = new Vue({
el: '#rsvp-list',
data: {
members: members,
},
});
}
getMembers();
xhttp.open("GET", "/geteventdetail?EventId=" + eventId, true);
xhttp.send();
}
// Call the function to load event details when the page is loaded
window.onload = getEventDetails;
// Function to get data of club members with AJAX
</script>
</body>
</html>`;
// Generate the file path and name
const filename = req.body.eventname.toLowerCase().replace(/ /g, '-');
const filePath = `public/${filename}.html`;
// Write the HTML content to the file
fs.writeFile(filePath, htmlContent, function (err) {
if (err) {
console.log("Error creating event HTML file:", err);
res.sendStatus(500);
return;
}
console.log(`Club HTML file "${req.body.eventname}.html" created successfully.`);
res.sendStatus(200);
});
});
});
});
app.post('/rsvp', function(req,res,next){
req.pool.getConnection(function(cerr, connection) {
if (cerr) {
console.log("rsvp cerr error",cerr);
res.sendStatus(500);
return;
}
let postquery = `INSERT IGNORE INTO rsvp (userid, postid) VALUES (?, ?);`;
connection.query(postquery,[req.session.user.userid, req.body.postid],function(qerr, rows, fields) {
connection.release();
if (qerr) {
console.log('postquery qerr',qerr);
res.sendStatus(500);
return;
}
res.end();
});
});
});
//DISPLAY RSVP LIST BASED ON RSVP DATA
// return list of members who sign up for event/rsvp
// alphabetical order: Firstname, lastname, email from userid that rsvpd
// need client to send postid
app.get('/rsvpdlist', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.log("cerr for GET /rsvpd");
res.sendStatus(500);
return;
}
let query = `SELECT firstname, lastname, email FROM rsvp
INNER JOIN Users ON rsvp.userid = Users.userid
WHERE postid = ?;`;
connection.query(query, [req.query.postid], function (qerr, rows, fields) {
connection.release();
if (qerr) {
console.log("qerr for GET /rsvpdlist");
res.sendStatus(500);
return;
}
res.json(rows);
});
});
});
app.get('/geteventdetail', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.log("cerr for GET /geteventdetails");
res.sendStatus(500);
return;
}
let query = `SELECT * FROM events WHERE EventId=?;`;
connection.query(query, [req.query.EventId], function (qerr, rows, fields) {
connection.release();
if (qerr) {
console.log("qerr for GET /eventdetails");
res.sendStatus(500);
return;
}
res.json(rows);
});
});
});
//GET ALL POSTS FOR A CLUB
// client send clubid
// if user is NOT a member of club then only public posts; if member is a user then see all post
// client all of post info (check table) + poster's first name last name
app.get('/posts', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.log("cerr for get /posts");
res.sendStatus(500);
return;
}
// check if they are a club member
let userquery = `SELECT * FROM membership WHERE userid = ? AND clubid = ?;`; // user is a manager, //
connection.query(userquery, [req.session.user.userid, req.query.clubid], function (qerr, rows, fields) {
if (qerr) {
console.log("qerr for get /posts", qerr);
res.sendStatus(500);
return;
}
let postquery = ''; // Declare the postquery variable
// if they are not a member, only public posts
if (rows.length <= 0) {
postquery = `SELECT * FROM post
INNER JOIN Users ON post.userid = Users.userid
WHERE clubid = ? AND publicity = 'public';`;
}
// if they are a member, fetch all posts
else {
postquery = `SELECT * FROM post
INNER JOIN Users ON post.userid = Users.userid
WHERE clubid = ?;`;
}
connection.query(postquery, [req.query.clubid], function (qerr2, rows2, fields2) {
connection.release();
if (qerr2) {
console.log("qerr2 for get /posts", qerr2);
res.sendStatus(500);
return;
}
res.json(rows2);
});
});
});
});
//GET ALL EVENTS OF A CLUB
app.get('/events', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.log("cerr for get /events");
res.sendStatus(500);
return;
}
// check if they are a club member
let userquery = `SELECT * FROM membership WHERE userid = ? AND clubid = ?;`; // user is a manager, //
connection.query(userquery, [req.session.user.userid, req.query.clubid], function (qerr, rows, fields) {
if (qerr) {
console.log("qerr for get /events", qerr);
res.sendStatus(500);
return;
}
let postquery = ''; // Declare the postquery variable
// if they are not a member, only public posts
if (rows.length <= 0) {
postquery = `SELECT * FROM events
INNER JOIN Users ON events.userid = Users.userid
WHERE clubid = ? AND publicity = 'public';`;
}
// if they are a member, fetch all posts
else {
postquery = `SELECT * FROM events
INNER JOIN Users ON events.userid = Users.userid
WHERE clubid = ?;`;
}
connection.query(postquery, [req.query.clubid], function (qerr2, rows2, fields2) {
connection.release();
if (qerr2) {
console.log("qerr2 for get /event", qerr2);
res.sendStatus(500);
return;
}
res.json(rows2);
});
});
});
});
// get members events from database
app.get('/members-events', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.log("cerr for GET /members-events", cerr);
res.sendStatus(500);
return;
}
let query = `SELECT e.*, c.clubname
FROM events e
JOIN membership m ON e.clubid = m.clubid
JOIN club c ON e.clubid = c.clubid
WHERE m.userid = ?;`; // Replaced req.query.postid with 1
connection.query(query, [req.session.user.userid],function (qerr, rows, fields) { // Removed [req.query.postid] parameter
connection.release();
if (qerr) {
console.log("qerr for GET /members-events", qerr);
res.sendStatus(500);
return;
}
res.json(rows);
});
});
});
// get members posts from database
app.get('/members-feeds', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.log("cerr for GET /members-feeds", cerr);
res.sendStatus(500);
return;
}
let query = `SELECT p.*, c.clubname
FROM post p
JOIN membership m ON p.clubid = m.clubid
JOIN club c ON p.clubid = c.clubid
WHERE m.userid = ?;`; // Replaced req.query.postid with 1
connection.query(query, [req.session.user.userid], function (qerr, rows, fields) { // Removed [req.query.postid] parameter
connection.release();
if (qerr) {
console.log("qerr for GET /members-feeds", qerr);
res.sendStatus(500);
return;
}
res.json(rows);
});
});
});
// ----------------------- NATHAN'S ROUTES ----------------------//
// some duplicates ... will clean this up
// get the userid of the current session
app.get('/get-userid', function (req, res) {
var { userid } = req.session.user;
res.json({ userid: userid });
});
// Route to check if current_userid has adminclearance
app.get('/admin-check', function (req, res) {
var { userid } = req.session.user;
// check adminclearance with session's userid
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.log("CError checking adminclearance:", cerr);
res.sendStatus(500);
return;
}
let query = `SELECT adminclearance FROM Users WHERE userid = ?`;
connection.query(query, [userid], function (qerr, rows, fields) {
connection.release();
if (qerr) {
console.log("QError checking adminclearance:", qerr);
res.sendStatus(500);
return;
}
// if user has adminclearance send true, else false
if (rows.length > 0 && rows[0].adminclearance === 'yes') {
res.send(true);
} else {
res.send(false);
}
});
});
});
// return list of user in alphabetical order: firstname, lastname, email, role
// need client to send clubid
app.get('/clubmembers', function (req, res, next) {
console.log("Trying to access club members");
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.error("Error establishing database connection:", cerr);
res.sendStatus(500);
return;
}
let query = `SELECT firstname, lastname, email, memberrole AS role FROM membership
JOIN Users ON membership.userid = Users.userid
WHERE membership.clubid = ?
ORDER BY firstname, lastname;`;
// changed from req.body
connection.query(query, [req.query.clubid], function (qerr, rows, fields) {
connection.release();
if (qerr) {
console.log("qerr for get /clubmembers");
res.sendStatus(500);
return;
}
res.json(rows);
});
});
});
// admin client receives firstname, lastname, email,
// adminclearance from all registered users in alphabetical order
app.get('/siteusers', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.error("Error establishing database connection:", cerr);
res.sendStatus(500);
return;
}
let query = `SELECT firstname, lastname, email, adminclearance FROM Users ORDER BY firstname, lastname;`;
connection.query(query, [req.body], function (qerr, rows, fields) {
connection.release();
if (qerr) {
console.log("qerr for get /siteusers");
res.sendStatus(500);
return;
}
res.json(rows);
});
});
});
// users deleted by admins in the manage-users.html page will be reflected in the database
app.post("/deleteuser", function (req, res) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.error("Error establishing database connection:", cerr);
res.sendStatus(500);
return;
}
var query = "DELETE FROM Users WHERE email = ?";
connection.query(query, [req.body.email], function (derr, result) {
connection.release();
if (derr) {
console.error("Error deleting user:", derr);
res.sendStatus(500);
return;
}
res.sendStatus(200);
});
});
});
// user information changed by admins in manage-users.html will be reflected in the database
app.post("/updateuser", function (req, res) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.error("Error establishing database connection:", cerr);
res.sendStatus(500);
return;
}
var query = "UPDATE Users SET firstname = ?, lastname = ?, email = ? WHERE email = ?";
// eslint-disable-next-line max-len
connection.query(query, [req.body.newFirstName, req.body.newLastName, req.body.newEmail, req.body.email], function (qerr, result) {
connection.release();
if (qerr) {
console.error("Error updating user details:", qerr);
res.sendStatus(500);
return;
}
res.sendStatus(200);
});
});
});
// apply to be a club member
// client send clubid
app.post('/membershipapplication', function (req, res, next) {
// get body from req; req is a json array extracted from a form
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.error("Error membershipapplication:", cerr);
res.sendStatus(500);
return;
}
let query = `INSERT INTO membership (userid, clubid) VALUES(?, ?)`;
// insert user input into queries
// eslint-disable-next-line max-len
connection.query(query, [req.session.user.userid, req.query.clubid], function (qerr, rows, fields) {
connection.release();
if (qerr) {
console.error("Error membershipapplication:", qerr);
res.sendStatus(500);
return;
}
res.end();
});
});
});
// join us button saves to 'joined' after refresh
// check if the user is a member of the club they are viewing
app.get('/checkmembership', function (req, res, next) {
var query = `SELECT * FROM membership WHERE userid = ? AND clubid = ?`;
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.error("Error checking membership status:", cerr);
res.sendStatus(500);
return;
}
// eslint-disable-next-line max-len
connection.query(query, [req.session.user.userid, req.query.clubid], function (qerr, rows, fields) {
connection.release();
if (qerr) {
console.error("Error checking membership status:", qerr);
res.sendStatus(500);
return;
}
if (rows.length > 0) {
console.log("JOINED");
res.json("JOINED");
} else {
console.log("NOT_JOINED");
res.json("NOT_JOINED");
}
});
});
});
// display a club
// client send clubid
// client receives clubname, clubdescription for public posts
app.get('/thisclub', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.log("Error getting club information:", cerr);
res.sendStatus(500);
return;
}
let query = `SELECT clubname, clubdescription FROM club WHERE clubid = ?;`;
connection.query(query, [req.query.clubid], function (qerr, rows, fields) {
console.log("Clubid:", req.query.clubid);
connection.release();
if (qerr) {
console.log("Error getting club information:", qerr);
res.sendStatus(500);
return;
}
res.json(rows[0]);
});
});
});
// system admin edit the clubname and description
app.post('/editclub', function (req, res, next) {
// get body from req; req is a json array extracted from a form
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.error("Error editing club info:", cerr);
res.sendStatus(500);
return;
}
let query = `UPDATE club SET clubname = ?, clubdescription = ? WHERE clubid = ?;`;
// insert user input into queries
connection.query(query, [req.body.clubname, req.body.clubdescription, req.body.clubid], function (qerr, rows, fields) {
connection.release();
if (qerr) {
console.error("Error editing club info:", qerr);
res.sendStatus(500);
return;
}
res.sendStatus(200);
});
});
});
// system admin delete a club from the database
app.delete('/deleteclub/:clubid', function (req, res, next) {
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.error("Error deleting club:", cerr);
res.sendStatus(500);
return;
}
let query = `DELETE FROM club WHERE clubid = ?;`;
connection.query(query, [req.params.clubid], function (qerr, result) {
connection.release();
if (qerr) {
console.error("Error deleting club:", qerr);
res.sendStatus(500);
return;
}
res.sendStatus(200);
});
});
});
// system admin upgrade another user to admin status
app.post('/giveAdminStatus', function (req, res, next) {
// get body from req; req is a json array extracted from a form
req.pool.getConnection(function (cerr, connection) {
if (cerr) {
console.error("Error upgrading user:", cerr);
res.sendStatus(500);
return;
}
let query = `UPDATE Users SET adminclearance = 'yes' WHERE email = ?;`;
// insert user input into queries
connection.query(query, [req.body.email], function (qerr, rows, fields) {
console.log(req.body.email);
connection.release();
if (qerr) {
console.error("Error upgrading user:", qerr);
res.sendStatus(500);
return;
}
console.log("Rows affected:", rows.affectedRows); // Check the number of affected rows
res.sendStatus(200);
});
});
});