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 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;
|
||||
};
|
||||
|
||||
module.exports = saveGameScore;
|
||||
|
Loading…
Reference in New Issue