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?
dasilv32/routes/secure.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
150 lines (136 sloc)
4.84 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
//router and body requirements | |
const Router = require('koa-router') | |
const koaBody = require('koa-body')({multipart: true, uploadDir: '.'}) | |
//all connections to to modules (js) | |
const GitContent = require('../modules/gitcontent') | |
const Answer = require('../modules/answers') | |
const Question = require('../modules/questions') | |
const dbName = 'website.db' | |
//prefix to make every page /secure | |
const router = new Router({ prefix: '/secure' }) | |
//----------------ROUTER.GET-------------------// | |
//renders the menu for the git content and quiz | |
router.get('/', async ctx => { | |
try { | |
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure') | |
console.log(ctx.hbs) | |
await ctx.render('menu.handlebars', ctx.hbs) | |
} catch(err) { | |
console.log(err.message) | |
ctx.hbs.error = err.message | |
await ctx.render('error', ctx.hbs) | |
} | |
}) | |
//Render the BACKOFFICE page | |
router.get('/backoffice', async ctx =>{ | |
try{ | |
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/backoffice') | |
const questions = await new Question(dbName) | |
ctx.hbs.questiont = await questions.all() | |
console.log(ctx.hbs) | |
await ctx.render('backoffice.handlebars', ctx.hbs) | |
} catch(err){ | |
console.log(err.message) | |
ctx.hbs.error = err.message | |
await ctx.render('error', ctx.hbs) | |
} | |
}) | |
//Render for the QUIZ PAGE handlebar | |
router.get('/quiz', async ctx => { | |
try { | |
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/quiz') | |
const questions = await new Question(dbName) | |
ctx.hbs.questiont = await questions.all() | |
console.log(ctx.hbs) | |
await ctx.render('quizpage', ctx.hbs) | |
} catch(err) { | |
ctx.hbs.error = err.message | |
await ctx.render('error', ctx.hbs) | |
} | |
}) | |
//Render for the INDIVIDUAL QUIZ (loop each id) | |
router.get('/quizf/:id', async ctx => { | |
try { | |
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/quizf') | |
const questions = await new Question(dbName) | |
ctx.hbs.questiont = await questions.getById(ctx.params.id) | |
console.log(ctx.hbs) | |
await ctx.render('quizfinal', ctx.hbs) | |
} catch(err) { | |
ctx.hbs.error = err.message | |
await ctx.render('error', ctx.hbs ) | |
} | |
}) | |
//Render the TEACHING CONTENT page | |
router.get('/learnGit', async ctx =>{ | |
try{ | |
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/learnGit') | |
const gitselected = await new GitContent(dbName) | |
ctx.hbs.contentdb = await gitselected.all() | |
console.log(ctx.hbs) | |
await ctx.render('teachingcontent', ctx.hbs) | |
} catch(err){ | |
console.log(err.message) | |
ctx.hbs.error = err.message | |
await ctx.render('error', ctx.hbs) | |
} | |
}) | |
//Render for the INDIVIDUAL CONTENT (loop each id) | |
router.get('/learnIndividual/:id', async ctx => { | |
try { | |
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/quizf') | |
const gitselected = await new GitContent(dbName) | |
ctx.hbs.contentdb = await gitselected.getById(ctx.params.id) | |
console.log(ctx.hbs) | |
await ctx.render('contentindividual', ctx.hbs) | |
} catch(err) { | |
ctx.hbs.error = err.message | |
await ctx.render('error', ctx.hbs ) | |
} | |
}) | |
//----------------ROUTER.POST-------------------// | |
//Gets the ANSWER input from the quizpage | |
router.post('/quiz', koaBody, async ctx => { | |
try{ | |
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/quiz') | |
const body = ctx.request.body | |
console.log(body) | |
const answers = await new Answer(dbName) | |
await answers.quiz(body) | |
//we render the async to allow the post to insert | |
return ctx.redirect('/secure?msg=new question answered') | |
} catch(err){ | |
ctx.hbs.error = err.message | |
return ctx.redirect(`/secure?msg=error answering question: ${err.message}`) | |
} | |
}) | |
//Gets the QUESTION input from the quizpage | |
router.post('/questionz', koaBody, async ctx => { | |
try{ | |
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/questionz') | |
const body = ctx.request.body | |
console.log(body) | |
const questions = await new Question(dbName) | |
await questions.questionz(body) | |
return ctx.redirect('/secure?msg=new question added') | |
} catch(err){ | |
ctx.hbs.error = err.message | |
return ctx.redirect(`/secure?msg=error adding a question: ${err.message}`) | |
} | |
}) | |
//Gets the git content input | |
router.post('/gitcontentz', koaBody, async ctx => { | |
try{ | |
if(ctx.hbs.authorised !== true) return ctx.redirect('/login?msg=you need to log in&referrer=/secure/gitcontentz') | |
const body = ctx.request.body | |
console.log(body) | |
const gitcontent = await new GitContent(dbName) | |
await gitcontent.content(body) | |
return ctx.redirect('/secure?msg=new Git content added') | |
} catch(err){ | |
ctx.hbs.error = err.message | |
return ctx.redirect(`/secure?msg=error adding git content: ${err.message}`) | |
} | |
}) | |
//Export the router to be imported somewhere | |
module.exports = router |