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/models/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.
70 lines (62 sloc)
2.38 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 db = require('../helpers/database'); | |
//list all the applications in the database | |
exports.getAll = async function getAll (page, limit, order, direction) { | |
const offset = (page - 1) * limit; | |
let query; | |
if (direction === 'DESC') { | |
query = "SELECT * FROM applications ORDER BY ?? DESC LIMIT ? OFFSET ?;"; | |
} else { | |
query = "SELECT * FROM applications ORDER BY ?? ASC LIMIT ? OFFSET ?;"; | |
} | |
const values = [order, parseInt(limit), parseInt(offset)]; | |
const data = await db.run_query(query, values); | |
return data; | |
} | |
//get a single application by its id | |
exports.getById = async function getById (id) { | |
const query = "SELECT * FROM applications WHERE ID = ?;"; | |
const values = [id]; | |
const data = await db.run_query(query, values); | |
return data; | |
} | |
//create a new application in the database | |
exports.add = async function add (application) { | |
const query = "INSERT INTO articles SET ?"; | |
const data = await db.run_query(query, article); | |
return data; | |
} | |
//delete an application by its id | |
exports.delById = async function delById (id) { | |
const query = "DELETE FROM appications WHERE ID = ?;"; | |
const values = [id]; | |
const data = await db.run_query(query, values); | |
return data; | |
} | |
//update an existing application | |
exports.update = async function update (appication) { | |
const query = "UPDATE applications SET ? WHERE ID = ?;"; | |
const values = [appication, appication.ID]; | |
const data = await db.run_query(query, values); | |
return data; | |
} | |
// get all applications with a pending status | |
exports.getByPendingStatus = async function getByPendingStatus (applicationStatus) { | |
const query = "SELECT * FROM applications WHERE applicationStatus = 'pending';"; | |
const values = [applicationStatus]; | |
const data = await db.run_query(query, values); | |
return data; | |
} | |
// get all applications with an accepted status | |
exports.getByAcceptedStatus = async function getByAcceptedStatus (currentStatus) { | |
const query = "SELECT * FROM applications WHERE applicationStatus = 'accepted';"; | |
const values = [applicationStatus]; | |
const data = await db.run_query(query, values); | |
return data; | |
} | |
// get all applications with a rejected status | |
exports.getByStatusRejected = async function getByRejectedStatus (applicationStatus) { | |
const query = "SELECT * FROM applications WHERE currentStatus = 'rejected';"; | |
const values = [applicationStatus]; | |
const data = await db.run_query(query, values); | |
return data; | |
} |