Skip to content
Permalink
c0abc7b2b2
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
230 lines (222 sloc) 6.39 KB
#!/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 formData = require('form-data')
//const jimp = require('jimp')
/* IMPORT CUSTOM MODULES */
const User = require('./modules/user')
const gamesMod = require('./modules/games')
const reviews = require('./modules/reviews')
const Contacts = require('./modules/contactpage')
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 database = 'User_database.db'
let admin=0
let username = ''
/**
* The secure home page.
*
* @name Home Page
* @route {GET} /
* @authentication This route requires cookie-based authentication.
*/
router.get('/', async ctx => {
ctx.redirect('/Home')
})
/**
* The user registration page.
*
* @name Register Page
* @route {GET} /register
*/
router.get('/register', async ctx => {
await ctx.render('register')
}
)
router.get('/Home', async ctx => {
await ctx.render('Home',
{logged: ctx.session.authorised})
}) //logged is passed into Home.handelbars and use in if statement for logout option.
router.get('/reviews', async ctx => {
const games = await new gamesMod(database)
const data = await games.getGenreGames('none')
await ctx.render('reviews', {Games: data})
})
router.get('/VReviews', async ctx => await ctx.render('VReviews'))
/**
* The script to process new user registrations.
*
* @name Register Script
* @route {POST} /register
*/
router.post('/register', koaBody, async ctx => {
try {
const body = ctx.request.body
const user = await new User(database)
await user.register(body.user, body.pass, body.email)
ctx.redirect('/Home')
} catch(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)
})
router.post('/login', async ctx => {
try {
const body = ctx.request.body
const user = await new User(database)
const data = await user.login(body.user, body.pass)
admin = data[1] === 1 ? true : false
username = data[0]
console.log(admin)
ctx.session.authorised = true
return ctx.redirect('/Home')
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/logout', async ctx => {
ctx.session.authorised = null
ctx.redirect('/?msg=you are now logged out')
})
router.get('/games/:genre', async ctx => {
try{
const games = await new gamesMod(database)
await games.createGenres()
const data = await games.getGenreGames(ctx.params.genre)
const genres = await games.getGenres()
console.log(data)
await ctx.render('games', {title: 'Games', games: data, admin: admin, genres: genres})
} catch(err) {
ctx.body = err.message
}
})
router.get('/details/:id', async ctx => {
try{
const games = await new gamesMod(database)
const data = await games.getGame(ctx.params.id)
await ctx.render('details', {game: data[0], reviews: data[1], admin: admin})
} catch(err) {
ctx.body = err.message
}
})
router.get('/adminReviews', async ctx => {
try{
if(admin) {
const review = await new reviews(database)
const data = await review.allPending()
console.log(data)
await ctx.render('adminReviews',{reviews: data})
} else{
ctx.body = 'You are not admin'
}
} catch(err) {
ctx.body = err.message
}
})
router.post('/adminReviews', async ctx => {
const review = await new reviews(database)
if(ctx.request.body.submit==='release') {
review.release(ctx.request.body.id)
await ctx.redirect('/adminReviews')
} else if(ctx.request.body.submit==='delete') {
review.delete(ctx.request.body.id)
await ctx.redirect('/adminReviews')
}
})
router.get('/detailedReview/:id', async ctx => {
try{
const review = await new reviews(database)
const data = await review.getReview(ctx.params.id)
await ctx.render('detailedReview', data)
} catch(err) {
ctx.body = err.message
}
})
router.get('/addGame', async ctx => {
try{
const games = await new gamesMod(database)
await games.createGenres()
const genres = await games.getGenres()
console.log(genres)
await ctx.render('addGame', {genres: genres})
} catch(err) {
ctx.body = err.message
}
})
router.post('/addGame', async ctx => {
try{
if(ctx.session.authorised !== true ) return ctx.render('error', {message: 'You are not logged in'})
const games = await new gamesMod(database)
const body = ctx.request.body
console.log(body)
games.addGame({name: body.Name,publisher: body.Publisher, release: body.Release,
description: body.Description, genre: body.Genre})
ctx.redirect('/games/none')
} catch(err) {
ctx.body = err.message
}
})
router.post('/reviews',async ctx => {
try {
const user = await new User(database)
const userID = await user.getUser(username)
if(ctx.session.authorised !== true ) return ctx.render('error', {message: 'You are not logged in'})
const body = ctx.request.body
const ViewR = await new reviews(database)
ViewR.AddReview({
ID: body.ID,
Title: body.Title,
ReviewText: body.ReviewText,
UserRating: body.UserRating,
userID: userID.id})
await ctx.render('Home')
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/VReviews/:id', async ctx => {
try{
const ViewR = await new reviews(database)
const data = await ViewR.getSpecificGame(ctx.params.id)
await ctx.render('VReviews',{reviews: data})
} catch(err) {
ctx.body = err.message
}
})
router.get('/contactpage',async ctx => {
await ctx.render('contactpage')
}
)
router.post('/contactpage',async ctx => {
try {
const body = ctx.request.body
const contact = await new Contacts(database)
await contact.addContact(body.fname, body.lname, body.num, body.email, body.querydetails)
await ctx.render('contactpage')
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
app.use(router.routes())
module.exports = app.listen(port, async() => console.log(`listening on port ${port}`))