serve room with joined game data from api/v1/rooms/:id

This commit is contained in:
Sorrel Bri 2020-01-21 16:01:35 -08:00 committed by sorrelbri
parent 5867c0ddd9
commit b3d05639ad
8 changed files with 89 additions and 4 deletions

View file

@ -1,7 +1,7 @@
const roomQueries = require('../../data/queries/room'); const roomQueries = require('../../data/queries/room');
const {enableRoomSocket} = require('../../socket'); const {enableRoomSocket} = require('../../socket');
const roomIndex = async (req, res, next) => { const getAll = async (req, res, next) => {
try { try {
// TODO eventually add check for user's private rooms // TODO eventually add check for user's private rooms
const publicRooms = await roomQueries.findPublicRooms(); const publicRooms = await roomQueries.findPublicRooms();
@ -14,6 +14,19 @@ const roomIndex = async (req, res, next) => {
} }
} }
module.exports = { const show = async (req, res, next) => {
roomIndex try {
const roomId = req.params.id;
const roomGames = await roomQueries.findRoomById(roomId);
const body = {roomGames}
res.status(200).json(body);
}
catch (err) {
res.status(500).json(err);
}
}
module.exports = {
getAll,
show
} }

View file

@ -4,6 +4,13 @@ const winType = [
'0', 'Void', '?' '0', 'Void', '?'
] ]
const rankArray = [
'D7', 'D6', 'D5', 'D4', 'D3', 'D2', 'D1',
'K1', 'K2', 'K3', 'K4', 'K5', 'K6', 'K7', 'K8', 'K9', 'K10',
'K11', 'K12', 'K13', 'K14', 'K15', 'K16', 'K17', 'K18', 'K19', 'K20',
'K21', 'K22', 'K23', 'K24', 'K25', 'K26', 'K27', 'K28', 'K29', 'K30', 'UR'
]
exports.up = function(knex) { exports.up = function(knex) {
return knex.schema.createTable("game", table => { return knex.schema.createTable("game", table => {
table.increments('id').primary(); table.increments('id').primary();
@ -18,6 +25,9 @@ exports.up = function(knex) {
table.string('player_black'); table.string('player_black');
table.string('player_white'); table.string('player_white');
table.enu('player_black_rank', rankArray).default('UR');
table.enu('player_white_rank', rankArray).default('UR');
table.string('event'); table.string('event');
table.string('name'); table.string('name');
table.string('description'); table.string('description');

View file

@ -1,11 +1,30 @@
const knex = require('../db'); const knex = require('../db');
const joinGameSection = [
'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 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(['id', 'name', 'description', 'language']);
} }
module.exports = { const findRoomById = async (roomId) => {
findPublicRooms
return await knex
.from('room')
.select(joinGameSection)
.where('room.id', '=', roomId)
.join('game', function() {
this.on('game.room', '=', 'room.id')
})
}
module.exports = {
findPublicRooms,
findRoomById
} }

View file

@ -0,0 +1,17 @@
exports.seed = function(knex) {
// Deletes ALL existing entries
return knex('game').del()
.then(function () {
// Inserts seed entries
return knex('game').insert([
{
id: 1, date: new Date(),
application: 'node-go', application_version: '0.1.0',
player_black: 'anon', player_white: 'anon',
player_black_rank: 'K3', player_white_rank: 'K2',
room: 1, time_setting: 1
}
]);
});
};

View file

@ -2,6 +2,7 @@ const express = require('express');
const router = express.Router(); const router = express.Router();
const apiRoomController = require('../../controllers/api/apiRoom'); const apiRoomController = require('../../controllers/api/apiRoom');
router.get('/', apiRoomController.roomIndex); router.get('/', apiRoomController.getAll);
router.get('/:id', apiRoomController.show);
module.exports = router; module.exports = router;

View file

@ -1,6 +1,21 @@
const apiRoomSpec = (chai, knex, server) => { 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 = {
roomGames: [ {
id: 1,
name: 'main',
description: 'A general place to play Go',
language: 'EN',
komi: 6.5,
handicap: 0,
board_size: 19,
player_black: 'anon',
player_white: 'anon',
player_black_rank: 'K3',
player_white_rank: 'K2'
} ]
}
it('seeded rooms should be present in db', done => { it('seeded rooms should be present in db', done => {
knex('room').where('id', 1).orWhere('id', 2).select('name').then(roomResults => { knex('room').where('id', 1).orWhere('id', 2).select('name').then(roomResults => {
@ -27,6 +42,16 @@ const apiRoomSpec = (chai, knex, server) => {
done(); done();
}); });
}) })
it('request to api room/1 should return 1 room record with game and message information', done => {
chai.request(server)
.get(`${roomEndpoint}/1`)
.end((err,res)=> {
if(err) done(err);
res.body.should.eql(roomOne);
done();
});
})
} }
module.exports = apiRoomSpec; module.exports = apiRoomSpec;