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?
DynamicWebsite/routes/menu.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
127 lines (113 sloc)
3.1 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
'use strict' | |
/* MODULE IMPORTS */ | |
const Router = require('koa-router') | |
const router = new Router() | |
const dbMenu = './databases/menu.db' | |
/* MODULE IMPORTS */ | |
const Order = require('../modules/order') | |
const PDF = require('../modules/pdf') | |
router.get('/mainmenu', async ctx => { | |
try{ | |
const auth = ctx.session.authorised | |
if(auth === 'Admin' || auth === 'Kitchen' || auth === 'Waiting') { | |
await ctx.render('mainmenu') | |
} else ctx.redirect('/login?msg=you need to log in') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.get('/ready', async ctx => { | |
try{ | |
const auth = ctx.session.authorised | |
if(auth === 'Admin' || auth === 'Kitchen') { | |
const order = await new Order(dbMenu) | |
const unready = await order.getNotReadyOrders() | |
const ready = await order.getReadyOrders() | |
await ctx.render('ready', {unreadyOrder: unready, readyOrder: ready}) | |
} else await ctx.render('mainmenu') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.post('/ready', async ctx => { | |
try { | |
const body = ctx.request.body | |
console.log(body) | |
const order = await new Order(dbMenu) | |
if (body.ready === 'Ready') { | |
await order.setReady(body.ID) | |
} else if (body.unready === 'Unready') { | |
await order.setUnready(body.ID) | |
} | |
await ctx.redirect('./ready') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.get('/adding', async ctx => { | |
try{ | |
const auth = ctx.session.authorised | |
if(auth === 'Admin' || auth === 'Waiting') { | |
await ctx.render('adding') | |
} else await ctx.render('mainmenu') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.post('/adding', async ctx => { | |
try { | |
const body = ctx.request.body | |
const order = await new Order(dbMenu) | |
order.createPrice(body) | |
await order.addItem(body) | |
await ctx.redirect('./menu') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.get('/menu', async ctx => { | |
try{ | |
const auth = ctx.session.authorised | |
if(auth === 'Admin') { | |
const order = await new Order(dbMenu) | |
const data = await order.getItemsName() | |
await ctx.render('menu', {items: data}) | |
} else await ctx.redirect('./mainmenu') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
// eslint-disable-next-line max-lines-per-function | |
router.post('/menu', async ctx => { | |
try { | |
const body = ctx.request.body | |
console.log(body) | |
const pdf = new PDF() | |
pdf.createPDF(body) | |
ctx.redirect('/menu') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.get('/edit/:itemID', async ctx => { | |
try{ | |
const order = await new Order(dbMenu) | |
const data = await order.getItemInfo(ctx.params.itemID) | |
order.splitPrice(data[0]) | |
await ctx.render('edit', {items: data}) | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.post('/edit', async ctx => { | |
try { | |
const body = ctx.request.body | |
const order = await new Order(dbMenu) | |
order.createPrice(body) | |
await order.editItem(body) | |
await ctx.redirect('./menu') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
module.exports = router |