From 5ad3fa1a6a07609e8c26042b1121f1d03a972e15 Mon Sep 17 00:00:00 2001 From: 8cy <50817549+8cy@users.noreply.github.com> Date: Sat, 16 May 2020 06:34:54 -0700 Subject: add marketplace + formatting also add admin panel to profile --- server.js | 160 +++++++++++++--- views/change-password.ejs | 3 + views/error.ejs | 3 + views/index.ejs | 15 +- views/login.ejs | 3 + views/marketplace-spike.ejs | 455 ++++++++++++++++++++++++++++++++++++++++++++ views/marketplace.ejs | 320 +++++++++++++++++++++++++++++++ views/profile.ejs | 25 +++ views/signup.ejs | 3 + 9 files changed, 960 insertions(+), 27 deletions(-) create mode 100644 views/marketplace-spike.ejs create mode 100644 views/marketplace.ejs diff --git a/server.js b/server.js index a0326af..f77f8c3 100644 --- a/server.js +++ b/server.js @@ -285,6 +285,24 @@ app.post('/logout', (req, res, next) => { // }); app.get('/profile', async (req, res, next) => { + let EntryCount = await EntryItem.countDocuments(); + let UserCount = await User.countDocuments(); + + let rawUptime = process.uptime(); + const date = new Date(rawUptime * 1000); + const days = date.getUTCDate() - 1, + hours = date.getUTCHours(), + minutes = date.getUTCMinutes(), + seconds = date.getUTCSeconds(), + milliseconds = date.getUTCMilliseconds(); + let segments = []; + if (days > 0) segments.push(days + ' day' + ((days == 1) ? '' : 's')); + if (hours > 0) segments.push(hours + ' hour' + ((hours == 1) ? '' : 's')); + if (minutes > 0) segments.push(minutes + ' minute' + ((minutes == 1) ? '' : 's')); + if (seconds > 0) segments.push(seconds + ' second' + ((seconds == 1) ? '' : 's')); + if (milliseconds > 0) segments.push(milliseconds + ' millisecond' + ((seconds == 1) ? '' : 's')); + const dateString = segments.join(', '); + User.findById(req.session.userId) .exec((error, user) => { if (error) { @@ -307,7 +325,10 @@ app.get('/profile', async (req, res, next) => { req: req, User: User, name: user.username, - quote: quotes.getRandomQuote() + quote: quotes.getRandomQuote(), + EntryCount: EntryCount, + UserCount: UserCount, + dateString: dateString }); } } @@ -355,30 +376,30 @@ app.get('/terms', (req, res) => { // }) // }); -app.get('/marketplace', (req, res) => { - User.findById(req.session.userId) - .exec((error, user) => { - if (error) { - return res.render('error', { - quote: quotes.getRandomQuote(), - errorMsg: 'Unknown error.' - }); - } else { - if (user === null) { - //let err = new Error('Not logged in!'); - return res.render('login', { - req: req, - User: User, - quote: quotes.getRandomQuote() - }); - //return err.status = 400; - //return next(err); - } else { - return res.redirect('/') - } - } - }) -}) +// app.get('/marketplace', (req, res) => { +// User.findById(req.session.userId) +// .exec((error, user) => { +// if (error) { +// return res.render('error', { +// quote: quotes.getRandomQuote(), +// errorMsg: 'Unknown error.' +// }); +// } else { +// if (user === null) { +// //let err = new Error('Not logged in!'); +// return res.render('login', { +// req: req, +// User: User, +// quote: quotes.getRandomQuote() +// }); +// //return err.status = 400; +// //return next(err); +// } else { +// return res.redirect('/') +// } +// } +// }) +// }) app.post('/closeExampleVideo', async (req, res) => { req.session.exampleVideo = false; @@ -489,6 +510,95 @@ app.post('/deleteItem/:id', (req, res) => { }) }) +app.get('/marketplace', async (req, res) => { + //const todoItems = await TodoItem.find(); + User.findById(req.session.userId) + .exec(async (error, user) => { + if (error) { + return res.render('error', { + quote: quotes.getRandomQuote(), + errorMsg: 'Unknown error.' + }); + } else { + if (user === null) { + //let err = new Error('Not logged in!'); + res.render('marketplace', { + req: req, + User: User, + name: null, + quote: quotes.getRandomQuote() + }); + //return err.status = 400; + //return next(err); + } else { + const entryItems = EntryItem; + const loop = await EntryItem.find({ + "type": "purchase", + user: user.username + }); + const loopCount = await EntryItem.find({ + "type": "purchase", + "user": user.username + }).countDocuments(); + + const loop2 = await EntryItem.find({ + "type": "sale", + "user": user.username + }); + const loop2Count = await EntryItem.find({ + "type": "sale", + "user": user.username + }).countDocuments(); + + const loop3 = await EntryItem.find({ + "type": "trade", + "user": user.username + }); + const loop3Count = await EntryItem.find({ + "type": "trade", + "user": user.username + }).countDocuments(); + + let EntryCount = await EntryItem.countDocuments(); + let UserCount = await User.countDocuments(); + + let rawUptime = process.uptime(); + const date = new Date(rawUptime * 1000); + const days = date.getUTCDate() - 1, + hours = date.getUTCHours(), + minutes = date.getUTCMinutes(), + seconds = date.getUTCSeconds(), + milliseconds = date.getUTCMilliseconds(); + let segments = []; + if (days > 0) segments.push(days + ' day' + ((days == 1) ? '' : 's')); + if (hours > 0) segments.push(hours + ' hour' + ((hours == 1) ? '' : 's')); + if (minutes > 0) segments.push(minutes + ' minute' + ((minutes == 1) ? '' : 's')); + if (seconds > 0) segments.push(seconds + ' second' + ((seconds == 1) ? '' : 's')); + if (milliseconds > 0) segments.push(milliseconds + ' millisecond' + ((seconds == 1) ? '' : 's')); + const dateString = segments.join(', '); + + return res.render('marketplace', { + req: req, + User: User, + name: user.username, + entryItems: entryItems, + user: user, + loop: loop, + loopCount: loopCount, + loop2: loop2, + loop2Count: loop2Count, + loop3: loop3, + loop3Count: loop3Count, + EntryCount: EntryCount, + UserCount: UserCount, + dateString: dateString, + quote: quotes.getRandomQuote() + }); + } + } + }); +}); + app.get('*', (req, res) => { res.redirect('/') }) diff --git a/views/change-password.ejs b/views/change-password.ejs index aa24b28..05c0588 100644 --- a/views/change-password.ejs +++ b/views/change-password.ejs @@ -83,6 +83,9 @@