From 10b7fc5ee2588a943e5639a32030508b73922cc1 Mon Sep 17 00:00:00 2001 From: Suvodip Date: Tue, 27 Aug 2024 21:30:12 +0530 Subject: [PATCH] s1 --- src/routes/api/generateQuestions copy.js | 35 ++++++++++++ src/routes/api/generateQuestions.js | 71 ++++++++++++++---------- 2 files changed, 77 insertions(+), 29 deletions(-) create mode 100644 src/routes/api/generateQuestions copy.js diff --git a/src/routes/api/generateQuestions copy.js b/src/routes/api/generateQuestions copy.js new file mode 100644 index 0000000..f893c56 --- /dev/null +++ b/src/routes/api/generateQuestions copy.js @@ -0,0 +1,35 @@ +const mysql = require("mysql2"); + +const generateQuestions = (req, res) => { + const connection = mysql.createConnection({ + host: process.env.MARIA_HOST, + user: process.env.MARIA_USER, + password: process.env.MARIA_PASS, + database: process.env.MARIA_DBNM + }); + + connection.connect((err) => { + if (err) { + console.error('Error connecting to the database:', err); + return; + } + console.log('Connected to the MariaDB database.'); + }); + + const { questions } = req.body; + res.send( questions ); + questions.forEach(question => { + const { questionText, options, correctAnswer } = question; + const sql = 'INSERT INTO quiz_questions (questionText, option1, option2, option3, option4, correctAnswer) VALUES (?, ?, ?, ?, ?, ?)'; + db.query(sql, [questionText, options[0], options[1], options[2], options[3], correctAnswer], (err, result) => { + if (err) { + console.error('Error inserting question:', err); + res.status(500).send('Error inserting question'); + return; + } + }); + }); + + res.status(200).send('Questions added successfully'); +} +module.exports = generateQuestions; \ No newline at end of file diff --git a/src/routes/api/generateQuestions.js b/src/routes/api/generateQuestions.js index f893c56..1ed5a38 100644 --- a/src/routes/api/generateQuestions.js +++ b/src/routes/api/generateQuestions.js @@ -1,35 +1,48 @@ -const mysql = require("mysql2"); +var MongoClient = require('mongodb').MongoClient; +// const AWS = require('aws-sdk'); -const generateQuestions = (req, res) => { - const connection = mysql.createConnection({ - host: process.env.MARIA_HOST, - user: process.env.MARIA_USER, - password: process.env.MARIA_PASS, - database: process.env.MARIA_DBNM - }); +const saveGameScore = (req, res) => { + const url = process.env.MONGODB_URL; + const dbName = process.env.MONGO_DB_NAME; + const client = new MongoClient(url, { useUnifiedTopology: true }); + client.connect((err) => { + if (err) { + console.error('Failed to connect to the server', err); + return; + } + // console.log('Connected successfully to server'); + const db = client.db(dbName); + const collection = db.collection('questions'); + // const data = req.body; + const { question, option1, option2, option3, option4, correctAnswer, moduleId } = req.body; + const data = { + question: question, + option1: option1, + option2: option2, + option3: option3, + option4: option4, + correctAnswer: correctAnswer, + moduleId: moduleId + }; - connection.connect((err) => { - if (err) { - console.error('Error connecting to the database:', err); - return; - } - console.log('Connected to the MariaDB database.'); - }); - - const { questions } = req.body; - res.send( questions ); - questions.forEach(question => { - const { questionText, options, correctAnswer } = question; - const sql = 'INSERT INTO quiz_questions (questionText, option1, option2, option3, option4, correctAnswer) VALUES (?, ?, ?, ?, ?, ?)'; - db.query(sql, [questionText, options[0], options[1], options[2], options[3], correctAnswer], (err, result) => { + collection.insertOne(data, (err, result) => { if (err) { - console.error('Error inserting question:', err); - res.status(500).send('Error inserting question'); - return; + console.error('Failed to insert document', err); + } else { + // console.log('Document inserted with _id: ', result.insertedId); } + client.close((err) => { + if (err) { + console.error('Failed to close connection', err); + } else { + console.log('Connection closed'); + + res.send(result.insertedId); + } + }); }); }); - - res.status(200).send('Questions added successfully'); -} -module.exports = generateQuestions; \ No newline at end of file + }; + + module.exports = saveGameScore; + \ No newline at end of file