retrieving chats
This commit is contained in:
+39
-1
@@ -15,6 +15,17 @@ const getUserFromDB = (userID) => {
|
||||
}
|
||||
}
|
||||
|
||||
const deleteUserFromDB = (userID) => {
|
||||
const index = users.findIndex(item => item.id === userID);
|
||||
if (index !== -1) {
|
||||
users.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
const addUserToDB = (user) => {
|
||||
users.push(user);
|
||||
}
|
||||
|
||||
const getChatFromDB = (firstID, secondID) => {
|
||||
if (!firstID || !secondID) {return false;}
|
||||
|
||||
@@ -29,4 +40,31 @@ const getChatFromDB = (firstID, secondID) => {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {users, chats, getUserFromDB, getChatFromDB}
|
||||
const getUsersChats = (userID) => {
|
||||
if (!userID) {return false;}
|
||||
|
||||
const userChats = chats.filter((chat) => (chat.id1 === userID || chat.id2 === userID));
|
||||
|
||||
if (userChats) {
|
||||
return userChats;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const deleteChatFromDB = (firstID, secondID) => {
|
||||
const index = chats.findIndex(item =>
|
||||
(item.id1 === firstID && item.id2 === secondID) || (item.id1 === secondID && item.id2 === firstID));
|
||||
|
||||
if (index !== -1) {
|
||||
chats.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
const addChatToDB = (chat) => {
|
||||
chats.push(chat);
|
||||
}
|
||||
|
||||
|
||||
module.exports = {users, chats, getUserFromDB, getChatFromDB, addUserToDB,
|
||||
deleteUserFromDB, addChatToDB, deleteChatFromDB, getUsersChats}
|
||||
|
||||
Reference in New Issue
Block a user