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
parent c1d269625b
commit 067ce69cbb
7 changed files with 50 additions and 12 deletions

View file

@ -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) {

View file

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

View file

@ -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 = {

View file

@ -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')

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_black_rank: 'K3',
player_white_rank: 'K2'
} ],
messages: [ {
content: 'Hey! Welcome to the general room!',
username: 'user-one',
admin: true
} ]
}