Skip to content
Permalink
master
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
#!/usr/bin/env node
//Routes File
'use strict'
/* MODULE IMPORTS */
const Koa = require('koa')
const Router = require('koa-router')
const views = require('koa-views')
const staticDir = require('koa-static')
const bodyParser = require('koa-bodyparser')
const koaBody = require('koa-body')({multipart: true, uploadDir: '.'})
const session = require('koa-session')
//const jimp = require('jimp')
//
const stat = require('koa-static')
const Database = require('sqlite-async')
const handlebars = require('koa-hbs-renderer')
//
/* IMPORT CUSTOM MODULES */
const User = require('./modules/user')
const app = new Koa()
const router = new Router()
/* CONFIGURING THE MIDDLEWARE */
app.keys = ['darkSecret']
app.use(staticDir('public'))
app.use(bodyParser())
app.use(session(app))
app.use(views(`${__dirname}/views`, { extension: 'handlebars' }, {map: { handlebars: 'handlebars' }}))
const defaultPort = 8080
const port = process.env.PORT || defaultPort
const dbName = 'website.db'
let userGlobalVar
let userGlobalPath
/**
* The secure home page.
*
* @name Home Page
* @route {GET} /
* @authentication This route requires cookie-based authentication.
*/
router.get('/default', async ctx => {
const data = {}
await ctx.render('default')
})
router.get('/', async ctx => {
try {
// console.log("________")
// console.log(ctx.session.authorised)
// console.log("________")
if(ctx.session.authorised !== true) {
return ctx.redirect('/default')
}
const data = {}
if(ctx.query.msg) data.msg = ctx.query.msg
await ctx.render('index')
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
/**
* The user registration page.
*
* @name Register Page
* @route {GET} /register
*/
router.get('/register', async ctx => await ctx.render('register'))
/**
* The script to process new user registrations.
*
* @name Register Script
* @route {POST} /register
*/
router.post('/register', koaBody, async ctx => {
try {
// extract the data from the request
const body = ctx.request.body
// console.log(body)
const {path, type} = ctx.request.files.avatar
// console.log('---------------')
// console.log({path, type})
// call the functions in the module
const user = await new User(dbName)
await user.register(body.user, body.pass)
await user.uploadPicture(body.user, path, type)
// redirect to the home page
ctx.redirect(`/?msg=new user "${body.name}" added`)
} catch(err) {
// console.log(err)
await ctx.render('error', {message: err.message})
}
})
router.get('/login', async ctx => {
const data = {}
if(ctx.query.msg) data.msg = ctx.query.msg
if(ctx.query.user) data.user = ctx.query.user
await ctx.render('login', data)
})
let adminPriv
router.post('/login', async ctx => {
try {
const body = ctx.request.body
const user = await new User(dbName)
await user.login(body.user, body.pass)
ctx.session.authorised = true
userGlobalVar = body.user
function isAdmin() {
console.log(userGlobalVar)
if (userGlobalVar === 'master') {
return true
} else {
return false
}
}
adminPriv = isAdmin()
return ctx.redirect('/?msg=you are now logged in...')
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/about', async ctx => {
const data = {}
await ctx.render('about')
})
router.get('/aboutWhileLoggedIn', async ctx => {
const data = {}
await ctx.render('aboutWhileLoggedIn')
})
router.get('/logout', async ctx => {
// console.log("logout"+userGlobalVar)
ctx.session.authorised = null
ctx.redirect('/?msg=you are now logged out')
})
//REROUTES CAUSE HANDLEBARS CANT HANDLE LINKS
// router.get('/html5Course', async ctx => {
// const data = {}
// await ctx.render('html5Course')
// })
// router.get('/gitCourse', async ctx => {
// const data = {}
// await ctx.render('gitCourse')
// })
// router.get('/pythonCourse', async ctx => {
// const data = {}
// await ctx.render('pythonCourse')
// })
router.get('/myProfile', async ctx => {
const data = {
imageSource: `avatars/${userGlobalVar}.jpeg`,
adminPriv
}
if(ctx.query.msg) data.msg = ctx.query.msg
if(ctx.query.user) data.user = ctx.query.user
await ctx.render('myProfile', data)
})
router.get('/firstPforQuiz', async ctx => {
const data = {}
await ctx.render('test/firstPforQuiz')
})
router.post('/firstPforQuiz', async ctx => {
try {
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/changePassword', async ctx => {
const data = {}
await ctx.render('changePassword')
})
router.post('/changePassword', async ctx => {
try {
const body = ctx.request.body
// console.log(body)
const user = await new User(dbName)
await user.changePassword(userGlobalVar,body.oldPass, body.newPass)
ctx.redirect('/myProfile')
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/changeProfilePicture', async ctx => {
const data = {}
await ctx.render('changeProfilePicture')
})
router.post('/changeProfilePicture', koaBody, async ctx => {
try {
const body = ctx.request.body
// console.log(body)
// console.log('---------------1111111111')
const {path, type} = ctx.request.files.avatar
// console.log({path, type})
// console.log('---------------222222222')
const user = await new User(dbName)
await user.changeProfilePicture(userGlobalVar,path,type) //body.avatar,
// userGlobalVar consists the name of username
// console.log('this is user name:')
// console.log(userGlobalVar)
ctx.redirect('/myProfile')
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/addCourse', async ctx => {
const data = {}
await ctx.render('addCourse')
})
router.post('/addCourse', koaBody, async ctx => {
try {
const body = ctx.request.body
// console.log(body)
const {path, type} = ctx.request.files.avatar
// console.log('---------------')
// console.log({path, type})
//user.
const course = await new User(dbName)
await course.saveCourse(body.name, body.courseDescription, body.content, path, type)
ctx.redirect(`/?msg=new course "${body.name}" added`)
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/courses', async ctx => {
try {
const course = await new User(dbName)
const sql = await course.showCourses()
const db = await Database.open(dbName)
const data = await db.all(sql)
await db.close()
// console.log(data)
await ctx.render('courses', {name: 'courses', courses: data})
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/courses/python', async ctx => {
try {
const course = await new User(dbName)
const sql = await course.showCoursesPython()
const db = await Database.open(dbName)
const data = await db.all(sql)
await db.close()
// console.log(data)
await ctx.render('coursesPython', {name: 'coursesPython', coursesPython: data})
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/courses/html', async ctx => {
try {
const course = await new User(dbName)
const sql = await course.showCoursesHTML()
const db = await Database.open(dbName)
const data = await db.all(sql)
await db.close()
// console.log(data)
await ctx.render('coursesHtml', {name: 'coursesHtml', coursesHtml: data})
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/courses/git', async ctx => {
try {
const course = await new User(dbName)
const sql = await course.showCoursesGit()
const db = await Database.open(dbName)
const data = await db.all(sql)
await db.close()
// console.log(data)
await ctx.render('coursesGit', {name: 'coursesGit', coursesGit: data})
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
//.
router.get('/viewResults', async ctx => {
try{
const results = await new User(dbName)
const sql = await results.showResults(userGlobalVar)
const db = await Database.open(dbName)
const data = await db.all(sql)
await db.close()
// console.log(data)
await ctx.render('viewResults', {name: 'viewResults', viewResults: data})
}catch(err) {
await ctx.render('error', {message: err.message})
}
})
//.
router.get('/content/:id', async ctx => {
try {
// console.log(ctx.params.id)// displayed in terminal
const course = await new User(dbName)
const sql = `SELECT * FROM courses WHERE id = ${ctx.params.id};`
const sql2 = await course.showSpecificCourse(sql)
const db = await Database.open(dbName)
const data = await db.get(sql2)
await db.close()
// console.log(data)
await ctx.render('details', data)
} catch(err) {
ctx.body = err.message
}
})
router.get('/addQuestion', async ctx => {
const data = {}
await ctx.render('addQuestion')
})
router.post('/addQuestion', koaBody, async ctx => {
try {
const body = ctx.request.body
// console.log(body)
const course = await new User(dbName)
await course.saveQuestion(body.name, body.courseDescription, body.question, body.answer)
ctx.redirect(`/?msg=new question "${body.name}" added`)
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/quizGit', async ctx => await ctx.render('quizGit'))
router.get('/quizHtml5', async ctx => await ctx.render('quizHtml5'))
router.get('/quizPython', async ctx => await ctx.render('quizPython'))
app.use(router.routes())
module.exports = app.listen(port, async() => console.log(`listening on port ${port}`))