Skip to content

Commit

Permalink
Merge pull request #2 from ashishraj09/feature/mongodb-support-for-web
Browse files Browse the repository at this point in the history
Add support for external db
  • Loading branch information
ashishraj09 authored Nov 10, 2024
2 parents b2b3de3 + 12b4b7b commit b366fde
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 40 deletions.
53 changes: 19 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ console.log(`Origin URL: ${originUrl}`);
const mongoUri = isDev ? "mongodb://localhost:27017/iptvnator" : process.env.MONGO_URI || "";
const dbName = isDev ? "iptvnator" : process.env.MONGO_DB_NAME || "";
const collectionName = isDev ? "playlists" : process.env.MONGO_COLLECTION_NAME || "";
let databaseService;

console.log(`dbName: ${dbName}`);
console.log(`mongoUri: ${mongoUri}`);
console.log(`collectionName: ${collectionName}`);

const mongoDBService = new MongoDBService(mongoUri, dbName, collectionName);
if (!mongoUri || !dbName || !collectionName) {
console.log("MongoDB not enabled: Missing configuration variables.");
} else {
console.log(`MongoDB enabled: dbName: ${dbName}, mongoUri: ${mongoUri}, collectionName: ${collectionName}`);
databaseService = new MongoDBService(mongoUri, dbName, collectionName);
}


const corsOptions = {
Expand Down Expand Up @@ -84,27 +86,11 @@ app.get("/xtream", cors(corsOptions), async (req, res) => {
});
});

// New route to check the database connection status
app.get("/check-db-connection", cors(corsOptions), async (req, res) => {

if (!mongoUri || !dbName || !collectionName) {
return res.status(200).send({ status: "error", message: "Database is not enabled" });
}

try {
await dbService.connect();
res.status(200).send({ status: "success", message: "Database is enabled" });
} catch (error) {
console.error('Error connecting to MongoDB:', error);
res.status(200).send({ status: "error", message: "Database is not enabled", error: error.message });
}
});

// New route to add multiple playlists
app.post("/addManyPlaylists", cors(corsOptions), async (req, res) => {
try {
const playlists = req.body;
const result = await mongoDBService.insertMany(playlists);
const result = await databaseService.insertMany(playlists);
res.status(200).send(result);
} catch (error) {
res.status(500).send({ error: 'Error adding multiple playlists to MongoDB' });
Expand All @@ -115,20 +101,19 @@ app.post("/addManyPlaylists", cors(corsOptions), async (req, res) => {
app.post("/addPlaylist", cors(corsOptions), express.json(), async (req, res) => {
const data = req.body;
try {
const result = await mongoDBService.insertData(data);
let insertedData;
insertedData = await mongoDBService.readData({ _id: result.insertedId });
const result = await databaseService.insertData(data);
let insertedData = await databaseService.readData({ _id: result.insertedId });
res.status(200).send(insertedData);
} catch (error) {
console.error('Error inserting data into MongoDB:', error);
res.status(500).send({ error: 'Error inserting data into MongoDB' });
console.error('Error inserting multiple data into MongoDB:', error);
res.status(500).send({ error: 'Error inserting multiple data into MongoDB' });
}
});

app.get("/getPlaylist/:id", cors(corsOptions), async (req, res) => {
try {
const { id } = req.params;
const result = await mongoDBService.readData({ _id: id });
const result = await databaseService.readData({ _id: id });
res.status(200).send(result);
} catch (error) {
res.status(500).send({ error: 'Error reading data from MongoDB' });
Expand All @@ -138,7 +123,7 @@ app.get("/getPlaylist/:id", cors(corsOptions), async (req, res) => {
// Updated route to read all data from MongoDB
app.get("/getAllPlaylists", cors(corsOptions), async (req, res) => {
try {
const result = await mongoDBService.readDataAll(); // No query parameters passed
const result = await databaseService.readDataAll(); // No query parameters passed
res.status(200).send(result);
} catch (error) {
res.status(500).send({ error: 'Error reading data from MongoDB' });
Expand All @@ -149,17 +134,17 @@ app.get("/getAllPlaylists", cors(corsOptions), async (req, res) => {
app.delete("/deletePlaylist/:id", cors(corsOptions), async (req, res) => {
try {
const { id } = req.params;
const result = await mongoDBService.deleteData({ _id: id});
const result = await databaseService.deleteData({ _id: id});
res.status(200).send(result);
} catch (error) {
res.status(500).send({ error: 'Error deleting playlist from MongoDB' });
}
});

// New route to remove all playlists
app.delete("/removeAllPlaylists", cors(corsOptions), async (req, res) => {
app.delete("/deleteAllPlaylists", cors(corsOptions), async (req, res) => {
try {
const result = await mongoDBService.removeAllPlaylists();
const result = await databaseService.deleteAllPlaylists();
res.status(200).send(result);
} catch (error) {
res.status(500).send({ error: 'Error removing all playlists from MongoDB' });
Expand All @@ -171,8 +156,8 @@ app.put("/updatePlaylist/:id", cors(corsOptions), async (req, res) => {
try {
const { id } = req.params;
const updatedPlaylist = req.body;
const result = await mongoDBService.updateData({ _id: id }, updatedPlaylist);
const updatedData = await mongoDBService.readData({ _id: id });
const result = await databaseService.updateData({ _id: id }, updatedPlaylist);
const updatedData = await databaseService.readData({ _id: id });
res.status(200).send(updatedData);
} catch (error) {
res.status(500).send({ error: 'Error updating playlist in MongoDB' });
Expand Down
9 changes: 3 additions & 6 deletions mongo-db.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ class MongoDBService {
this.database = this.client.db(this.dbName);
this.collection = this.database.collection(this.collectionName);
this.isConnected = true;
console.log(`[${new Date().toISOString()}] Connected to MongoDB`);
console.log(`URI: ${this.uri}`);
console.log(`Database: ${this.dbName}`);
console.log(`Collection: ${this.collectionName}`);
console.log(`[${new Date().toISOString()}] Connected to MongoDB: ${this.uri}`);
} catch (error) {
console.error(`[${new Date().toISOString()}] Error connecting to MongoDB:`, error);
}
Expand All @@ -44,7 +41,7 @@ class MongoDBService {
console.log(`[${new Date().toISOString()}] Data inserted:`, result.insertedIds);
return result;
} catch (error) {
console.error(`[${new Date().toISOString()}] Error inserting data into MongoDB:`, error);
console.error(`[${new Date().toISOString()}] Error inserting multiple data into MongoDB:`, error);
}
}

Expand Down Expand Up @@ -81,7 +78,7 @@ class MongoDBService {
}
}

async removeAllPlaylists() {
async deleteAllPlaylists() {
try {
await this.connect();
const result = await this.collection.deleteMany({});
Expand Down

0 comments on commit b366fde

Please sign in to comment.