Skip to content
Permalink
Browse files
First commit
Just started the assignment

Adding in question module which allows searching for questions on homepage
  • Loading branch information
dhirk committed Oct 24, 2019
1 parent c073d0f commit a8fdbf4ca94f3887dc2aaf7ea5cc6b79fbaa3a56
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 25 deletions.

This file was deleted.

This file was deleted.

@@ -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
}
}


}
@@ -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
@@ -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 }}
@@ -3,6 +3,21 @@
<main>
<section>
<h1>CONTENT WILL BE HERE</h1>

<section>
<section class ="border" id ="topBorder">
<h1>All Questions</h1>

</section>
{{#each Questions}}
<form action = "/question/{{id}}" class = "border">
<button>TEST </button>
<h2>{{this.title}}</h2>
<p>{{this.question}}</p>
</form>
{{/each}}
</section>

</section>
</main>

@@ -5,8 +5,22 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{{ title }}</title>
<link href ="css/homepage.css" type ="text/css" rel = "stylesheet"/>
</head>

<body>
<header>
<h1>HEADER AND NAVIGATION WILL GO HERE</h1>

<nav>
<form action = "/"><input class = "toolbar floatLeft" type="submit" value="Home"></form>
<form action = "/createquestion"><input class = "toolbar floatLeft" type="submit" value="Add"></form>
<form action = "/"><input class = "toolbar floatRight" type="submit" value="Sign up"></form>
<form action = "/"><input class = "toolbar floatRight" type="submit" value="Login"></form>
<form action="/" method="get">
<input id = "searchBox" type="text" name="search" placeholder="Search" value="{{query}}">
<input id = "searchButton" type="submit" value="Search">
</form>
</nav>

</header>
@@ -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;
}
@@ -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',

0 comments on commit a8fdbf4

Please sign in to comment.