Skip to content
Permalink
Browse files
Change repo to follow project template
Change to line up this repo with the template found
[here](https://github.coventry.ac.uk/web/template-dynamic-websites).

This includes:
- ESLint config changes and code reformated to reflect this
- githooks
- DbContext not implemented error
- JSDoc config added
- Jest config changed
- VSCode config changed to line up with ESLint
  • Loading branch information
soperd committed Oct 15, 2019
1 parent 14b287f commit 0ab3b12308cff6fd543d4d87b3140f8d6d75a6dc
Show file tree
Hide file tree
Showing 18 changed files with 366 additions and 341 deletions.

This file was deleted.

@@ -1,6 +1,74 @@

{
"extends": "standard",
"env": {
"es6": true,
"jasmine": true,
"node": true,
"browser": true,
"jest": true
},
"parserOptions": {
"ecmaVersion": 8
},
"rules": {
"arrow-body-style": 2,
"arrow-spacing": ["warn", {"before": true, "after": true}],
"brace-style": 2,
"camelcase": [2, {"properties": "never"}],
"complexity": ["error", 5],
"eol-last": "warn",
"eqeqeq": "error",
"func-call-spacing": ["error", "never"],
"global-require": "error",
"handle-callback-err": "warn",
"indent": [1, "tab", {"SwitchCase": 1}],
"key-spacing": ["error", {"beforeColon": false, "afterColon": true}],
"linebreak-style": ["warn", "unix"],
"max-depth": ["error", 3],
"max-len": ["warn", { "code": 120, "tabWidth": 4 }],
"max-lines": ["warn", 150],
"max-lines-per-function": ["error", 20],
"max-nested-callbacks": ["error", 4],
"max-params": ["error", 5],
"max-statements": ["error", 20],
"no-cond-assign": 2,
"no-dupe-args": "error",
"no-dupe-keys": "error",
"no-duplicate-case": "error",
"no-empty": "warn",
"no-empty-function": 2,
"no-multiple-empty-lines": "warn",
"no-extra-parens": 2,
"no-func-assign": 2,
"no-irregular-whitespace": 2,
"no-magic-numbers": [1, {"ignore": [-1, 0, 1]}],
"no-multi-spaces": 1,
"no-multi-str": 1,
"no-unexpected-multiline": 2,
"no-unreachable": 2,
"no-self-assign": 2,
"no-trailing-spaces": 1,
"no-undef": 2,
"no-unused-vars": 1,
"no-var": 2,
"prefer-arrow-callback": 1,
"prefer-const": 2,
"prefer-template": "error",
"quotes": [1, "single"],
"semi": [1, "never"],
"space-before-blocks": ["error", { "functions": "always", "keywords": "always", "classes": "always" }],
"space-before-function-paren": [2, "never"],
"strict": [2, "global"],
"yoda": 2
},
"overrides": [{
"files": [ "*.test.js", "*.spec.js", "sqlite-async.js" ],
"rules": {
"space-before-function-paren": "off"
"global-require": "off",
"max-lines-per-function": "off",
"max-lines": "off",
"max-statements": "off",
"no-magic-numbers": "off"
}
}]
}
@@ -0,0 +1,4 @@
#!/bin/sh

echo "running the 'post-commit' script"
./node_modules/.bin/markdownlint --ignore node_modules .
@@ -0,0 +1,4 @@
#!/bin/sh

echo "running the 'pre-commit' script"
./node_modules/.bin/eslint .
@@ -1,4 +1,6 @@
node_modules

# ignore sqlite databases
*.db
.DS_Store
node_modules/
coverage/
*.db
screenshots/*
docs/
@@ -1,3 +1,5 @@
{
"editor.tabSize": 2
}
"editor.insertSpaces": false,
"editor.tabSize": 2,
"files.eol": "\n"
}
@@ -26,5 +26,6 @@ This project is split into a few folders:
- `controllers/`: contains subrouters for different parts of the site.
- `models/`: contains classes that represent database entities.
- `public/`: contains the site's front-end static pages and JavaScript.
- `utils/`: contains shared utility code.
- `views/`: contains dynamic HTML templates.
- `./`: contains code that is used directly by the Koa app.
12 app.js
@@ -2,18 +2,20 @@
* Creates a common Koa app
*/

'use strict'

const path = require('path')

const Koa = require('koa')
const Views = require('koa-views')

const app = new Koa()
const handlebars = new Views(
path.join(__dirname, '/views'),
{
map: { hbs: 'handlebars' },
extension: 'hbs'
}
path.join(__dirname, '/views'),
{
map: { hbs: 'handlebars' },
extension: 'hbs'
}
)

const login = require('./controllers/login')
@@ -1,18 +1,20 @@
#!/usr/bin/env node

'use strict'

const fs = require('fs')
const path = require('path')

const sqlite = require('sqlite')

sqlite.open('app.db').then(db => {
const sqlPath = path.join(__dirname, 'build_db.sql')
fs.readFile(sqlPath, 'utf8', async (err, sql) => {
if (err) {
console.error(err)
process.exit(1)
}
const sqlPath = path.join(__dirname, 'build_db.sql')
fs.readFile(sqlPath, 'utf8', async(err, sql) => {
if (err) {
console.error(err)
process.exit(1)
}

await db.exec(sql)
})
await db.exec(sql)
})
})
@@ -1,3 +1,4 @@
'use strict'

const Router = require('koa-router')

@@ -1,15 +1,16 @@
'use strict'

const Router = require('koa-router')

const login = new Router({ prefix: '/login' })

login.get('/', async ctx => {
await ctx.render('login.hbs')
await ctx.render('login.hbs')
})

login.post('/', async ctx => {
console.log(ctx.query)
console.log(ctx.querystring)
console.log(ctx.query)
console.log(ctx.querystring)
})

module.exports = login
178 db.js
@@ -2,92 +2,116 @@
* Database controller
*/

'use strict'

const sqlite = require('sqlite')

const { NotImplemented } = require('./utils/errors')
const User = require('./models/user')

class DbContext {
async getUsers() { }
async getUser(id) { }
async deleteUser(id) { }
async createUser(user) { }
async updateUser(user) { }

async execute(query) { }
async getUsers() {
throw new NotImplemented('getUsers is not implemented')
}

// eslint-disable-next-line no-unused-vars
async getUser(id) {
throw new NotImplemented('getUser is not implemented')
}

// eslint-disable-next-line no-unused-vars
async deleteUser(id) {
throw new NotImplemented('deleteUser is not implemented')
}

// eslint-disable-next-line no-unused-vars
async createUser(user) {
throw new NotImplemented('createUser is not implemented')
}

// eslint-disable-next-line no-unused-vars
async updateUser(user) {
throw new NotImplemented('updateUser is not implemented')
}

// eslint-disable-next-line no-unused-vars
async execute(query) {
throw new NotImplemented('execute is not implemented')
}
}

class SqliteDbContext extends DbContext {
constructor(filename) {
super()

this.sqlitePromise = sqlite.open(filename, { Promise })
}

async getUser(id) {
const sqlite = await this.sqlitePromise

let query

if (typeof id === 'number') {
query = 'SELECT * FROM `users` WHERE `id` = ?;'
} else if (typeof id === 'string') {
query = 'SELECT * FROM `users` WHERE `username` = ?;'
} else {
throw new TypeError('id must be number or string')
}

const user = await sqlite.get(query, id)
return Object.assign(new User(), user)
}

async getUsers() {
const sqlite = await this.sqlitePromise

const users = await sqlite.all('SELECT * FROM `users`;')
return users.map(x => Object.assign(new User(), x))
}

async deleteUser(id) {
const sqlite = await this.sqlitePromise

let query

if (typeof id === 'number') {
query = 'DELETE FROM `users` WHERE `id` = ?;'
} else if (typeof id === 'string') {
query = 'DELETE FROM `users` WHERE `username` = ?;'
} else {
throw new TypeError('id must be number or string')
}

await sqlite.run(query, id)
}

async createUser(user) {
const sqlite = await this.sqlitePromise

const { lastID } = await sqlite.run(
'INSERT INTO `users` (`username`, `hash`) VALUES (?, ?);',
user.username,
user.hash
)
return this.getUser(lastID)
}

async updateUser(user) {
const sqlite = await this.sqlitePromise

await sqlite.run(
'UPDATE `users` SET `username` = ?, `hash` = ? WHERE `id` = ?;',
user.username,
user.hash,
user.id
)
return this.getUser(user.id)
}
constructor(filename) {
super()

this.sqlitePromise = sqlite.open(filename, { Promise })
}

async getUser(id) {
const sqlite = await this.sqlitePromise

let query

if (typeof id === 'number') {
query = 'SELECT * FROM `users` WHERE `id` = ?;'
} else if (typeof id === 'string') {
query = 'SELECT * FROM `users` WHERE `username` = ?;'
} else {
throw new TypeError('id must be number or string')
}

const user = await sqlite.get(query, id)
return Object.assign(new User(), user)
}

async getUsers() {
const sqlite = await this.sqlitePromise

const users = await sqlite.all('SELECT * FROM `users`;')
return users.map(x => Object.assign(new User(), x))
}

async deleteUser(id) {
const sqlite = await this.sqlitePromise

let query

if (typeof id === 'number') {
query = 'DELETE FROM `users` WHERE `id` = ?;'
} else if (typeof id === 'string') {
query = 'DELETE FROM `users` WHERE `username` = ?;'
} else {
throw new TypeError('id must be number or string')
}

await sqlite.run(query, id)
}

async createUser(user) {
const sqlite = await this.sqlitePromise

const { lastID } = await sqlite.run(
'INSERT INTO `users` (`username`, `hash`) VALUES (?, ?);',
user.username,
user.hash
)
return this.getUser(lastID)
}

async updateUser(user) {
const sqlite = await this.sqlitePromise

await sqlite.run(
'UPDATE `users` SET `username` = ?, `hash` = ? WHERE `id` = ?;',
user.username,
user.hash,
user.id
)
return this.getUser(user.id)
}
}

module.exports = {
DbContext,
SqliteDbContext
DbContext,
SqliteDbContext
}

0 comments on commit 0ab3b12

Please sign in to comment.