mongoose + tests

This commit is contained in:
Primakov Alexandr Alexandrovich
2024-10-16 11:06:23 +03:00
parent 2cfcd7347b
commit 4b0d9b4dbc
1295 changed files with 4579 additions and 1719 deletions

View File

@@ -1,72 +1,72 @@
// Read already defined users (pseudo-DB)
const users = require('./auth/users.json');
const chats = require('./chat/chats.json');
const users = require('./auth/users.json')
const chats = require('./chat/chats.json')
const getUserFromDB = (userID) => {
if (!userID) {return false;}
if (!userID) {return false}
// Accessing 'DB'
const user = users.find((user) => user.id === userID);
const user = users.find((user) => user.id === userID)
if (user) {
return user;
return user
} else {
return false;
return false
}
}
const deleteUserFromDB = (userID) => {
const index = users.findIndex(item => item.id === userID);
const index = users.findIndex(item => item.id === userID)
if (index !== -1) {
users.splice(index, 1);
users.splice(index, 1)
}
}
const addUserToDB = (user) => {
users.push(user);
users.push(user)
}
const getChatFromDB = (firstID, secondID) => {
if (!firstID || !secondID) {return false;}
if (!firstID || !secondID) {return false}
// Accessing 'DB'
const chat = chats.find((item) =>
(item.id1 === firstID && item.id2 === secondID) || (item.id1 === secondID && item.id2 === firstID));
(item.id1 === firstID && item.id2 === secondID) || (item.id1 === secondID && item.id2 === firstID))
if (chat) {
return chat;
return chat
} else {
return false;
return false
}
}
const getUsersChats = (userID) => {
if (!userID) {return false;}
if (!userID) {return false}
const userChats = chats.filter((chat) => (chat.id1 === userID || chat.id2 === userID));
const userChats = chats.filter((chat) => (chat.id1 === userID || chat.id2 === userID))
if (userChats) {
return userChats;
return userChats
} else {
return false;
return false
}
}
const addMessageToChat = (chat, msg) => {
chat.messages.push(msg);
chat.messages.push(msg)
}
const deleteChatFromDB = (firstID, secondID) => {
const index = chats.findIndex(item =>
(item.id1 === firstID && item.id2 === secondID) || (item.id1 === secondID && item.id2 === firstID));
(item.id1 === firstID && item.id2 === secondID) || (item.id1 === secondID && item.id2 === firstID))
if (index !== -1) {
chats.splice(index, 1);
chats.splice(index, 1)
}
}
const addChatToDB = (chat) => {
chats.push(chat);
chats.push(chat)
}