forked from pedroavpereira/bookswap-api
-
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.
Merge branch 'dev' into Initial-db-setup
- Loading branch information
Showing
4 changed files
with
141 additions
and
0 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,8 @@ | ||
require("dotenv").config(); | ||
const server = require("./websocket"); | ||
|
||
const PORT = process.env.PORT || 3001; | ||
|
||
server.listen(PORT, () => { | ||
console.log(`SERVER RUNNING ON PORT ${PORT}`); | ||
}); |
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,31 @@ | ||
const db = require("../db/connect"); | ||
|
||
class Message { | ||
constructor({ message_id, room_id, user_sent, message, sent_at }) { | ||
this.message_id = message_id; | ||
this.room_id = room_id; | ||
this.user_sent = user_sent; | ||
this.message = message; | ||
this.sent_at = sent_at; | ||
} | ||
|
||
static async getMessages(room_id) { | ||
const response = await db.query( | ||
"SELECT * FROM message WHERE room_id = $1;", | ||
[room_id] | ||
); | ||
|
||
return response.rows.map((msg) => new Message(msg)); | ||
} | ||
|
||
static async createMessage({ room_id, user_sent, message }) { | ||
const response = await db.query( | ||
"INSERT INTO message (room_id, author, message) VALUES ($1, $2, $3);", | ||
[room_id, user_sent, message] | ||
); | ||
|
||
return new Message(response.rows[0]); | ||
} | ||
} | ||
|
||
module.exports = Message; |
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,44 @@ | ||
const db = require("../db/connect"); | ||
|
||
class Room { | ||
constructor({ room_id, user_1, user_2, swap_id }) { | ||
this.room_id = room_id; | ||
this.user_1 = user_1; | ||
this.user_2 = user_2; | ||
this.swap_id = swap_id; | ||
} | ||
|
||
static async getRoom(room_id) { | ||
const response = await db.query("SELECT * FROM rooms WHERE room_id = $1", [ | ||
room_id, | ||
]); | ||
|
||
if (response.rows.length === 0) return null; | ||
|
||
return new Room(response.rows[0]); | ||
} | ||
|
||
static async getRooms(user_id) { | ||
const response = await db.query( | ||
"SELECT * FROM rooms WHERE (user_1 = $1 OR user_2 = $1) AND closed = false ", | ||
[user_id] | ||
); | ||
|
||
if (response.rows.length === 0) return []; | ||
|
||
return new Room(response.rows[0]); | ||
} | ||
|
||
static async createRoom({ user_1, user_2, swap_id }) { | ||
const response = await db.query( | ||
"INSERT into rooms (user_1 , user_2~, swap) VALUES ($1, $2) RETURNING *;", | ||
[user_1, user_2, swap_id] | ||
); | ||
|
||
if (response.rows.length === 0) throw new Error("Error creating room"); | ||
|
||
return new Room(response.rows[0]); | ||
} | ||
} | ||
|
||
module.exports = Room; |
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,58 @@ | ||
const http = require("http"); | ||
const { Server } = require("socket.io"); | ||
|
||
const app = require("./app"); | ||
|
||
const Room = require("./models/Room"); | ||
const Message = require("./models/Message"); | ||
|
||
const server = http.createServer(app); | ||
|
||
const io = new Server(server, { | ||
cors: { | ||
origin: "*", | ||
methods: ["GET", "POST"], | ||
}, | ||
}); | ||
|
||
io.on("connection", async (socket) => { | ||
console.log(`User Connected: ${socket.id}`); | ||
|
||
socket.on("join_room", async ({ room, username }) => { | ||
const roomDB = await Room.getRoom(parseInt(room)); | ||
|
||
if (!roomDB) { | ||
socket.emit("unauthorized"); | ||
socket.disconnect(true); | ||
return; | ||
} | ||
|
||
if (username !== roomDB.username_1 && username !== roomDB.username_2) { | ||
socket.emit("unauthorized"); | ||
socket.disconnect(true); | ||
return; | ||
} | ||
|
||
const messages = await Message.getMessages(room); | ||
socket.join(room); | ||
socket.emit("receive_messages", messages); | ||
|
||
console.log(`User with ID: ${socket.id} joined room: ${room}`); | ||
}); | ||
|
||
socket.on("send_message", async (data) => { | ||
const { room, user_sent, message } = data; | ||
const newMessage = await Message.createMessage({ | ||
room_id: room, | ||
user_sent, | ||
message, | ||
}); | ||
socket.to(room).emit("receive_message", data); | ||
}); | ||
|
||
socket.on("disconnect", () => { | ||
console.log("User Disconnected", socket.id); | ||
}); | ||
}); | ||
|
||
module.exports = server; |