Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
40 changes: 21 additions & 19 deletions src/controllers/question_controller.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { Question } from '../database/question.js';

class QuestionController {
static question = new Question();

static index(req, res) {
const alternatives = [
{ id: "a", option: "Texto da alternativa A" },
{ id: "b", option: "Texto da alternativa B" },
{ id: "c", option: "Texto da alternativa C" },
{ id: "d", option: "Texto da alternativa D" },
];

const question = {
alternatives: alternatives,
number: -1,
statement: "Lorem ipsum",
exam: "FUVEST 2030",
};

const allQuestions = [];

for (let i = 0; i < 90; i++) {
allQuestions.push({ ...question, number: i + 1 });
}
const allQuestions = QuestionController.question.findAll(90);

return res.status(200).json({ allQuestions });
}

static correctAnswers(req, res) {
const answersDict = req.body;
const keys = Object.keys(answersDict);
const correctedAnswers = {};
let numberOfCorrectAnswers = 0;

keys.forEach((key) => {
const question = QuestionController.question.findById(key);
correctedAnswers[key] = answersDict[key] === question.correctAnswer;

if (correctedAnswers[key]) numberOfCorrectAnswers++;
});

return res.status(200).json({ numberOfCorrectAnswers, correctedAnswers });
}
}

export default QuestionController;
37 changes: 37 additions & 0 deletions src/database/question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export class Question {
constructor() {
const alternatives = [
{ id: 'a', option: 'Texto da alternativa A' },
{ id: 'b', option: 'Texto da alternativa B' },
{ id: 'c', option: 'Texto da alternativa C' },
{ id: 'd', option: 'Texto da alternativa D' },
];

const question = {
alternatives: alternatives,
number: -1,
statement: 'Lorem ipsum',
exam: 'FUVEST 2030',
id: -1,
correctAnswer: 'c',
};

this.allQuestions = [];

for (let i = 0; i < 90; i++) {
this.allQuestions.push({ ...question, number: i + 1, id: i + 1 });
}
}

findAll(size) {
return this.allQuestions.slice(0, size).map((question) => {
const copiedQuestion = Object.assign({}, question);
delete copiedQuestion.correctAnswer;
return copiedQuestion;
});
}

findById(id) {
return this.allQuestions.find((question) => question.id === parseInt(id));
}
}
1 change: 1 addition & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import UserController from './controllers/user_controller.js';
const router = Router();

router.get('/questions', QuestionController.index);
router.post('/questions/answers', QuestionController.correctAnswers);
router.post('/users', UserController.create);
router.post('/users/login', UserController.login);

Expand Down