-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.js
28 lines (21 loc) · 886 Bytes
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const users = []
const addUser = (id, name, room) => {
// const existingUser = users.find(user => user.name.trim().toLowerCase() === name.trim().toLowerCase())
// if (existingUser) return { error: "Username has already been taken" }
// if (!name && !room) return { error: "Username and room are required" }
// if (!name) return { error: "Username is required" }
// if (!room) return { error: "Room is required" }
const user = { id, name, room }
users.push(user)
return { user }
}
const getUser = id => {
let user = users.find(user => user.id == id)
return user
}
const deleteUser = (id) => {
const index = users.findIndex((user) => user.id === id);
if (index !== -1) return users.splice(index, 1)[0];
}
const getUsers = (room) => users.filter(user => user.room === room)
module.exports = { addUser, getUser, deleteUser, getUsers }