diff --git a/packages/server/packages/play-node-go/server/controllers/api/apiRoom.js b/packages/server/packages/play-node-go/server/controllers/api/apiRoom.js index 9e41f2e..a3faa56 100644 --- a/packages/server/packages/play-node-go/server/controllers/api/apiRoom.js +++ b/packages/server/packages/play-node-go/server/controllers/api/apiRoom.js @@ -1,4 +1,5 @@ const roomQueries = require('../../data/queries/room'); +const messageQueries = require('../../data/queries/message'); const {enableRoomSocket} = require('../../socket'); const getAll = async (req, res, next) => { @@ -18,7 +19,8 @@ const show = async (req, res, next) => { try { const roomId = req.params.id; const roomGames = await roomQueries.findRoomById(roomId); - const body = {roomGames} + const messages = await messageQueries.findMessageByRoom(roomId); + const body = {roomGames, messages}; res.status(200).json(body); } catch (err) { diff --git a/packages/server/packages/play-node-go/server/controllers/auth.js b/packages/server/packages/play-node-go/server/controllers/auth.js index 17ebb48..09b7f5b 100644 --- a/packages/server/packages/play-node-go/server/controllers/auth.js +++ b/packages/server/packages/play-node-go/server/controllers/auth.js @@ -14,18 +14,16 @@ const checkValidationErrors = (req, res) => { const signup = async (req, res, next) => { checkValidationErrors(req, res); const user = req.body; - try { delete user.confirmPassword; + const existingUser = await userQueries.findUserByNameOrEmail(user); const hashedPassword = await hashPassword(user.password); const secureUser = { ...user, password: hashedPassword }; - const existingUser = await userQueries.findUserByNameOrEmail(secureUser); - if (existingUser.length) { return res.status(409).json({errors: [{auth: 'User already exists!'}]}) } - const newUser = await userQueries.insertUser(secureUser) + const newUser = await userQueries.insertUser(secureUser); signToken(res, newUser) res.status(201).json({...newUser}); } diff --git a/packages/server/packages/play-node-go/server/data/queries/message.js b/packages/server/packages/play-node-go/server/data/queries/message.js index 3a20e12..eafcdf4 100644 --- a/packages/server/packages/play-node-go/server/data/queries/message.js +++ b/packages/server/packages/play-node-go/server/data/queries/message.js @@ -1,11 +1,17 @@ const knex = require('../db'); - -// TODO timestamps +const joinUserSelect = [ + 'content', 'username', 'admin' +] const findMessageByRoom = async (roomId) => { - return await knex('message') - .where({'id': roomId}) - .select('*'); + return await knex + .from('message') + .where({'message.room': roomId}) + .select(joinUserSelect) + .join('user', function() { + this.on('message.user', '=', 'user.id') + }) + // .toSQL(); } module.exports = { diff --git a/packages/server/packages/play-node-go/server/data/queries/room.js b/packages/server/packages/play-node-go/server/data/queries/room.js index c00c30b..693af1f 100644 --- a/packages/server/packages/play-node-go/server/data/queries/room.js +++ b/packages/server/packages/play-node-go/server/data/queries/room.js @@ -1,6 +1,6 @@ const knex = require('../db'); -const joinGameSection = [ +const joinGameSelect = [ 'room.id', 'room.name', 'room.description', 'room.language', 'game.komi', 'game.handicap', 'game.board_size', 'game.player_black', 'game.player_white', @@ -17,7 +17,7 @@ const findRoomById = async (roomId) => { return await knex .from('room') - .select(joinGameSection) + .select(joinGameSelect) .where('room.id', '=', roomId) .join('game', function() { this.on('game.room', '=', 'room.id') diff --git a/packages/server/packages/play-node-go/server/data/seeds/04_user.js b/packages/server/packages/play-node-go/server/data/seeds/04_user.js new file mode 100644 index 0000000..e68be10 --- /dev/null +++ b/packages/server/packages/play-node-go/server/data/seeds/04_user.js @@ -0,0 +1,16 @@ +const {hashPassword} = require('../../services/bcrypt'); +require('dotenv').config(); +const password = process.env.USER_ONE_PASSWORD; +const hashedPassword = hashPassword(password); +const email = process.env.USER_ONE_EMAIL; + +exports.seed = function(knex) { + // Deletes ALL existing entries + return knex('user').del() + .then(function () { + // Inserts seed entries + return knex('user').insert([ + {id: 2, username: 'user-one', email: email, password: hashedPassword, admin: true}, + ]); + }); +}; \ No newline at end of file diff --git a/packages/server/packages/play-node-go/server/data/seeds/05_message.js b/packages/server/packages/play-node-go/server/data/seeds/05_message.js new file mode 100644 index 0000000..e1af1cc --- /dev/null +++ b/packages/server/packages/play-node-go/server/data/seeds/05_message.js @@ -0,0 +1,11 @@ + +exports.seed = function(knex) { + // Deletes ALL existing entries + return knex('message').del() + .then(function () { + // Inserts seed entries + return knex('message').insert([ + {id: 1, content: 'Hey! Welcome to the general room!', room: 1, user: 2} + ]); + }); +}; \ No newline at end of file diff --git a/packages/server/packages/play-node-go/server/test/room/api.room.spec.js b/packages/server/packages/play-node-go/server/test/room/api.room.spec.js index 8242bb9..15b690f 100644 --- a/packages/server/packages/play-node-go/server/test/room/api.room.spec.js +++ b/packages/server/packages/play-node-go/server/test/room/api.room.spec.js @@ -14,6 +14,11 @@ const apiRoomSpec = (chai, knex, server) => { player_white: 'anon', player_black_rank: 'K3', player_white_rank: 'K2' + } ], + messages: [ { + content: 'Hey! Welcome to the general room!', + username: 'user-one', + admin: true } ] }