Skip to content

Commit

Permalink
backend for view/create chat room
Browse files Browse the repository at this point in the history
  • Loading branch information
effy971 committed Oct 31, 2020
1 parent 5ffda10 commit bf7aa3d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
29 changes: 29 additions & 0 deletions createchatroom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { MongoClient } = require("mongodb");

module.exports = async function (roomId, roomname) {
const uri = process.env.DB_CREDENTIALS;

const client = new MongoClient(uri);
let successOrFail = false;
try {
await client.connect();

const database = client.db("4learn");
const collection = database.collection("Chatroom");

// create js obj received by /register

const doc = { roomId: roomId, chatroomName: roomname };
console.log(roomId);
const result = await collection.insertOne(doc);

console.log(`${result.insertedCount} documents were inserted into db`);
console.log(result.insertedCount > 0);
if (result.insertedCount > 0) {
successOrFail = true;
return true;
}
} finally {
await client.close();
}
};
24 changes: 24 additions & 0 deletions getchatroomlist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { MongoClient } = require("mongodb");

module.exports = async function (roomListArray) {
const uri = process.env.DB_CREDENTIALS;

const client = new MongoClient(uri, { useUnifiedTopology: true });
let successOrFail = false;

try {
await client.connect();

const database = client.db("4learn");
const collection = database.collection("Chatroom");

// create js obj received by /login

const result = await collection.find();
console.log("getting list");
await result.forEach((result) => roomListArray.push(result));
} finally {
await client.close();
return roomListArray;
}
};

0 comments on commit bf7aa3d

Please sign in to comment.