Skip to content

Commit

Permalink
Merge pull request #21 from CodeforHawaii/formatDate
Browse files Browse the repository at this point in the history
Format date
  • Loading branch information
tyliec authored Oct 28, 2022
2 parents 2bed1e4 + 07e0a12 commit fb5b892
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 7 deletions.
29 changes: 27 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const path = require('path');
const mongoose = require('mongoose');
const passport = require('passport');
const sass = require('node-sass-middleware');
// TODO: Edit functionality
// const methodOverride = require('method-override')

/**
* Load environment variables from .env file, where API keys and passwords are configured.
Expand Down Expand Up @@ -54,6 +56,12 @@ mongoose.connection.on('error', (err) => {
process.exit();
});

/**
* Allows modules (moment) to be used in pug
*/

app.locals.moment = require('moment');

/**
* Express configuration.
*/
Expand Down Expand Up @@ -115,6 +123,18 @@ app.use('/js/lib', express.static(path.join(__dirname, 'node_modules/popper.js/d
app.use('/js/lib', express.static(path.join(__dirname, 'node_modules/bootstrap/dist/js'), { maxAge: 31557600000 }));
app.use('/js/lib', express.static(path.join(__dirname, 'node_modules/jquery/dist'), { maxAge: 31557600000 }));
app.use('/webfonts', express.static(path.join(__dirname, 'node_modules/@fortawesome/fontawesome-free/webfonts'), { maxAge: 31557600000 }));
/**
* Allows hidden inputs to send PUT & DELETE requests
*/
// TODO: Edit functionality
// app.use(methodOverride((req, res) => {
// if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// // look in urlencoded POST bodies and delete it
// let method = req.body._method
// delete req.body._method
// return method
// }
// }))

/**
* Primary app routes.
Expand All @@ -132,6 +152,11 @@ app.post('/signup', userController.postSignup);
app.get('/contact', contactController.getContact);
app.post('/contact', contactController.postContact);
app.get('/urls', urlsController.index);
app.get('/urls/sortDes', urlsController.sortDescending);
app.get('/urls/sortAsc', urlsController.sortAscending);
app.get('/urls/edit/:id', urlsController.editUrl);
// TODO: Edit functionality
// app.put('/urls/:id', urlsController.updateUrl);
app.delete('/urls/deleteUrl', urlsController.deleteUrl);
app.get('/admin', adminController.index);
app.get('/account/verify', passportConfig.isAuthenticated, userController.getVerifyEmail);
Expand Down Expand Up @@ -163,8 +188,8 @@ if (process.env.NODE_ENV === 'development') {
app.use(errorHandler());
} else {
app.use((err, req, res) => {
console.error(err);
res.status(500).send('Server Error');
// console.error(err);
// res.status(500).send('Server Error');
});
}

Expand Down
72 changes: 72 additions & 0 deletions controllers/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,77 @@ module.exports = {
} catch(error) {
console.error(error)
}
},
sortDescending: async (req, res) => {
try {
const links = await Url.find().sort({date:-1})
const users = await User.find()
res.render('urls', {
title: 'URLs',
urlInfo:links,
currentUser:req.user.id,
userInfo:users,
visits: links.clickCounter,
})
} catch(error) {
console.error(error)
}
},
sortAscending: async (req, res) => {
try {
const links = await Url.find().sort({date:1})
const users = await User.find()
res.render('urls', {
title: 'URLs',
urlInfo:links,
currentUser:req.user.id,
userInfo:users,
visits: links.clickCounter,
})
} catch(error) {
console.error(error)
}
},
editUrl: async (req, res) => {
try {
let links = await Url.findById(req.params.id)
let users = await User.find({email:links.user})

if (!users) {
res.redirect('/')
}

if (users[0].email != req.user.email) {
res.redirect('/urls')
} else {
res.render('edit', {
title: 'Edit Url',
users,
links})
}
} catch(error) {
console.error(error)
}
},
updateUrl: async (req, res) => {
// TODO: Edit functionality
try {
console.log('if u see this it works')
// if (!users) {
// res.redirect('/')
// }

// if (users[0].email != req.user.email) {
// res.redirect('/urls')
// } else {
// links = await Url.findOneAndUpdate({_id:req.params.id}, req.body, {
// new: true,
// runValidators: true
// })
// res.redirect('/urls')
// }
} catch(error) {
console.error(error)
}
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"lodash": "^4.17.21",
"lusca": "^1.7.0",
"mailchecker": "^4.1.14",
"moment": "^2.29.1",
"moment": "^2.29.4",
"mongoose": "^6.2.8",
"morgan": "^1.10.0",
"nanoid": "^4.0.0",
Expand Down
24 changes: 24 additions & 0 deletions views/edit.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
extends layout

block head
script(src='https://www.google.com/recaptcha/api.js' async='' defer='')

block content
.card.rounded-10.pb-4.pt-2.mt-4.px-5.mx-auto
.card-body
p In development
form(method='PUT' action=`/urls/${links.id}`)
input(type='hidden', name='method', value="PUT")
label.col-form-label.d-block(for='name') Enter the link you want to shorten
input.form-control.mb-3(type='url', name='longUrl', placeholder='https://portal.ehawaii.gov/residents/newcomers-guide/#becomeAResidentOfHawaii', autofocus, required value=`${links.longUrl}`)
.alert.alert-danger.invisible.d-none#error-message
label.col-form-label.d-block(for='name') Customize your link
input.form-control.mb-3(type='text', name='aliasUrl', placeholder='NewcomersGuide', autofocus, required value=`${links.slug}`)
button.btn.btn-primary.w-100(type='submit')
i.far.fa-envelope.fa-sm
| Send
br
.alert.alert-success.invisible.d-none#result
.alert-heading 🎉 Your new URL 🎉
hr
p#shortUrl
11 changes: 7 additions & 4 deletions views/urls.pug
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ block content
else
h3 Welcome #{user.profile.name}
p Here are previous URLs you have shortened.
.d-flex.justify-content-end
a(href='/urls/' class='fas fa-redo text-dark text-decoration-none mx-2' title="Reset to default")
a(href='/urls/sortDes' class='fas fa-sort-amount-down text-dark text-decoration-none mx-2' title="Sort by descending date")
a(href='/urls/sortAsc' class='fas fa-sort-amount-up-alt text-dark text-decoration-none mx-2' title="Sort by ascending date")
.table-responsive
table.table.table-striped
thead
tr
th(scope='col') Alias
th(scope='col') Date
th(scope='col') Date
th(scope='col') Original URL
th(scope='col') Visits
tbody
Expand All @@ -26,16 +30,15 @@ block content
td
a(href=`${link.slug}` target="_blank") #{link.slug}
td
p #{link.date}
p= moment(link.date).format('MMMM Do YYYY, h:mm:ss a')
td
a(href=`${link.longUrl}` target="_blank ") #{link.longUrl}
td
p #{link.clickCounter}
td
a.btn.btn-success.mx-1.my-3#editUrlBtn(href='#')
a.btn.btn-success.mx-1.my-3#editUrlBtn(href=`/urls/edit/${link.id}`)
i.fas.fa-edit
form.d-inline-block#delete-form(action="#", method="post")
input(type='hidden', value='DELETE', name='_method')
button.btn.btn-danger.mx-1.my-3#delUrlBtn(type='submit')
i.fas.fa-trash
script(src="/js/urlBtns.js")

0 comments on commit fb5b892

Please sign in to comment.