serve room with joined game data from api/v1/rooms/:id
This commit is contained in:
parent
c323968404
commit
c1d269625b
8 changed files with 89 additions and 4 deletions
|
@ -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
|
||||
}
|
|
@ -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');
|
||||
|
|
|
@ -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
|
||||
}
|
17
server/data/seeds/03_game.js
Normal file
17
server/data/seeds/03_game.js
Normal 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
|
||||
}
|
||||
]);
|
||||
});
|
||||
};
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
Loading…
Reference in a new issue