Skip to content
Permalink
4db3e5365e
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
266 lines (227 sloc) 6.29 KB
const Router = require('koa-router')
const koaBody = require('koa-body')({ multipart: true, uploadDir: '.' })
const router = new Router()
const fs = require("fs");
const tracks=require("../Alltracks.json")
const User = require('../modules/user')
const dbName = 'website.db'
const List=require('../modules/list')
const UploadTrack=require('../modules/uploadTrack')
/**
* The secure home page.
*
* @name Home Page
* @route {GET} /
*/
router.get('/', async ctx => {
try {
await ctx.render('index', ctx.hbs)
} catch (err) {
await ctx.render('error')
}
})
/**
* 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 => {
const user = await new User(dbName)
try {
// call the functions in the module
await user.register(ctx.request.body.user, ctx.request.body.pass, ctx.request.body.email)
const day = 86400, milliseconds = 1000
const expiry = Math.floor(Date.now() / milliseconds) + day
//const token = await user.generateToken(ctx.request.body.user, expiry)
const html = `<h2>Paste this url into your browser</h2>\
<p>${ctx.hbs.host}/validate/${ctx.request.body.user}</p>`
//await user.sendEmail(ctx.request.body.email, 'Validate Your Account', html)
ctx.redirect(`/postregister?msg=new user "${ctx.request.body.user}" added`)
} catch (err) {
console.log(err);
ctx.msg = err.message
ctx.body = ctx.request.body
console.log(ctx.hbs)
await ctx.render('register', ctx.hbs)
} finally {
user.tearDown()
}
})
router.get('/postregister', async ctx => await ctx.render('validate'))
router.get('/validate/:user/:token', async ctx => {
try {
console.log('VALIDATE')
console.log(`URL --> ${ctx.request.url}`)
if (!ctx.request.url.includes('.css')) {
console.log(ctx.params)
const milliseconds = 1000
const now = Math.floor(Date.now() / milliseconds)
const user = await new User(dbName)
await user.checkToken(ctx.params.user, ctx.params.token, now)
ctx.hbs.msg = `account "${ctx.params.user}" has been validated`
await ctx.render('login', ctx.hbs)
}
} catch (err) {
await ctx.render('login', ctx.hbs)
}
})
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', koaBody, async ctx => {
const user = await new User(dbName)
ctx.hbs.body = ctx.request.body
try {
const body = ctx.request.body
const res= await user.login(body.user, body.pass)
ctx.session.authorised = true
console.log(res)
ctx.session.user=res
return ctx.redirect('/secure?msg=you are now logged in...',ctx)
} catch (err) {
ctx.hbs.msg = err.message
await ctx.render('login', ctx.hbs)
} finally {
user.tearDown()
}
})
router.get('/logout', async ctx => {
ctx.session.authorised = null
ctx.redirect('/?msg=you are now logged out')
})
router.get('/listTrack',koaBody,async ctx => {
try{
console.log(tracks);
ctx.hbs.record=tracks;
await ctx.render('listTrack', ctx.hbs) }
catch (err) {
console.log(err)
ctx.hbs.msg = err.message
ctx.hbs.body = ctx.request.body
console.log(ctx.hbs)
}
})
router.get('/trackDetails/:track',koaBody,async ctx=>{
try
{
const value=ctx.params.track;
console.log(value)
var arrFound = tracks.filter(function(item) {
return item.Track == value;
});
console.log(arrFound[0].Description)
}
catch(err){ console.log(err)
ctx.hbs.msg = err.message
ctx.hbs.body = ctx.request.body
console.log(ctx.hbs)}
})
router.get('/uploadTrack/:id',koaBody,async ctx=>{
console.log("enter")
const value=ctx.params.id;
console.log(value)
const upload = await new UploadTrack(dbName)
try
{ var arrFound = tracks.filter(function(item) {
return item.Song_id == value;
});
console.log(arrFound[0])
const res= await upload.insert(arrFound[0].Song_id,arrFound[0].Singer_Name , arrFound[0].Singer_details,arrFound[0].description,arrFound[0].Track,arrFound[0].image_url,arrFound[0].trackSource)
console.log('record inserted')
return ctx.redirect('/playlist')
}
catch(err){ console.log(err)
ctx.hbs.msg = err.message
ctx.hbs.body = ctx.request.body
console.log(ctx.hbs)}
})
router.get('/songs/:id',koaBody,async ctx=>{
console.log("play")
await ctx.redirect('playlist',ctx.hbs)
})
router.get('/playlist',koaBody,async ctx=>{
const upload= await new UploadTrack(dbName)
try{
const records= await upload.getAll()
console.log(records)
ctx.hbs.playlist=records
await ctx.render('playlist',ctx.hbs)
}
catch (err) {
console.log(err)
ctx.hbs.msg = err.message
ctx.hbs.body = ctx.request.body
console.log(ctx.hbs)
}finally {
upload.tearDown()
}
})
/*
router.post('/issues', koaBody,async ctx => {
const iss = await new Issues(dbName)
try {
console.log(ctx.request.body.userName)
await iss.insert(ctx.request.body.userName, ctx.request.body.issueList, ctx.request.body.status)
console.log('record inserted')
await ctx.render('issues', ctx.hbs)
} catch (err) {
console.log(err)
ctx.hbs.msg = err.message
ctx.hbs.body = ctx.request.body
console.log(ctx.hbs)
}finally {
iss.tearDown()
}
})
router.get('/issues', koaBody,async ctx => {
const iss = await new Report(dbName)
//const tech= await new Technician(dbName)
try {
const records= await iss.getAll()
// const record=await tech.getTech()
ctx.hbs.record=records
await ctx.render('issues', ctx.hbs)
}catch (err) {
console.log(err)
console.log(ctx.hbs)
}finally {
iss.tearDown()
}
})
router.get('/raiseissue',async ctx =>{
await ctx.render('raiseissue')
})
router.put('/issues', async ctx => {
const iss = await new Issues(dbName)
try{
await iss.update()
}catch(err) {
console.log(err.message)
}finally{
iss.tearDown()
}
})
router.delete('/issues', async ctx => {
const iss = await new Issues(dbName)
try{
await iss.delRecord()
}catch(err) {
console.log(err.message)
}finally{
iss.tearDown()
}
})
})
*/
module.exports = router