diff --git a/server/controllers/api/apiRoom.js b/server/controllers/api/apiRoom.js index a3faa56..9f77c87 100644 --- a/server/controllers/api/apiRoom.js +++ b/server/controllers/api/apiRoom.js @@ -1,13 +1,11 @@ const roomQueries = require('../../data/queries/room'); const messageQueries = require('../../data/queries/message'); +const gameQueries = require('../../data/queries/game'); const {enableRoomSocket} = require('../../socket'); const getAll = async (req, res, next) => { try { - // TODO eventually add check for user's private rooms - const publicRooms = await roomQueries.findPublicRooms(); - enableRoomSocket(1) - res.status(200).json({rooms: publicRooms}) + res.status(200).json({rooms: [...publicRooms]}) } catch (err) { @@ -18,9 +16,14 @@ const getAll = async (req, res, next) => { const show = async (req, res, next) => { try { const roomId = req.params.id; - const roomGames = await roomQueries.findRoomById(roomId); + // TODO eventually add check for user's private rooms + const publicRooms = await roomQueries.findPublicRooms(); + enableRoomSocket(roomId); + + const currentRoom = await roomQueries.findRoomById(roomId); const messages = await messageQueries.findMessageByRoom(roomId); - const body = {roomGames, messages}; + const roomGames = await gameQueries.findGameByRoom(roomId); + const body = {currentRoom, messages, roomGames}; res.status(200).json(body); } catch (err) { diff --git a/server/data/queries/game.js b/server/data/queries/game.js index 83edf27..fc660df 100644 --- a/server/data/queries/game.js +++ b/server/data/queries/game.js @@ -1,5 +1,10 @@ const knex = require('../db'); +const gameOverviewSelect = [ + 'id', 'board_size', 'komi', 'handicap', + 'player_black', 'player_black_rank', 'player_white', 'player_white_rank' +] + const findGameById = async (gameId) => { return await knex('game') .where({'id': gameId}) @@ -9,7 +14,7 @@ const findGameById = async (gameId) => { const findGameByRoom = async (roomId) => { return await knex('game') .where({'id': roomId}) - .select('*'); + .select(gameOverviewSelect); } const insertGame = async (game) => { diff --git a/server/data/queries/room.js b/server/data/queries/room.js index 693af1f..70ef436 100644 --- a/server/data/queries/room.js +++ b/server/data/queries/room.js @@ -1,27 +1,22 @@ const knex = require('../db'); -const joinGameSelect = [ - 'room.id', 'room.name', 'room.description', 'room.language', - 'game.komi', 'game.handicap', 'game.board_size', - 'game.player_black', 'game.player_white', - 'game.player_black_rank', 'game.player_white_rank' +const roomSelect = [ + 'id', 'name', 'description', 'language', ] const findPublicRooms = async () => { return await knex('room') .where('private', false) - .select(['id', 'name', 'description', 'language']); + .select(roomSelect); } const findRoomById = async (roomId) => { - - return await knex + const results = await knex .from('room') - .select(joinGameSelect) - .where('room.id', '=', roomId) - .join('game', function() { - this.on('game.room', '=', 'room.id') - }) + .select(roomSelect) + .where('room.id', roomId) + + return results[0] } module.exports = { diff --git a/server/test/room/api.room.spec.js b/server/test/room/api.room.spec.js index 15b690f..5de030f 100644 --- a/server/test/room/api.room.spec.js +++ b/server/test/room/api.room.spec.js @@ -2,11 +2,14 @@ const apiRoomSpec = (chai, knex, server) => { const roomEndpoint = '/api/v1/rooms'; const publicRooms = {rooms: [{id: 1, name: 'main', description: 'A general place to play Go', language: 'EN'}]}; const roomOne = { - roomGames: [ { + currentRoom: { id: 1, name: 'main', description: 'A general place to play Go', - language: 'EN', + language: 'EN' + }, + roomGames: [ { + id: 1, komi: 6.5, handicap: 0, board_size: 19,