Skip to content
Permalink
Browse files
Update, delete articles, articleViews table
  • Loading branch information
ryklovae committed Jan 27, 2023
1 parent ca2139d commit b9da238c3422140249572dbb578cd97962c0dea8
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 112 deletions.
@@ -1 +1,3 @@
node_modules/
node_modules/

config.js
@@ -3,7 +3,7 @@ const fetchPromise = fetch('https://kiwipanel-gravitycrater-3000.codio-box.uk/ap
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({title: "test", fullText: 'Textual content'})
body: JSON.stringify({title: "test", allText: 'Textual content'})
});
fetchPromise.then(res => res.json()).then(res => console.log(res))

@@ -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'
}
}
@@ -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);
@@ -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;
}
@@ -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;
}
@@ -0,0 +1 @@
const db = require('../helpers/database');

0 comments on commit b9da238

Please sign in to comment.