Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Updated ESLint and JSDoc Exercise
The exercise was unclear. Exercise is now based on the todo files which have been updated
- Loading branch information
Showing
22 changed files
with
330 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -8,6 +8,7 @@ coverage/ | ||
docs/ | ||
sessions/ | ||
screenshots/* | ||
out/ | ||
|
||
# sqlite databases | ||
# *.db | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,56 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Routes File | ||
*/ | ||
|
||
'use strict' | ||
|
||
/* MODULE IMPORTS */ | ||
const bcrypt = require('bcrypt-promise') | ||
const Koa = require('koa') | ||
const Router = require('koa-router') | ||
const views = require('koa-views') | ||
const staticDir = require('koa-static') | ||
const stat = require('koa-static') | ||
const bodyParser = require('koa-bodyparser') | ||
const koaBody = require('koa-body')({multipart: true, uploadDir: '.'}) | ||
const session = require('koa-session') | ||
const sqlite = require('sqlite-async') | ||
const fs = require('fs-extra') | ||
const mime = require('mime-types') | ||
//const jimp = require('jimp') | ||
|
||
/* IMPORT CUSTOM MODULES */ | ||
const User = require('./modules/accounts') | ||
const handlebars = require('koa-hbs-renderer') | ||
|
||
const app = new Koa() | ||
const router = new Router() | ||
|
||
/* CONFIGURING THE MIDDLEWARE */ | ||
app.keys = ['darkSecret'] | ||
app.use(staticDir('public')) | ||
app.use(stat('public')) | ||
app.use(bodyParser()) | ||
app.use(session(app)) | ||
app.use(views(`${__dirname}/views`, { extension: 'handlebars' }, {map: { handlebars: 'handlebars' }})) | ||
app.use(handlebars({ paths: { views: `${__dirname}/views` } })) | ||
app.use(router.routes()) | ||
|
||
const port = 8080 | ||
|
||
const defaultPort = 8080 | ||
const port = process.env.PORT || defaultPort | ||
const dbName = 'website.db' | ||
const saltRounds = 10 | ||
const todo = require('./modules/todo') | ||
|
||
/** | ||
* The secure home page. | ||
* | ||
* @name Home Page | ||
* @route {GET} / | ||
* @authentication This route requires cookie-based authentication. | ||
*/ | ||
router.get('/', async ctx => { | ||
try { | ||
if(ctx.session.authorised !== true) return ctx.redirect('/login?msg=you need to log in') | ||
const data = {} | ||
if(ctx.query.msg) data.msg = ctx.query.msg | ||
await ctx.render('index') | ||
data.items = todo.getAll() | ||
ctx.render('home', data) | ||
} catch(err) { | ||
await ctx.render('error', {message: err.message}) | ||
console.log(err.message) | ||
ctx.render('home', {msg: err.message}) | ||
} | ||
}) | ||
|
||
/** | ||
* The user registration page. | ||
* | ||
* @name Register Page | ||
* @route {GET} /register | ||
*/ | ||
router.get('/register', async ctx => await ctx.render('register')) | ||
|
||
/** | ||
* The script to process new user registrations. | ||
* | ||
* @name Register Script | ||
* @route {POST} /register | ||
*/ | ||
router.post('/register', koaBody, async ctx => { | ||
router.post('/', ctx => { | ||
try { | ||
// extract the data from the request | ||
const body = ctx.request.body | ||
console.log(body) | ||
const {path, type} = ctx.request.files.avatar | ||
// call the functions in the module | ||
const user = await new User(dbName) | ||
await user.register(body.user, body.pass) | ||
// await user.uploadPicture(path, type) | ||
// redirect to the home page | ||
ctx.redirect(`/?msg=new user "${body.name}" added`) | ||
todo.add(body.item, body.qty) | ||
ctx.redirect('/') | ||
} catch(err) { | ||
await ctx.render('error', {message: err.message}) | ||
console.log(err.message) | ||
ctx.redirect(`/?msg=${err.message}`) | ||
} | ||
}) | ||
|
||
router.get('/login', async ctx => { | ||
const data = {} | ||
if(ctx.query.msg) data.msg = ctx.query.msg | ||
if(ctx.query.user) data.user = ctx.query.user | ||
await ctx.render('login', data) | ||
}) | ||
|
||
router.post('/login', async ctx => { | ||
router.get('/delete/:key', ctx => { | ||
try { | ||
const body = ctx.request.body | ||
const user = await new User(dbName) | ||
await user.login(body.user, body.pass) | ||
ctx.session.authorised = true | ||
return ctx.redirect('/?msg=you are now logged in...') | ||
console.log(`key: ${ctx.params.key}`) | ||
todo.delete(ctx.params.key) | ||
ctx.redirect('/msg=item deleted') | ||
} catch(err) { | ||
await ctx.render('error', {message: err.message}) | ||
console.log(err.message) | ||
ctx.redirect(`/${err.message}`) | ||
} | ||
}) | ||
|
||
router.get('/logout', async ctx => { | ||
ctx.session.authorised = null | ||
ctx.redirect('/?msg=you are now logged out') | ||
}) | ||
|
||
app.use(router.routes()) | ||
module.exports = app.listen(port, async() => console.log(`listening on port ${port}`)) | ||
module.exports = app.listen(port, () => console.log(`listening on port ${port}`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
'use strict'; | ||
|
||
module.exports = { | ||
displayName: 'test', | ||
verbose: true, | ||
collectCoverage: true, | ||
coverageThreshold: { | ||
global: { | ||
branches: 0, | ||
functions: 0, | ||
lines: 0, | ||
statements: 0 | ||
} | ||
}, | ||
testPathIgnorePatterns: [ | ||
'/node_modules/', | ||
'/__tests__/fixtures/', | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
'use strict' | ||
|
||
let data = [] | ||
|
||
module.exports.clear = () => { | ||
data = [] | ||
} | ||
|
||
module.exports.add = (item, qty) => { | ||
qty = Number(qty) | ||
if(isNaN(qty)) throw new Error('qty must be a number') | ||
data.push({item: item, qty: qty}) | ||
} | ||
|
||
module.exports.getAll = () => { | ||
for(const key in data) data[key].key = key | ||
return data | ||
} | ||
|
||
module.exports.delete = key => { | ||
console.log(`delete key ${key}`) | ||
return | ||
} | ||
|
||
module.exports.countItems = () => data.length |
Oops, something went wrong.