serve room with message data from api/v1/rooms/:id

This commit is contained in:
Sorrel Bri 2020-01-21 17:02:54 -08:00 committed by sorrelbri
parent aa0c850aab
commit 661fc74df2
7 changed files with 50 additions and 12 deletions

View file

@ -1,4 +1,5 @@
const roomQueries = require('../../data/queries/room'); const roomQueries = require('../../data/queries/room');
const messageQueries = require('../../data/queries/message');
const {enableRoomSocket} = require('../../socket'); const {enableRoomSocket} = require('../../socket');
const getAll = async (req, res, next) => { const getAll = async (req, res, next) => {
@ -18,7 +19,8 @@ const show = async (req, res, next) => {
try { try {
const roomId = req.params.id; const roomId = req.params.id;
const roomGames = await roomQueries.findRoomById(roomId); const roomGames = await roomQueries.findRoomById(roomId);
const body = {roomGames} const messages = await messageQueries.findMessageByRoom(roomId);
const body = {roomGames, messages};
res.status(200).json(body); res.status(200).json(body);
} }
catch (err) { catch (err) {

View file

@ -14,18 +14,16 @@ const checkValidationErrors = (req, res) => {
const signup = async (req, res, next) => { const signup = async (req, res, next) => {
checkValidationErrors(req, res); checkValidationErrors(req, res);
const user = req.body; const user = req.body;
try { try {
delete user.confirmPassword; delete user.confirmPassword;
const existingUser = await userQueries.findUserByNameOrEmail(user);
const hashedPassword = await hashPassword(user.password); const hashedPassword = await hashPassword(user.password);
const secureUser = { ...user, password: hashedPassword }; const secureUser = { ...user, password: hashedPassword };
const existingUser = await userQueries.findUserByNameOrEmail(secureUser);
if (existingUser.length) { if (existingUser.length) {
return res.status(409).json({errors: [{auth: 'User already exists!'}]}) 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) signToken(res, newUser)
res.status(201).json({...newUser}); res.status(201).json({...newUser});
} }

View file

@ -1,11 +1,17 @@
const knex = require('../db'); const knex = require('../db');
const joinUserSelect = [
// TODO timestamps 'content', 'username', 'admin'
]
const findMessageByRoom = async (roomId) => { const findMessageByRoom = async (roomId) => {
return await knex('message') return await knex
.where({'id': roomId}) .from('message')
.select('*'); .where({'message.room': roomId})
.select(joinUserSelect)
.join('user', function() {
this.on('message.user', '=', 'user.id')
})
// .toSQL();
} }
module.exports = { module.exports = {

View file

@ -1,6 +1,6 @@
const knex = require('../db'); const knex = require('../db');
const joinGameSection = [ const joinGameSelect = [
'room.id', 'room.name', 'room.description', 'room.language', 'room.id', 'room.name', 'room.description', 'room.language',
'game.komi', 'game.handicap', 'game.board_size', 'game.komi', 'game.handicap', 'game.board_size',
'game.player_black', 'game.player_white', 'game.player_black', 'game.player_white',
@ -17,7 +17,7 @@ const findRoomById = async (roomId) => {
return await knex return await knex
.from('room') .from('room')
.select(joinGameSection) .select(joinGameSelect)
.where('room.id', '=', roomId) .where('room.id', '=', roomId)
.join('game', function() { .join('game', function() {
this.on('game.room', '=', 'room.id') this.on('game.room', '=', 'room.id')

View file

@ -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},
]);
});
};

View file

@ -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}
]);
});
};

View file

@ -14,6 +14,11 @@ const apiRoomSpec = (chai, knex, server) => {
player_white: 'anon', player_white: 'anon',
player_black_rank: 'K3', player_black_rank: 'K3',
player_white_rank: 'K2' player_white_rank: 'K2'
} ],
messages: [ {
content: 'Hey! Welcome to the general room!',
username: 'user-one',
admin: true
} ] } ]
} }