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.
Showing
16 changed files
with
1,667 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Export database connection information. | ||
// Use the environment settings or given defaults. | ||
exports.config = { | ||
host: process.env.DB_HOST || "localhost", | ||
port: process.env.DB_PORT || 3306, | ||
user: process.env.DB_USER || "root", | ||
password: process.env.DB_PASSWORD || "codio", | ||
database: process.env.DB_DATABASE || "mydatabase", | ||
connection_limit: 100 | ||
} |
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,6 @@ | ||
const passport = require('koa-passport'); | ||
const basicAuth = require('../strategies/basic'); | ||
|
||
passport.use(basicAuth); | ||
|
||
module.exports = passport.authenticate(['basic'], {session:false}); |
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,29 @@ | ||
const {Validator, ValidationError} = require('jsonschema'); | ||
|
||
const schema = require('../schemas/article.schema.js'); | ||
|
||
const v = new Validator(); | ||
|
||
exports.validateArticle = async (ctx, next) => { | ||
const validationOptions = { | ||
throwError: true, | ||
allowUnknownAttributes: false | ||
}; | ||
|
||
const body = ctx.request.body; | ||
|
||
try { | ||
v.validate(body, schema, validationOptions); | ||
await next(); | ||
} catch (error) { | ||
if (error instanceof ValidationError) { | ||
console.error(error); | ||
ctx.status = 400 | ||
ctx.body = error; | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
} | ||
|
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,14 @@ | ||
var mysql = require('promise-mysql'); | ||
var info = require('../config'); // define an async utility function to get a connection // run an SQL query then end the connection | ||
|
||
exports.run_query = async function run_query(query, values) { | ||
try { | ||
const connection = await mysql.createConnection(info.config); | ||
let data = await connection.query(query, values); | ||
await connection.end(); | ||
return data; | ||
} catch (error) { | ||
console.error(error, query, values); | ||
throw 'Database query error' | ||
} | ||
} |
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,31 @@ | ||
const Koa = require('koa'); | ||
const Router = require('koa-router'); | ||
const cors = require('@koa/cors'); | ||
|
||
const app = new Koa(); | ||
const router = new Router(); | ||
|
||
//this lets the API make cross origin requests | ||
const corsOptions = { | ||
origin: 'https://open-saddle-3000.codio-box.uk', | ||
} | ||
|
||
router.get('/api', welcomeAPI); | ||
|
||
function welcomeAPI(ctx) { | ||
ctx.body = { | ||
message: 'This is the root of the API' | ||
} | ||
} | ||
|
||
const articles = require('./routes/articles.js'); | ||
//const special = require('./routes/special.js'); | ||
|
||
app.use(router.routes()); | ||
//app.use(special.routes()); | ||
app.use(articles.routes()); | ||
app.use(cors(corsOptions)); | ||
|
||
// run the app on port 3000 | ||
//let port = process.env.PORT || 3000; | ||
app.listen(3000); |
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,23 @@ | ||
const db = require('../helpers/database'); | ||
|
||
//get a single article by its id | ||
exports.getById = async function getById (id) { | ||
let query = "SELECT * FROM articles WHERE ID = ?"; | ||
let values = [id]; | ||
let data = await db.run_query(query, values); | ||
return data; | ||
} | ||
|
||
//list all the articles in the database | ||
exports.getAll = async function getAll (page, limit, order) { | ||
let query = "SELECT * FROM articles;"; | ||
let data = await db.run_query(query); | ||
return data; | ||
} | ||
|
||
//create a new article in the database | ||
exports.add = async function add (article) { | ||
let query = "INSERT INTO articles SET ?"; | ||
let data = await db.run_query(query, article); | ||
return data; | ||
} |
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,69 @@ | ||
const db = require('../helpers/database'); | ||
const bcrypt = require('bcrypt'); | ||
|
||
exports.findByUsername = async function findByUsername(username) { | ||
const query = "SELECT * FROM users WHERE username = ?;"; | ||
const user = await db.run_query(query, username); | ||
return user; | ||
} | ||
|
||
//search for users based on email pattern | ||
exports.emailSearch = async function emailSearch (q) { | ||
const query = "SELECT * FROM users WHERE email LIKE ?;"; | ||
const data = await db.run_query(query, '%'+q+'%'); | ||
return data; | ||
} | ||
|
||
//get a single user by its id | ||
exports.getById = async function getById (id) { | ||
const query = "SELECT * FROM users WHERE ID = ?;"; | ||
const values = [id]; | ||
const data = await db.run_query(query, values); | ||
return data; | ||
} | ||
|
||
//get a single user by the (unique) username | ||
exports.findByUsername = async function getByUsername(username) { | ||
const query = "SELECT * FROM users WHERE username = ?;"; | ||
const user = await db.run_query(query, username); | ||
return user; | ||
} | ||
|
||
//list all the users in the database | ||
exports.getAll = async function getAll (limit=10, page=1) { | ||
const offset = (page - 1) * limit; | ||
const query = "SELECT * FROM users LIMIT ?,?;"; | ||
const data = await db.run_query(query, [offset, limit]); | ||
return data; | ||
} | ||
|
||
//create a new user in the database | ||
exports.add = async function add (user) { | ||
const query = "INSERT INTO users SET ?"; | ||
const password = user.password; | ||
const hash = bcrypt.hashSync(password, 10); | ||
user.password = hash; | ||
const data = await db.run_query(query, user); | ||
return data; | ||
} | ||
|
||
//delete a user by its id | ||
exports.delById = async function delById (id) { | ||
const query = "DELETE FROM users WHERE ID = ?;"; | ||
const values = [id]; | ||
const data = await db.run_query(query, values); | ||
return data; | ||
} | ||
|
||
//update an existing user | ||
exports.update = async function update (user) { | ||
const query = "UPDATE users SET ? WHERE ID = ?;"; | ||
if (user.password) { | ||
const password = user.password; | ||
const hash = bcrypt.hashSync(password, 10); | ||
user.password = hash; | ||
} | ||
const values = [user, user.ID]; | ||
const data = await db.run_query(query, values); | ||
return data; | ||
} |
Oops, something went wrong.