|
| 1 | +import { |
| 2 | + newRoom, |
| 3 | + getRoomId, |
| 4 | + getAllRooms, |
| 5 | + fetchRoomChatHistory, |
| 6 | + getQuestionIdByRoomId, |
| 7 | + getAllRoomsByUserId, |
| 8 | +} from "../model/repository.js"; |
| 9 | +import crypto from "crypto"; |
| 10 | + |
| 11 | +// Create a room between two users |
| 12 | +export async function createRoom(req, res) { |
| 13 | + const { user1, user2, question_id } = req.body; |
| 14 | + |
| 15 | + if (!user1 || !user2 || !question_id) { |
| 16 | + return res |
| 17 | + .status(400) |
| 18 | + .json({ error: "user1,user2 and question_id are required" }); |
| 19 | + } |
| 20 | + |
| 21 | + // Generate a unique room ID by hashing the two user IDs |
| 22 | + const timeSalt = new Date().toISOString().slice(0, 19); |
| 23 | + const roomId = crypto |
| 24 | + .createHash("sha256") |
| 25 | + .update(user1 + user2 + timeSalt) |
| 26 | + .digest("hex"); |
| 27 | + const room = await newRoom(user1, user2, roomId, question_id); |
| 28 | + |
| 29 | + if (room) { |
| 30 | + res.status(201).json(room); |
| 31 | + } else { |
| 32 | + res.status(500).json({ error: "Failed to create room" }); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Get room ID by user |
| 37 | +export async function getRoomByUser(req, res) { |
| 38 | + const { user } = req.params; |
| 39 | + |
| 40 | + if (!user) { |
| 41 | + return res.status(400).json({ error: "User is required" }); |
| 42 | + } |
| 43 | + |
| 44 | + const room = await getRoomId(user); |
| 45 | + |
| 46 | + if (room) { |
| 47 | + res.status(200).json(room); |
| 48 | + } else { |
| 49 | + res.status(404).json({ error: `Room not found for user: ${user}` }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Get all rooms |
| 54 | +export async function getAllRoomsController(req, res) { |
| 55 | + const rooms = await getAllRooms(); |
| 56 | + |
| 57 | + if (rooms) { |
| 58 | + res.status(200).json(rooms); |
| 59 | + } else { |
| 60 | + res.status(500).json({ error: "Failed to retrieve rooms" }); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Get all rooms by user |
| 65 | +export async function getAllRoomsByUser(req, res) { |
| 66 | + const { user } = req.params; |
| 67 | + const rooms = await getAllRoomsByUserId(user); |
| 68 | + |
| 69 | + if (rooms) { |
| 70 | + res.status(200).json(rooms); |
| 71 | + } else { |
| 72 | + res.status(500).json({ error: "Failed to retrieve rooms" }); |
| 73 | + } |
| 74 | +} |
| 75 | +export async function getRoomChatHistory(req, res) { |
| 76 | + const { roomId } = req.params; |
| 77 | + |
| 78 | + if (!roomId) { |
| 79 | + return res.status(400).json({ error: "Room ID is required" }); |
| 80 | + } |
| 81 | + |
| 82 | + const room = await fetchRoomChatHistory(roomId); |
| 83 | + |
| 84 | + if (room) { |
| 85 | + res.status(200).json(room); |
| 86 | + } else { |
| 87 | + res.status(404).json({ error: `Room not found for ID: ${roomId}` }); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Get QuestionId from the room based on the roomId |
| 92 | +export async function getQuestionId(req, res) { |
| 93 | + const { roomId } = req.params; |
| 94 | + |
| 95 | + if (!roomId) { |
| 96 | + return res.status(400).json({ error: "Room ID is required" }); |
| 97 | + } |
| 98 | + const questionId = await getQuestionIdByRoomId(roomId); |
| 99 | + |
| 100 | + if (questionId) { |
| 101 | + res.status(200).json({ questionId }); |
| 102 | + } else { |
| 103 | + res |
| 104 | + .status(404) |
| 105 | + .json({ error: `Question ID not found for room ID: ${roomId}` }); |
| 106 | + } |
| 107 | +} |
0 commit comments