forked from reisborw/FAQ
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
First commit
Just started the assignment Adding in question module which allows searching for questions on homepage
- Loading branch information
Showing
9 changed files
with
208 additions
and
25 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
|
||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{{> header}} | ||
|
||
<main> | ||
<section> | ||
<h1>CONTENT WILL BE HERE</h1> | ||
|
||
<form action ="insertquestion" method = "POST"> | ||
<label>Question Title:<br></label> | ||
<input id = "qtitle" type="text" placeholder = "Input title..." maxlength="100" name="title"><br> | ||
<label>Question:<br></label> | ||
<textarea id = "question" type="text" placeholder = "Input question..." maxlength="1500" name="question"></textarea> | ||
<p><input type="submit" value="Create Question"></p> | ||
</form> | ||
|
||
</section> | ||
</main> | ||
|
||
{{> footer }} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
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