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?
8463645-6003CEM-Backend-Resit-Deferral/routes/applications.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
99 lines (84 sloc)
2.82 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'); | |
const auth = require('../controllers/auth'); | |
const applications = require('../models/applications'); | |
const {validateApplication} = require('../controllers/validation'); | |
const router = Router({prefix: '/api/v1/applications'}); | |
// applications routes | |
router.get('/', getAll); | |
router.post('/', auth, bodyParser(), validateApplication, createApplication); | |
router.get('/:id([0-9]{1,})', getById); | |
router.put('/:id([0-9]{1,})', auth, bodyParser(), validateApplication, updateApplication); | |
router.del('/:id([0-9]{1,})', auth, deleteApplication); | |
// application status routes | |
router.get('/status/pending', getByPendingStatus); | |
router.get('/status/accepted', getByAcceptedStatus); | |
router.get('/status/rejected', getByRejectedStatus); | |
async function getAll(ctx) { | |
const {page=1, limit=100, order="id", direction='ASC'} = ctx.request.query; | |
const result = await applications.getAll(page, limit, order, direction); | |
if (result.length) { | |
ctx.body = result; | |
} | |
} | |
async function getById(ctx) { | |
const id = ctx.params.id; | |
const result = await applications.getById(id); | |
if (result.length) { | |
const application = result[0]; | |
ctx.body = application; | |
} | |
} | |
async function createApplication(ctx) { | |
const body = ctx.request.body; | |
const result = await applications.add(body); | |
if (result.affectedRows) { | |
const id = result.insertId; | |
ctx.status = 201; | |
ctx.body = {ID: id, created: true, link: `${ctx.request.path}/${id}`}; | |
} | |
} | |
async function updateApplication(ctx) { | |
const id = ctx.params.id; | |
let result = await applications.getById(id); // check it exists | |
if (result.length) { | |
let article = result[0]; | |
// exclude fields that should not be updated | |
const {ID, firstName, lastName, email, ...body} = ctx.request.body; | |
// overwrite updatable fields with remaining body data | |
Object.assign(application, body); | |
result = await applications.update(article); | |
if (result.affectedRows) { | |
ctx.body = {ID: id, updated: true, link: ctx.request.path}; | |
} | |
} | |
} | |
async function deleteApplication(ctx) { | |
const id = ctx.params.id; | |
const result = await appications.delById(id); | |
if (result.affectedRows) { | |
ctx.body = {ID: id, deleted: true} | |
} | |
} | |
async function getByPendingStatus(ctx) { | |
const status = ctx.params.id; | |
const result = await applications.getByPendingStatus(status); | |
if (result.length) { | |
ctx.body = result; | |
} | |
} | |
async function getByAcceptedStatus(ctx) { | |
const status = ctx.params.id; | |
const result = await applications.getByAcceptedStatus(status); | |
if (result.length) { | |
ctx.body = result; | |
} | |
} | |
async function getByRejectedStatus(ctx) { | |
const status = ctx.params.id; | |
const result = await applications.getByRejectedStatus(status); | |
if (result.length) { | |
ctx.body = result; | |
} | |
} | |
module.exports = router; |