-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from CoderSwarup/userduplication
fix #40 Users Can Join Room with Duplicate Name
- Loading branch information
Showing
4 changed files
with
99 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
// first we create an empty array of users | ||
const users=[]; | ||
|
||
// first we create an empty array of users | ||
const users = []; | ||
|
||
// add user into the users array | ||
const addUser=({id,username,room})=>{ | ||
|
||
// clean the data | ||
username=username.trim().toLowerCase(); | ||
room=room.trim().toLowerCase(); | ||
|
||
// Validate the data | ||
if (!username || !room) { | ||
return { | ||
error: 'Username and room are required!' | ||
} | ||
} | ||
// check for existing User | ||
const existingUser=users.find((user)=>{ | ||
return user.room===room && user.username===username; | ||
}) | ||
|
||
// validate | ||
if(existingUser) | ||
{ | ||
return{ | ||
error:'UserName is in Used' | ||
} | ||
} | ||
|
||
// push into the array | ||
const user={id,username,room}; | ||
users.push(user); | ||
return {user} | ||
|
||
} | ||
|
||
// remove user by id | ||
const removeUser=(id)=>{ | ||
const index=users.findIndex((user)=>user.id===id) | ||
|
||
if(index!==-1) | ||
{ | ||
return users.splice(index,1)[0] | ||
} | ||
} | ||
|
||
// getuser by id | ||
const getUser=(id)=>{ | ||
return users.find((user)=>user.id===id) | ||
} | ||
const addUser = ({ id, username, room }) => { | ||
// clean the data | ||
username = username.trim().toLowerCase(); | ||
room = room.trim().toLowerCase(); | ||
|
||
// Validate the data | ||
if (!username || !room) { | ||
return { | ||
status: false, | ||
error: "Username and room are required!", | ||
user: {}, | ||
}; | ||
} | ||
|
||
// check for existing User | ||
|
||
const existingUser = users?.find((user) => { | ||
if (user?.room === room && user?.username === username) return true; | ||
else return false; | ||
}); | ||
|
||
// validate | ||
if (existingUser) { | ||
return { | ||
status: false, | ||
error: "UserName is in Used", | ||
user: {}, | ||
}; | ||
} | ||
|
||
// push into the array | ||
const user = { id, username, room }; | ||
users.push(user); | ||
return { status: true, error: "", user }; | ||
}; | ||
|
||
// remove user by id | ||
const removeUser = (id) => { | ||
const index = users.findIndex((user) => user.id === id); | ||
|
||
if (index !== -1) { | ||
return users.splice(index, 1)[0]; | ||
} | ||
}; | ||
|
||
// getuser by id | ||
const getUser = (id) => { | ||
return users.find((user) => user.id === id); | ||
}; | ||
|
||
// getUsersInRoom by room | ||
|
||
const getUsersInRoom=(room)=>{ | ||
room=room.trim().toLowerCase(); | ||
return users.filter((user)=>user.room===room) | ||
} | ||
|
||
module.exports={addUser,removeUser,getUser,getUsersInRoom}; | ||
|
||
const getUsersInRoom = (room) => { | ||
room = room.trim().toLowerCase(); | ||
return users.filter((user) => user.room === room); | ||
}; | ||
|
||
module.exports = { addUser, removeUser, getUser, getUsersInRoom }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters