-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73a4db6
commit 7c3fe4a
Showing
4 changed files
with
85 additions
and
28 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const db = require("../utils/database.js"); | ||
|
||
class User { | ||
constructor(username, roomId, socketId) { | ||
this.username = username; | ||
this.roomId = roomId; | ||
this.socketId = socketId; | ||
this.type = "WAITING"; | ||
} | ||
//------------------------------------------------------------------------------------------------------- | ||
async CreateUser() { | ||
try { | ||
const numberOfUsers = await this.getUser(this.username, this.roomId); | ||
if (numberOfUsers !== 0) { | ||
return JSON.parse({ | ||
message: | ||
"Please change the username as the same username is in the room", | ||
}); | ||
} else { | ||
const query = `INSERT INTO users VALUES ( '${this.username}', '${this.roomId}' , 'WAITING' , '${this.socketId}' )`; | ||
await db.execute(query); | ||
return this; | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
//------------------------------------------------------------------------------------------------------- | ||
async getUser(username, roomId) { | ||
const query = ` SELECT count(*) as cnt FROM users WHERE users.username = ? AND users.room_id = ?;`; | ||
const [rows, fields] = await db.execute(query, [username, roomId]); | ||
console.log(rows[0].cnt); | ||
return rows[0].cnt; | ||
} | ||
//------------------------------------------------------------------------------------------------------- | ||
static async getUserBySocketId(socketId) { | ||
const query = ` SELECT * | ||
FROM users | ||
WHERE socket_id = '${socketId}' | ||
`; | ||
return await db.execute(query); | ||
} | ||
//------------------------------------------------------------------------------------------------------- | ||
static async getSpecificUsers(roomId, type) { | ||
const query = ` SELECT * | ||
FROM users | ||
WHERE room_id = '${roomId}' AND | ||
type = '${type}' | ||
`; | ||
return await db.execute(query); | ||
} | ||
//------------------------------------------------------------------------------------------------------- | ||
static async getCnt(roomId, type) { | ||
const query = ` SELECT COUNT(*) as cnt | ||
FROM USERS | ||
WHERE room_id='${roomId}' and type ='${type}' | ||
`; | ||
return await db.execute(query); | ||
} | ||
//------------------------------------------------------------------------------------------------------- | ||
static async updateUserType(username,roomId, type) { | ||
const query = `UPDATE USERS SET type='${type}' WHERE username= '${username}' AND room_id='${roomId}'`; | ||
return await db.execute(query); | ||
} | ||
|
||
} | ||
|
||
module.exports = User; |
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