From a8fdbf4ca94f3887dc2aaf7ea5cc6b79fbaa3a56 Mon Sep 17 00:00:00 2001 From: Kieran Dhir Date: Thu, 24 Oct 2019 10:06:57 +0100 Subject: [PATCH] First commit Just started the assignment Adding in question module which allows searching for questions on homepage --- core/controllers/questionController.js | 12 ---- core/controllers/userController.js | 13 ---- core/models/question.js | 24 +++++++ core/routes.js | 36 ++++++++++ core/views/createquestion.hbs | 18 +++++ core/views/home.hbs | 15 +++++ core/views/partials/header.hbs | 14 ++++ public/css/homepage.css | 91 ++++++++++++++++++++++++++ server.js | 10 +++ 9 files changed, 208 insertions(+), 25 deletions(-) delete mode 100644 core/controllers/questionController.js delete mode 100644 core/controllers/userController.js create mode 100644 core/views/createquestion.hbs create mode 100644 public/css/homepage.css diff --git a/core/controllers/questionController.js b/core/controllers/questionController.js deleted file mode 100644 index e571b45..0000000 --- a/core/controllers/questionController.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict' - -const Question = require('../models/question') - -module.exports = class QuestionController { - - static showExample() { - const question = new Question() - console.log(question.example()) - return question.example() - } -} diff --git a/core/controllers/userController.js b/core/controllers/userController.js deleted file mode 100644 index f7d93a1..0000000 --- a/core/controllers/userController.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict' - -const User = require('../models/user') - -module.exports = class UserController { - static hello() { - const user = new User() - user.setName('Ben') - - return `Hello ${user.name}` - } - -} diff --git a/core/models/question.js b/core/models/question.js index 71d7e0c..7e8ae83 100644 --- a/core/models/question.js +++ b/core/models/question.js @@ -1,7 +1,31 @@ 'use strict' +const Database = require('sqlite-async') + +const dbName = 'server.db' + module.exports = class Question { example() { return 'this is an example' } + + async getAllQuestions(query) { + try { + let sql = 'SELECT id, title, question FROM Questions;' + console.log(query.search) + if(query !== undefined && query.search !== undefined) { + sql = `SELECT id, title, question FROM Questions + WHERE upper(title) LIKE "%${query.search}%";` + } + const db = await Database.open(dbName) + const data = await db.all(sql) + await db.close() + console.log(data) + return data + } catch(err) { + return err.message + } + } + + } diff --git a/core/routes.js b/core/routes.js index 57510d1..cf25969 100644 --- a/core/routes.js +++ b/core/routes.js @@ -1,8 +1,44 @@ 'use strict' const Router = require('koa-router') +const Question = require('./models/question') + +//temp +const Database = require('sqlite-async') +const handlebars = require('koa-hbs-renderer') +const dbName = 'server.db' +// + const router = new Router() +const question = new Question() +/* router.get('/', async ctx => await ctx.render('home', {title: 'Home'})) +*/ + +//temp +router.get('/', async ctx => { + const data = await question.getAllQuestions(ctx.query) + console.log(data) + await ctx.render('home', {Questions: data,title: 'Home'}) +}) + +router.post('/insertquestion', async ctx => { + try { + console.log(ctx.request.body) + const body = ctx.request.body + const sql = `INSERT INTO Questions(title, question) + VALUES("${body.title}", "${body.question}");` + console.log(sql) + const db = await Database.open(dbName) + await db.run(sql) + await db.close() + ctx.redirect('/') + } catch(err) { + ctx.body = err.message + } +}) + +router.get('/createquestion', async ctx => ctx.render('createquestion')) module.exports = router diff --git a/core/views/createquestion.hbs b/core/views/createquestion.hbs new file mode 100644 index 0000000..c00607b --- /dev/null +++ b/core/views/createquestion.hbs @@ -0,0 +1,18 @@ +{{> header}} + +
+
+

CONTENT WILL BE HERE

+ +
+ +
+ + +

+
+ +
+
+ +{{> footer }} \ No newline at end of file diff --git a/core/views/home.hbs b/core/views/home.hbs index aad3685..56776f2 100644 --- a/core/views/home.hbs +++ b/core/views/home.hbs @@ -3,6 +3,21 @@

CONTENT WILL BE HERE

+ +
+
+

All Questions

+ +
+ {{#each Questions}} +
+ +

{{this.title}}

+

{{this.question}}

+
+ {{/each}} +
+
diff --git a/core/views/partials/header.hbs b/core/views/partials/header.hbs index e56bb58..b97a431 100644 --- a/core/views/partials/header.hbs +++ b/core/views/partials/header.hbs @@ -5,8 +5,22 @@ {{ title }} + +

HEADER AND NAVIGATION WILL GO HERE

+ + +
\ No newline at end of file diff --git a/public/css/homepage.css b/public/css/homepage.css new file mode 100644 index 0000000..c89af93 --- /dev/null +++ b/public/css/homepage.css @@ -0,0 +1,91 @@ +header{ + font-family: "Avantgarde, Lucida Grande",Verdana; +} +body{ + font-family: "Avantgarde, Lucida Grande",Verdana; + background-color: lightgrey; +} + +h1{ + font-size: 40px; +} + +.border{ + border-style: solid; + text-align: left; + border-width: 2px; + border-radius: 8px; + margin: auto; + margin-bottom: 10px; + margin-top: 10px; + width:950px; + height: 250px; + padding-left: 30px; + background-color:white; + cursor: pointer; +} + +#topBorder{ + height: 100px; + margin-bottom: 60px; + margin-top: 30px; + cursor: auto; +} + +nav{ + border-style: solid; + border-width: 2px; + border-left: 0px; + border-right: 0px; + width:100%; + height: 51px; + position: fixed; + left:0px; + top:0px; + background-color: #585858; +} + +.toolbar{ + display: block; + border-style: none; + width: 100px; + height: 50px; + background-color:#585858; + text-align: center; + font-weight: bold; + color:white; + font-size: 18px; + cursor: pointer; +} + +.toolbar:hover{ + background-color: black; +} + +.floatRight{ + float: right; +} + +.floatLeft{ + float:left; +} + + +#searchButton { + width: 80px; + padding: 8px; + background: #2196F3; + color: white; + font-size: 17px; + cursor: pointer; + box-sizing: border-box; +} + +#searchBox{ + width: 400px; + padding: 8px; + background-color: lightgrey; + margin-top: 7px; + margin-left: 30px; + box-sizing: border-box; +} \ No newline at end of file diff --git a/server.js b/server.js index d13afb1..4019292 100644 --- a/server.js +++ b/server.js @@ -4,11 +4,21 @@ require('dotenv').config() const Koa = require('koa') const views = require('koa-views') +//temp +const stat = require('koa-static') +const koaBody = require('koa-body') +// + const Router = require('./core/routes') const app = new Koa() const port = process.env.SERVER_PORT +//temp +app.use(stat('public')) +app.use(koaBody()) +// + app.use(views(`${__dirname}/core/views`, { extension: 'hbs',