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
12 changed files
with
411 additions
and
112 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
node_modules/ | ||
node_modules/ | ||
|
||
config.js |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const mysql = require('promise-mysql'); | ||
const 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) { | ||
// Don't let the actual error propagate up to the response object | ||
// as it may contain sensitive server information. | ||
// Instead log it somehow and throw a generic 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 |
---|---|---|
@@ -1,30 +1,13 @@ | ||
// My blog API | ||
// Set up the application and its router | ||
const Koa = require('koa'); | ||
|
||
const Koa = require('koa'); | ||
const Router = require('koa-router'); | ||
const app = new Koa(); | ||
const router = new Router(); | ||
|
||
/* | ||
* Define route handler(s): | ||
* | ||
* This means we connect HTTP methods: GET, POST, ... | ||
* and URI paths: /some/uri/path | ||
* to JavaScript functions that handle the request. | ||
* | ||
* Once defined we then add them to the app object. | ||
*/ | ||
|
||
router.get('/api/v1', welcomeAPI); | ||
app.use(router.routes()); // Define the actual handler functions | ||
|
||
function welcomeAPI(ctx, next) { | ||
ctx.body = { message: "Welcome to the blog API!" } | ||
} | ||
const app = new Koa(); | ||
|
||
const special = require('./routes/special.js') | ||
const articles = require('./routes/articles.js'); | ||
|
||
app.use(special.routes()); | ||
app.use(articles.routes()); | ||
|
||
// Finally, run the app as a process on a given port | ||
app.listen(3000); | ||
let port = process.env.PORT || 3000; | ||
|
||
app.listen(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,32 @@ | ||
const db = require('../helpers/database'); | ||
|
||
// update views of an article by its id | ||
exports.updateViews = async function updateViews (id) { | ||
// check if article id exists | ||
let query = "SELECT * FROM articles WHERE ID = ?;"; | ||
let articleExists = await db.run_query(query, id); | ||
console.log(articleExists) | ||
if (articleExists.length == 0) return 404; // article not found | ||
|
||
// check if article was viewed, get number of views | ||
query = "SELECT * FROM articleViews WHERE articleID = ?;"; | ||
let data = await db.run_query(query, id); | ||
|
||
console.log(data) | ||
if (data.length > 0) { // record exists | ||
let query = "UPDATE articleViews SET views = ? WHERE articleID = ?"; | ||
let views = data[0].views | ||
let values = [ views + 1, id ] | ||
let data2 = await db.run_query(query, values); | ||
} | ||
else { | ||
let query = "INSERT INTO articleViews SET ?"; | ||
let values = { "articleID": id, "views": 1 } | ||
let data2 = await db.run_query(query, values); | ||
} | ||
|
||
//let query = ""; | ||
//let values = [id]; | ||
//let data = await db.run_query(query, values); | ||
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,45 @@ | ||
const db = require('../helpers/database'); | ||
const articleViews = require('./articleViews'); | ||
|
||
//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); | ||
|
||
// update views | ||
await articleViews.updateViews(id); | ||
|
||
return data; | ||
} | ||
|
||
//list all the articles in the database | ||
exports.getAll = async function getAll (page, limit, order) { | ||
// TODO: use page, limit, order to give pagination | ||
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; | ||
} | ||
|
||
//update an article in the database | ||
exports.update = async function update (id, article) { | ||
let query = "UPDATE articles SET ? WHERE ID = ?"; | ||
|
||
let data = await db.run_query(query, [article, id]); | ||
return article; | ||
} | ||
|
||
//delete an article from the database | ||
exports.deleteArticle = async function deleteArticle (id) { | ||
let query = "DELETE FROM articles WHERE id = ?;"; | ||
|
||
let data = await db.run_query(query, id); | ||
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 @@ | ||
const db = require('../helpers/database'); |
Oops, something went wrong.