galleryApi
dev2 siliconpin 2024-08-12 19:24:20 +00:00
parent f12e2d9e0a
commit 8b420fb34f
3 changed files with 127 additions and 24 deletions

View File

@ -1,28 +1,38 @@
const getGameScore = (req, res) => { const getGameScore = (req, res) => {
const { MongoClient, ObjectId } = require('mongodb'); const { MongoClient } = require('mongodb');
const { userId, gameName, gameID } = req.body;
const url = process.env.MONGODB_URL;
const dbName = process.env.MONGO_DB_NAME;
let db;
// Connect to MongoDB async function main() {
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => { const uri = 'mongodb://mongoadmin:Bijaynagardwd@10.0.0.90:27017'; // Replace with your MongoDB connection string
if (err) { const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
console.error('Failed to connect to the database');
throw err; try {
} // Connect to the MongoDB client
console.log('Connected to Database2',dbName); await client.connect();
db = client.db(dbName);
}); // Select the database and collection
db.collection('gameData').find({}).toArray((err, results) => { const database = client.db('iimttnewdb'); // Replace with your database name
if (err) { const collection = database.collection('gameData'); // Replace with your collection name
console.error('Error fetching game data:', err);
return res.status(500).json({ message: 'Internal Server Error' }); // Find a single document
const user = await collection.findOne({ userId: 'dsfdfgffgfgeg' });
// Output the result
if (user) {
console.log('User found:', user);
} else {
console.log('No user found with the username "john_doe"');
} }
} catch (error) {
console.error('Error connecting to MongoDB or performing query:', error);
} finally {
// Close the connection
await client.close();
}
}
res.json(results); main().catch(console.error);
});
};
module.exports = getGameScore; };
module.exports = getGameScore;

View File

@ -0,0 +1,43 @@
const getGameScore = (req, res) => {
const { MongoClient, ObjectId } = require('mongodb');
const { userId, gameName, gameID } = req.body;
const url = process.env.MONGODB_URL;
const dbName = process.env.MONGO_DB_NAME;
let db;
// Connect to MongoDB
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
if (err) {
console.error('Failed to connect to the database');
throw err;
}
console.log('Connected to Database2',dbName);
db = client.db(dbName);
databasesList = client.db().iimttnewdb().listDatabases();
console.log("Databases:");
databasesList.databases.forEach(db => console.log(` - ${db.name}`));
});
db.collection('gameData').findOne({ userId: userId, gameName: gameName, gameID: gameID }, (err, gameData) => {
if (err) {
console.error('Error fetching game data:', err);
return res.status(500).json({ message: 'Internal Server Error' });
}
if (gameData) {
res.json({
score: gameData.score.toString(),
gameTime: gameData.gameTime.toFixed(2).toString()
});
} else {
res.status(404).json({ message: 'Game data not found' });
}
});
};
module.exports = getGameScore;

View File

@ -0,0 +1,50 @@
const getGameScore = (req, res) => {
const { MongoClient } = require('mongodb');
// Connection URI to your MongoDB instance
const url = process.env.MONGODB_URL;
const dbName = 'test';
// Create a new MongoClient
const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true });
let db;
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
.then(client => {
console.log('Connected to Database',dbName);
db = client.db(dbName);
})
.catch(error => console.error(error));
// Function to list all databases
async function listDatabases() {
try {
const { userId, gameName, gameID } = req.body;
const gameData = await db.collection('gameData').findOne({
userId: userId,
gameName: gameName,
gameID: gameID
});
if (gameData) {
res.json({
score: gameData.score.toString(),
gameTime: gameData.gameTime.toFixed(2).toString()
});
} else {
res.status(404).json({ message: 'Game data not found' });
}
} catch (error) {
res.status(500).json({ message: 'Internal Server Error' });
console.error(error);
} finally {
// Ensure that the client will close when you finish
await client.close();
}
}
// Call the function
listDatabases();
};
module.exports = getGameScore;