Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
node-backendapi/routes/articles.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
121 lines (110 sloc)
3.61 KB
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
const Router = require('koa-router'); | |
const bodyParser = require('koa-bodyparser'); | |
//adding prefix as articles.js will be used when manipulating with articles on /api/v1/articles/... | |
const router = Router({prefix: '/api/v1/articles'}); | |
const service = require('../models/articles'); | |
const auth = require('../auth/basic') | |
// Router methods used - functions | |
router.get('/', getAll); | |
router.post('/', bodyParser(), createArticle); | |
// add view +1 when getById invoked | |
router.get('/:id([0-9]{1,})', auth, getById); | |
router.patch('/:id([0-9]{1,})', bodyParser(), updateArticle); | |
router.del('/:id([0-9]{1,})', deleteArticle); | |
//FUNCTIONS DEFINITIONS | |
// Function to get all articles | |
async function getAll(ctx, next) { | |
results = await service.getAll(); | |
console.log(results) | |
if ((results != null) && (results.length > 0)) { | |
ctx.status = 200; | |
ctx.body = results; | |
} | |
else { | |
ctx.status = 404; | |
ctx.body = 'No articles found'; | |
} | |
} | |
// Function to get articles by specified ID - using articleRequestById function | |
async function getById(ctx, next) { | |
let id = ctx.params.id; | |
if ((id > 0) && (id != NaN)) { | |
let result = await service.getById(id); | |
console.log(result) | |
if ((result != null) && (result.length > 0)) { | |
ctx.status = 200; | |
ctx.body = JSON.parse(JSON.stringify(result)); | |
} | |
else { | |
ctx.status = 404; | |
ctx.body = 'Article not found'; | |
} | |
} | |
else { | |
ctx.status = 400; | |
ctx.body = 'ID needs to be number bigger than 0'; | |
} | |
} | |
// Function to create article --- needs to be handled | |
async function createArticle(ctx, next) { | |
let newArticle = ctx.request.body; | |
try { | |
let obj = await service.createArticle(newArticle); | |
ctx.status = 201; | |
ctx.body = obj; | |
} | |
catch(e) { | |
ctx.status = 400; | |
ctx.body = 'You submitted wrong values or you missing some, please check and try again'; | |
} | |
} | |
// Function to update article based on specified ID (loop if updating more columns) | |
async function updateArticle(ctx, next) { | |
let id = ctx.params.id; | |
let updatedArticle = ctx.request.body; | |
if (id > 0) { | |
try { | |
var returnedObj = await service.getById(id); | |
if ((returnedObj != null) && (returnedObj.length > 0)) { | |
let obj = await service.updateArticle(id, updatedArticle); | |
ctx.status = 201; | |
ctx.body = obj[0]; | |
} | |
else { | |
ctx.status = 404; | |
ctx.body = 'Requested article was not found and cannot be edited'; | |
} | |
} | |
catch(error) { | |
ctx.status = 400; | |
ctx.body = 'You submitted wrong values or you missing some, please check and try again'; | |
} | |
} | |
else { | |
ctx.status = 400; | |
ctx.body = 'Not valid value of article'; | |
} | |
} | |
// Function to delete article based on specified ID (select from articleRequestById [SELECT] then delete) | |
async function deleteArticle(ctx, next) { | |
let id = ctx.params.id; | |
if ((id > 0) && (id != NaN)) { | |
let result = await service.getById(id) | |
if ((result.length > 0) && (result != null)) { | |
let obj = await service.deleteArticle(id); | |
//check the article | |
ctx.body = obj[0]; | |
ctx.status = 201; | |
} | |
else { | |
ctx.status = 404; | |
ctx.body = 'Requested article was not found'; | |
} | |
} | |
else { | |
ctx.status = 400; | |
ctx.body = 'ID needs to be bigger than 0' | |
} | |
} | |
// Exports all to be used in router lib | |
module.exports = router; |