From b3d05639ad1acc6f02c7f20188fcc720d4d55c4b Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Tue, 21 Jan 2020 16:01:35 -0800 Subject: [PATCH] serve room with joined game data from api/v1/rooms/:id --- .../server/server/controllers/api/apiRoom.js | 17 +++++++++++-- .../data/migrations/20200120224032_game.js | 10 ++++++++ packages/server/server/data/queries/room.js | 21 +++++++++++++++- .../server/data/seeds/{room.js => 01_room.js} | 0 .../{timeSetting.js => 02_timeSetting.js} | 0 packages/server/server/data/seeds/03_game.js | 17 +++++++++++++ packages/server/server/routes/api/room.js | 3 ++- .../server/server/test/room/api.room.spec.js | 25 +++++++++++++++++++ 8 files changed, 89 insertions(+), 4 deletions(-) rename packages/server/server/data/seeds/{room.js => 01_room.js} (100%) rename packages/server/server/data/seeds/{timeSetting.js => 02_timeSetting.js} (100%) create mode 100644 packages/server/server/data/seeds/03_game.js diff --git a/packages/server/server/controllers/api/apiRoom.js b/packages/server/server/controllers/api/apiRoom.js index bc08296..9e41f2e 100644 --- a/packages/server/server/controllers/api/apiRoom.js +++ b/packages/server/server/controllers/api/apiRoom.js @@ -1,7 +1,7 @@ const roomQueries = require('../../data/queries/room'); const {enableRoomSocket} = require('../../socket'); -const roomIndex = async (req, res, next) => { +const getAll = async (req, res, next) => { try { // TODO eventually add check for user's private rooms const publicRooms = await roomQueries.findPublicRooms(); @@ -14,6 +14,19 @@ const roomIndex = async (req, res, next) => { } } +const show = async (req, res, next) => { + 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 = { - roomIndex + getAll, + show } \ No newline at end of file diff --git a/packages/server/server/data/migrations/20200120224032_game.js b/packages/server/server/data/migrations/20200120224032_game.js index 16449c9..b70b147 100644 --- a/packages/server/server/data/migrations/20200120224032_game.js +++ b/packages/server/server/data/migrations/20200120224032_game.js @@ -4,6 +4,13 @@ const winType = [ '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) { return knex.schema.createTable("game", table => { table.increments('id').primary(); @@ -18,6 +25,9 @@ exports.up = function(knex) { table.string('player_black'); 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('name'); table.string('description'); diff --git a/packages/server/server/data/queries/room.js b/packages/server/server/data/queries/room.js index b08edf9..c00c30b 100644 --- a/packages/server/server/data/queries/room.js +++ b/packages/server/server/data/queries/room.js @@ -1,11 +1,30 @@ 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 () => { return await knex('room') .where('private', false) .select(['id', 'name', 'description', 'language']); } +const findRoomById = async (roomId) => { + + return await knex + .from('room') + .select(joinGameSection) + .where('room.id', '=', roomId) + .join('game', function() { + this.on('game.room', '=', 'room.id') + }) +} + module.exports = { - findPublicRooms + findPublicRooms, + findRoomById } \ No newline at end of file diff --git a/packages/server/server/data/seeds/room.js b/packages/server/server/data/seeds/01_room.js similarity index 100% rename from packages/server/server/data/seeds/room.js rename to packages/server/server/data/seeds/01_room.js diff --git a/packages/server/server/data/seeds/timeSetting.js b/packages/server/server/data/seeds/02_timeSetting.js similarity index 100% rename from packages/server/server/data/seeds/timeSetting.js rename to packages/server/server/data/seeds/02_timeSetting.js diff --git a/packages/server/server/data/seeds/03_game.js b/packages/server/server/data/seeds/03_game.js new file mode 100644 index 0000000..185c88e --- /dev/null +++ b/packages/server/server/data/seeds/03_game.js @@ -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 + } + ]); + }); +}; \ No newline at end of file diff --git a/packages/server/server/routes/api/room.js b/packages/server/server/routes/api/room.js index 9f8e56c..adba750 100644 --- a/packages/server/server/routes/api/room.js +++ b/packages/server/server/routes/api/room.js @@ -2,6 +2,7 @@ const express = require('express'); const router = express.Router(); const apiRoomController = require('../../controllers/api/apiRoom'); -router.get('/', apiRoomController.roomIndex); +router.get('/', apiRoomController.getAll); +router.get('/:id', apiRoomController.show); module.exports = router; diff --git a/packages/server/server/test/room/api.room.spec.js b/packages/server/server/test/room/api.room.spec.js index 527da88..8242bb9 100644 --- a/packages/server/server/test/room/api.room.spec.js +++ b/packages/server/server/test/room/api.room.spec.js @@ -1,6 +1,21 @@ 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: [ { + 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 => { knex('room').where('id', 1).orWhere('id', 2).select('name').then(roomResults => { @@ -27,6 +42,16 @@ const apiRoomSpec = (chai, knex, server) => { 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; \ No newline at end of file