Skip to content
Snippets Groups Projects
Commit 8cad032b authored by narindra.rajohnso's avatar narindra.rajohnso
Browse files

add id field in question create

parent 7adb9d9d
Branches
No related tags found
No related merge requests found
...@@ -63,7 +63,7 @@ export class Database{ ...@@ -63,7 +63,7 @@ export class Database{
} }
static async createQuestion(question: string, possibleResponse: string[], correctResponse: string, category: string): Promise<number> { static async createQuestion(question: string, possibleResponse: string[], correctResponse: number, category: string): Promise<number> {
let result: number=-1; let result: number=-1;
await Question.create({ await Question.create({
question: question, question: question,
...@@ -72,7 +72,7 @@ export class Database{ ...@@ -72,7 +72,7 @@ export class Database{
category: category category: category
}).then(question => { }).then(question => {
console.log(`Question "${question.id}" ajouté avec succès !`); console.log(`Question "${question.id}" ajouté avec succès !`);
result= 0; result= question.id;
}).catch(err => { }).catch(err => {
console.log(`Une erreur s'est produite lors de l'ajout de la question: ${err}`) console.log(`Une erreur s'est produite lors de l'ajout de la question: ${err}`)
result= -1; result= -1;
...@@ -126,7 +126,7 @@ export class Database{ ...@@ -126,7 +126,7 @@ export class Database{
} }
static async updateQuestion(id: number, question?: string, possibleResponse?: string[], correctResponse?: string, category? : string){ static async updateQuestion(id: number, question?: string, possibleResponse?: string[], correctResponse?: number, category? : string){
let updatedData: Question=new Question(); let updatedData: Question=new Question();
updatedData.id=id; updatedData.id=id;
if(question !== undefined) updatedData.question = question; if(question !== undefined) updatedData.question = question;
......
No preview for this file type
...@@ -18,7 +18,7 @@ export function initQuestion(sequelize: Sequelize){ ...@@ -18,7 +18,7 @@ export function initQuestion(sequelize: Sequelize){
allowNull: false allowNull: false
}, },
correctResponse: { correctResponse: {
type: DataTypes.STRING, type: DataTypes.INTEGER,
allowNull: false allowNull: false
}, },
category: { category: {
......
...@@ -4,7 +4,7 @@ export class Question extends Model { ...@@ -4,7 +4,7 @@ export class Question extends Model {
public id!: number; public id!: number;
public question!: string; public question!: string;
public possibleResponse!: string; public possibleResponse!: string;
public correctResponse!: string; public correctResponse!: number;
public category!: string; public category!: string;
public readonly createdAt!: Date; public readonly createdAt!: Date;
......
...@@ -38,7 +38,9 @@ router.get('/:admin/list-questions', checkExistingUser, async (req: express.Req ...@@ -38,7 +38,9 @@ router.get('/:admin/list-questions', checkExistingUser, async (req: express.Req
const questions = await Database.getAllQuestions(); const questions = await Database.getAllQuestions();
questions.forEach(q => { questions.forEach(q => {
q.possibleResponse = JSON.parse(q.possibleResponse); q.possibleResponse = JSON.parse(q.possibleResponse);
q.correctResponse=parseInt(String(q.correctResponse));
}); });
res.status(StatusCodes.OK).json({ questions: questions }); res.status(StatusCodes.OK).json({ questions: questions });
} catch (error) { } catch (error) {
...@@ -51,14 +53,15 @@ router.post('/:admin/create-user-account', checkExistingUser, checkUserFields, ...@@ -51,14 +53,15 @@ router.post('/:admin/create-user-account', checkExistingUser, checkUserFields,
}); });
router.post('/:admin/create-question', checkExistingUser, checkQuestionFields,async (req: express.Request, res: express.Response) => { router.post('/:admin/create-question', checkExistingUser, checkQuestionFields,async (req: express.Request, res: express.Response) => {
const data = req.body const data = req.body;
const correctResponse: string = data.possibleResponse[data.correctResponse]; const correctResponse: string = data.possibleResponse[data.correctResponse];
if (correctResponse !== undefined) { if (correctResponse !== undefined) {
try{ try{
const result=await Database.createQuestion(data.question, data.possibleResponse, correctResponse, data.category); const result=await Database.createQuestion(data.question, data.possibleResponse, data.correctResponse, data.category);
if (result === 0) { if (result !== -1) {
res.status(StatusCodes.OK).json({ res.status(StatusCodes.OK).json({
new_question: { new_question: {
id: result,
question: data.question, question: data.question,
possibleResponse: data.possibleResponse, possibleResponse: data.possibleResponse,
correctResponse: correctResponse, correctResponse: correctResponse,
...@@ -113,8 +116,7 @@ router.put('/:admin/update-question', checkExistingUser,checkIdField,async (req: ...@@ -113,8 +116,7 @@ router.put('/:admin/update-question', checkExistingUser,checkIdField,async (req:
const question = await Question.findOne({where: {id}}); const question = await Question.findOne({where: {id}});
if(question){ if(question){
try{ try{
const correctResponse=data.possibleResponse!==undefined?data.possibleResponse[data.correctResponse]:JSON.parse(question.possibleResponse)[data.correctResponse]; const nbQuestionUpdated=await Database.updateQuestion(data.id, data.question, data.possibleResponse, data.correctResponse, data.category);
const nbQuestionUpdated=await Database.updateQuestion(data.id, data.question, data.possibleResponse, correctResponse, data.category);
if(nbQuestionUpdated){ if(nbQuestionUpdated){
res.status(StatusCodes.OK).json({ res.status(StatusCodes.OK).json({
message: `Question ${data.id} updated` message: `Question ${data.id} updated`
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment