Skip to content
This repository was archived by the owner on Feb 8, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/controllers/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Question from '../models/Question'
import Comment from '../models/Comment'

export async function addComment(ctx) {
const { qno } = ctx.params
const { user } = ctx.state
const question = await Question.findOne({
where: { qno },
})
const questionid = question.id
const userid = user.id
await Comment.create({ questionId: questionid, userId: userid, comment: ctx.request.body.comment})
ctx.body = { response: ctx.request.body.comment }
}
export async function getAll(ctx) {
const { qno } = ctx.params
const question = await Question.findOne({
where: { qno },
})
const qid = question.id
const comment = await Comment.findAll({
where: {
questionId: qid
},
})
ctx.body = { response: comment }
}
30 changes: 18 additions & 12 deletions src/controllers/question.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Question from '../models/Question'
import UserAnswer from '../models/UserAnswer'
import Comment from '../models/Comment'

export async function get(ctx) {
const { qno } = ctx.params
Expand All @@ -11,24 +10,40 @@ export async function get(ctx) {
}

export async function checkAnswer(ctx) {
//var wrongAttempts=0
var correctAttempts=0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use let instead of var

const { qno } = ctx.params
const question = await Question.findOne({
where: { qno },
})
ctx.body = ctx.request.body;
const {user} = ctx.state
const preAnswered = await UserAnswer.findAll({
where:{
questionId: question.id,
userId: user.id
},
})
for(var i in preAnswered){
if (preAnswered[i].useranswer in JSON.parse(question.answer))
Copy link
Member

@ironmaniiith ironmaniiith Sep 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Store the output of JSON.parse(question.answer) in a variable outside the for loop and use it wherever required. It will avoid recomputing it everytime.

correctAttempts+=1
// else{
// wrongAttempts+=1
// }
}
await UserAnswer.create(
{ questionId: question.id, userId: user.id, useranswer: ctx.body.answer}
)
if (ctx.body.answer in JSON.parse(question.answer)) {
if (correctAttempts==0 && (ctx.body.answer in JSON.parse(question.answer))) {
const { user } = ctx.state
if (user.maxUnlock == qno) {
user.maxUnlock += 1
user.score += 20
user.score += question.score
await user.save()
}
ctx.body = { response: true }
}
else if(correctAttempts==1) ctx.body = { response: "Already attempted" }
else ctx.body = { response: false }

}
Expand All @@ -38,12 +53,3 @@ export async function getAll(ctx) {
})
// XXX: Include only question number and title and nothing else
}
export async function addcomment(ctx) {
const { qno } = ctx.params
const { user } = ctx.state
const question = await Question.findOne({
where: { qno },
})
await Comment.create({ qid: question.id, uid: user.id, comment: ctx.request.body.comment})
ctx.body = { response: ctx.request.body.comment }
}
7 changes: 7 additions & 0 deletions src/models/Question.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ const Question = db.define('question', {
notEmpty: true,
}
},
score: {
type: Sequelize.INTEGER,
allowNull: false,
validate: {
notEmpty: true,
}
},
});

export default Question
10 changes: 5 additions & 5 deletions src/models/setupModels.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export default async function setupModels() {
]);
await Question.sync({ force: true })
await Question.bulkCreate([
{ qno: 1, title: 'QTitle 1', body: 'QBody 1', answer: JSON.stringify(['1','2','3'])},
{ qno: 2, title: 'QTitle 2', body: 'QBody 2', answer: JSON.stringify(['2'])},
{ qno: 3, title: 'QTitle 3', body: 'QBody 3', answer: JSON.stringify(['1','4'])},
{ qno: 4, title: 'QTitle 4', body: 'QBody 4', answer: JSON.stringify(['3'])},
{ qno: 5, title: 'QTitle 5', body: 'QBody 5', answer: JSON.stringify(['4'])},
{ qno: 1, title: 'QTitle 1', body: 'QBody 1', answer: JSON.stringify(['1','2','3']), score: 30},
{ qno: 2, title: 'QTitle 2', body: 'QBody 2', answer: JSON.stringify(['2']), score: 30},
{ qno: 3, title: 'QTitle 3', body: 'QBody 3', answer: JSON.stringify(['1','4']), score: 90},
{ qno: 4, title: 'QTitle 4', body: 'QBody 4', answer: JSON.stringify(['3']), score: 39},
{ qno: 5, title: 'QTitle 5', body: 'QBody 5', answer: JSON.stringify(['4']), score: 80},
]);
await UserAnswer.sync({ force: true })
}
39 changes: 39 additions & 0 deletions src/routes/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import koaRouter from 'koa-joi-router';

import * as ctrlc from '../controllers/comment';

import { isAuthenticated } from '../middleware/auth'

const Joi = koaRouter.Joi;
const router = koaRouter();
router.prefix('/api/comments')

const routes = [
{
method: 'post',
path: '/:qno',
handler: [ isAuthenticated, ctrlc.addComment ],
validate: {
params: {
qno: Joi.number()
},
type: 'form',
body: {
comment: Joi.string(),
}
}
},
{
method: 'get',
path: '/:qno',
handler: [ isAuthenticated, ctrlc.getAll ],
validate: {
params: {
qno: Joi.number()
},
}
},
];

router.route(routes);
export default router
2 changes: 2 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import main from './main'
import user from './user'
import question from './question'
import scoreboard from './scoreboard'
import comment from './comment'

export default compose([
main.middleware(),
user.middleware(),
question.middleware(),
scoreboard.middleware(),
comment.middleware(),
])
16 changes: 1 addition & 15 deletions src/routes/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,7 @@ const routes = [
method: 'get',
path: '/',
handler: [ isAuthenticated, ctrl.getAll ],
},
{
method: 'post',
path: '/:qno',
handler: [ isAuthenticated, ctrl.addcomment ],
validate: {
params: {
qno: Joi.number()
},
type: 'form',
body: {
comment: Joi.string(),
}
}
},
},
];

router.route(routes);
Expand Down