galleryApi
parent
879a792907
commit
10b7fc5ee2
|
@ -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;
|
|
@ -1,35 +1,48 @@
|
||||||
const mysql = require("mysql2");
|
var MongoClient = require('mongodb').MongoClient;
|
||||||
|
// const AWS = require('aws-sdk');
|
||||||
|
|
||||||
const generateQuestions = (req, res) => {
|
const saveGameScore = (req, res) => {
|
||||||
const connection = mysql.createConnection({
|
const url = process.env.MONGODB_URL;
|
||||||
host: process.env.MARIA_HOST,
|
const dbName = process.env.MONGO_DB_NAME;
|
||||||
user: process.env.MARIA_USER,
|
const client = new MongoClient(url, { useUnifiedTopology: true });
|
||||||
password: process.env.MARIA_PASS,
|
client.connect((err) => {
|
||||||
database: process.env.MARIA_DBNM
|
|
||||||
});
|
|
||||||
|
|
||||||
connection.connect((err) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Error connecting to the database:', err);
|
console.error('Failed to connect to the server', err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log('Connected to the MariaDB database.');
|
// 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
|
||||||
|
};
|
||||||
|
|
||||||
const { questions } = req.body;
|
collection.insertOne(data, (err, result) => {
|
||||||
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) {
|
if (err) {
|
||||||
console.error('Error inserting question:', err);
|
console.error('Failed to insert document', err);
|
||||||
res.status(500).send('Error inserting question');
|
} else {
|
||||||
return;
|
// 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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = saveGameScore;
|
||||||
|
|
||||||
res.status(200).send('Questions added successfully');
|
|
||||||
}
|
|
||||||
module.exports = generateQuestions;
|
|
Loading…
Reference in New Issue