refactor api rooms/:id endpoint to deliver current room without table join
This commit is contained in:
parent
abfc0c7c94
commit
0a66d3f93c
4 changed files with 28 additions and 22 deletions
|
@ -1,13 +1,11 @@
|
||||||
const roomQueries = require('../../data/queries/room');
|
const roomQueries = require('../../data/queries/room');
|
||||||
const messageQueries = require('../../data/queries/message');
|
const messageQueries = require('../../data/queries/message');
|
||||||
|
const gameQueries = require('../../data/queries/game');
|
||||||
const {enableRoomSocket} = require('../../socket');
|
const {enableRoomSocket} = require('../../socket');
|
||||||
|
|
||||||
const getAll = async (req, res, next) => {
|
const getAll = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
// TODO eventually add check for user's private rooms
|
res.status(200).json({rooms: [...publicRooms]})
|
||||||
const publicRooms = await roomQueries.findPublicRooms();
|
|
||||||
enableRoomSocket(1)
|
|
||||||
res.status(200).json({rooms: publicRooms})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (err) {
|
catch (err) {
|
||||||
|
@ -18,9 +16,14 @@ const getAll = async (req, res, next) => {
|
||||||
const show = async (req, res, next) => {
|
const show = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const roomId = req.params.id;
|
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 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);
|
res.status(200).json(body);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
const knex = require('../db');
|
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) => {
|
const findGameById = async (gameId) => {
|
||||||
return await knex('game')
|
return await knex('game')
|
||||||
.where({'id': gameId})
|
.where({'id': gameId})
|
||||||
|
@ -9,7 +14,7 @@ const findGameById = async (gameId) => {
|
||||||
const findGameByRoom = async (roomId) => {
|
const findGameByRoom = async (roomId) => {
|
||||||
return await knex('game')
|
return await knex('game')
|
||||||
.where({'id': roomId})
|
.where({'id': roomId})
|
||||||
.select('*');
|
.select(gameOverviewSelect);
|
||||||
}
|
}
|
||||||
|
|
||||||
const insertGame = async (game) => {
|
const insertGame = async (game) => {
|
||||||
|
|
|
@ -1,27 +1,22 @@
|
||||||
const knex = require('../db');
|
const knex = require('../db');
|
||||||
|
|
||||||
const joinGameSelect = [
|
const roomSelect = [
|
||||||
'room.id', 'room.name', 'room.description', 'room.language',
|
'id', 'name', 'description', 'language',
|
||||||
'game.komi', 'game.handicap', 'game.board_size',
|
|
||||||
'game.player_black', 'game.player_white',
|
|
||||||
'game.player_black_rank', 'game.player_white_rank'
|
|
||||||
]
|
]
|
||||||
|
|
||||||
const findPublicRooms = async () => {
|
const findPublicRooms = async () => {
|
||||||
return await knex('room')
|
return await knex('room')
|
||||||
.where('private', false)
|
.where('private', false)
|
||||||
.select(['id', 'name', 'description', 'language']);
|
.select(roomSelect);
|
||||||
}
|
}
|
||||||
|
|
||||||
const findRoomById = async (roomId) => {
|
const findRoomById = async (roomId) => {
|
||||||
|
const results = await knex
|
||||||
return await knex
|
|
||||||
.from('room')
|
.from('room')
|
||||||
.select(joinGameSelect)
|
.select(roomSelect)
|
||||||
.where('room.id', '=', roomId)
|
.where('room.id', roomId)
|
||||||
.join('game', function() {
|
|
||||||
this.on('game.room', '=', 'room.id')
|
return results[0]
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -2,11 +2,14 @@ const apiRoomSpec = (chai, knex, server) => {
|
||||||
const roomEndpoint = '/api/v1/rooms';
|
const roomEndpoint = '/api/v1/rooms';
|
||||||
const publicRooms = {rooms: [{id: 1, name: 'main', description: 'A general place to play Go', language: 'EN'}]};
|
const publicRooms = {rooms: [{id: 1, name: 'main', description: 'A general place to play Go', language: 'EN'}]};
|
||||||
const roomOne = {
|
const roomOne = {
|
||||||
roomGames: [ {
|
currentRoom: {
|
||||||
id: 1,
|
id: 1,
|
||||||
name: 'main',
|
name: 'main',
|
||||||
description: 'A general place to play Go',
|
description: 'A general place to play Go',
|
||||||
language: 'EN',
|
language: 'EN'
|
||||||
|
},
|
||||||
|
roomGames: [ {
|
||||||
|
id: 1,
|
||||||
komi: 6.5,
|
komi: 6.5,
|
||||||
handicap: 0,
|
handicap: 0,
|
||||||
board_size: 19,
|
board_size: 19,
|
||||||
|
|
Loading…
Reference in a new issue