refactor api rooms/:id endpoint to deliver current room without table join

This commit is contained in:
Sorrel Bri 2020-01-23 14:46:49 -08:00 committed by sorrelbri
parent 775e1fc050
commit 8589976264
4 changed files with 28 additions and 22 deletions

View file

@ -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) {

View file

@ -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) => {

View file

@ -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 = {

View file

@ -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,