Skip to content

Commit

Permalink
Merge cb89065 into 41c287d
Browse files Browse the repository at this point in the history
  • Loading branch information
AccaliaDeElementia committed Jul 12, 2018
2 parents 41c287d + cb89065 commit 818e8d5
Show file tree
Hide file tree
Showing 15 changed files with 547 additions and 300 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ errorShots
.nyc_output

#Dev Mode Database File.
database.sqlite
database.sqlite

#vscode workspace files
.vscode
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

[![Stories in Ready](https://badge.waffle.io/SockDrawer/SockRPG.png?label=ready&title=Ready)](https://waffle.io/SockDrawer/SockRPG)


SockRPG: A Forum built by role players, for roleplayers.

Nowhere near complete!
20 changes: 10 additions & 10 deletions src/controllers/boardController.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ function getAllGames(_, res) {
*/
function getGame(req, res) {
if (!req.params.id) {
res.status(501).send({error: 'Missing ID'});
res.status(400).send({error: 'Missing ID'});
return Promise.resolve();
}

return Game.getGame(req.params.id).then((data) => {
if (!data) {
return Game.get(req.params.id).then((data) => {
if (!data || !(data instanceof Game)) {
return res.status(404).end();
}

Expand Down Expand Up @@ -97,8 +97,8 @@ function addGame(req, res) {
* @returns {Promise} A promise that will resolve when the response has been sent.
*/
function updateGame(req, res) {
return Game.getGame(req.params.id).then((board) => {
if (!board) {
return Game.get(req.params.id).then((board) => {
if (!board || !(board instanceof Game)) {
return res.status(404).end();
}
board.Name = req.body.Name || board.Name;
Expand Down Expand Up @@ -143,12 +143,12 @@ function getAllBoards(_, res) {
*/
function getBoard(req, res) {
if (!req.params.id) {
res.status(501).send({error: 'Missing ID'});
res.status(400).send({error: 'Missing ID'});
return Promise.resolve();
}

return Board.getBoard(req.params.id).then((data) => {
if (!data) {
return Board.get(req.params.id).then((data) => {
if (!data || data instanceof Game) {
res.status(404).end();
return undefined;
}
Expand Down Expand Up @@ -191,8 +191,8 @@ function addBoard(req, res) {
* @returns {Promise} A promise that will resolve when the response has been sent.
*/
function updateBoard(req, res) {
return Board.getBoard(req.params.id).then((board) => {
if (!board) {
return Board.get(req.params.id).then((board) => {
if (!board || board instanceof Game) {
return res.status(404).end();
}
board.Name = req.body.Name || board.Name;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/pageController.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function getHomePage(req, res) {
*/
function getBoardView(req, res) {
let board;
return Board.getBoard(req.params.id).then((data) => {
return Board.get(req.params.id).then((data) => {
if (!data) {
res.status(404);
res.end();
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/threadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const Board = require('../model/Board');
*/
function getThreadsForBoard(req, res) {

return Board.getBoard(req.params.id).then((data) => {
return Board.get(req.params.id).then((data) => {
if (!data) {
res.status(404).end();
return Promise.resolve();
Expand Down Expand Up @@ -84,7 +84,7 @@ function getThread(req, res) {
* @returns {Promise} A Promise that is resolved when the thread is added
*/
function addThreadToBoard(req, res) {
return Board.getBoard(req.params.id).then((data) => {
return Board.get(req.params.id).then((data) => {
if (!data) {
res.status(404).end();
return Promise.resolve();
Expand Down
81 changes: 59 additions & 22 deletions src/model/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Threads = require('./Thread');
const DB = require('./db');
let Game = null; // Late load of `require('./Game')

/**
* The Board table.
Expand All @@ -12,7 +13,7 @@ const DB = require('./db');
* @license MIT
* @author yamikuronue
*/

class Board {
constructor (rowData) {
this.data = rowData;
Expand Down Expand Up @@ -61,10 +62,29 @@ class Board {
this.data.Adult = Boolean(adult);
}

getParent () {
if (!this.data.ParentID){
return Promise.resolve(null);
}
return Board.get(this.data.ParentID);
}

setParent (parent) {
if (parent !== null && !(parent instanceof Board)){
return Promise.reject(new Error('Parent must be a Board'));
}
this.data.ParentID = parent && parent.ID;
return this.save();
}

getThreads() {
return Threads.getThreadsInBoard(this.ID)
.then((threads) => threads && threads.length ? threads.map((thread) => thread.ID) : []);
}

getChildren () {
return Board.getChildrenOf(this.ID);
}

serialize() {
const serial = JSON.parse(JSON.stringify(this.data));
Expand All @@ -76,6 +96,14 @@ class Board {
return DB.knex('Boards').where('ID', this.ID).update(this.data);
}

static deserialize(data) {
if (data.GameID) {
return new Game(data);
}
delete data.gameDescription;
return new Board(data);
}

/**
* Get all vanilla boards in the forum.
*
Expand All @@ -86,23 +114,25 @@ class Board {
}

/**
* Get all vanilla boards that belong to a parent board, or all root-level vanilla boards if no parent specified.
* Get all boards and games that belong to a parent board, or all root-level
* boards and games if no parent specified.
*
* @param {Number} [parentID] The ID of the parent board, or `null` for root-level boards
* @param {Number} [parent] The parent board, or `null` for root-level boards
*
* @returns {Promise} A Promise that is resolved with a list of vanilla boards
*/
static getBoards(parentID) {
//TODO: How does a child board get added? is it for games? should figure this out (Accalia)
if (parentID !== 0) {
parentID = parentID || null; //Coerce to null to prevent avoidable errors
static getChildrenOf(parent) {
if (parent instanceof Board) {
parent = parent.ID;
}
if (!parent) {
parent = null;
}

return DB.knex('Boards')
.leftJoin('ChildBoards', 'Boards.ID', 'ChildBoards.ChildID')
.where('parentID', parentID)
.select('Boards.ID', 'Owner', 'Name', 'Description', 'GameID')
.map((row) => new Board(row));
.leftJoin('Games', 'Boards.GameID', 'Games.ID')
.where('Boards.ParentID', parent)
.select('Boards.ID', 'ParentID', 'Owner', 'Name', 'Adult', 'GameID', 'gameDescription', 'Description')
.then((rows) => rows.map(Board.deserialize));
}

/**
Expand All @@ -112,16 +142,17 @@ class Board {
*
* @returns {Promise} A Promise that is resolved with the board requested
*/
static getBoard(id) {
static get(id) {
return DB.knex('Boards')
.where('ID', id)
.select('Boards.ID', 'Owner', 'Name', 'Description', 'GameID', 'Adult')
.then((rows) => {
if (!rows || rows.length <= 0) {
return null;
}
return new Board(rows[0]);
});
.leftJoin('Games', 'Boards.GameID', 'Games.ID')
.where('Boards.ID', id)
.select('Boards.ID', 'ParentID', 'Owner', 'Name', 'Adult', 'GameID', 'gameDescription', 'Description')
.then((rows) => {
if (!rows.length) {
return null;
}
return Board.deserialize(rows[0]);
});
}

/**
Expand All @@ -143,10 +174,16 @@ class Board {
if (!board.Name) {
throw new Error('A board has no name.');
}
return DB.knex('Boards').insert(board.data);
return DB.knex('Boards').insert(board.data)
.then((ids) => {
board.data.ID = ids[0];
return ids;
});
});
}

}

module.exports = Board;

Game = require('./Game');
42 changes: 2 additions & 40 deletions src/model/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,45 +66,6 @@ class Game extends Board {
return DB.knex('Boards').innerJoin('Games', 'Boards.GameID', 'Games.ID').select().map((row) => new Game(row));
}

/**
* Get all vanilla boards that belong to a parent board, or all root-level vanilla boards if no parent specified.
*
* @param {Number} [parentID] The ID of the parent board, or `null` for root-level boards
*
* @returns {Promise} A Promise that is resolved with a list of vanilla boards
*/
static getGames(parentID) {
if (parentID !== 0) {
parentID = parentID || null; //Coerce to null to prevent avoidable errors
}

return DB.knex('Boards')
.leftJoin('ChildBoards', 'Boards.ID', 'ChildBoards.ChildID')
.where('parentID', parentID)
.select('Boards.ID', 'Owner', 'Name', 'GameID')
.map((row) => new Game(row));
}

/**
* Get a vanilla board by ID.
*
* @param {Number} id The ID of the board requested
*
* @returns {Promise} A Promise that is resolved with the board requested
*/
static getGame(id) {
return DB.knex('Boards')
.innerJoin('Games', 'Boards.GameID', 'Games.ID')
.where('Boards.ID', id)
.select('Boards.ID', 'Owner', 'Name', 'Adult', 'GameID', 'gameDescription', 'Description').then((rows) => {
if (!rows || rows.length <= 0) {
return null;
}

return new Game(rows[0]);
});
}

/**
* Add a game.
*
Expand All @@ -117,7 +78,8 @@ class Game extends Board {
.then(() => {
if (!(game instanceof Game)) {
if (game instanceof Board || !game.GameID && !game.Game) {
throw new Error('Vanilla boards cannot be added using this method; please use addBoard() instead');
throw new Error('Vanilla boards cannot be added using this method; ' +
'please use addBoard() instead');
}
game = new Game(game);
}
Expand Down
6 changes: 1 addition & 5 deletions src/model/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,12 @@ const db = {
table.increments('ID').primary();
//This shouldn't be nullable, but we don't have users working yet
table.integer('Owner').references('Users.ID'); //.notNullable();
table.integer('ParentID').references('Boards.ID').nullable();
table.integer('GameID').references('Games.ID').nullable();
table.string('Name').notNullable();
table.boolean('Adult').defaultTo(false);
table.string('Description').notNullable().defaultTo('');
}],
['ChildBoards', (table) => {
table.increments('ID').primary();
table.integer('ParentID').references('Boards.ID').notNullable();
table.integer('ChildID').references('Boards.ID').notNullable();
}],
['Threads', (table) => {
table.increments('ID').primary();
table.string('Title').notNullable();
Expand Down
1 change: 0 additions & 1 deletion src/views/home.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@

<script type="handlebars" id="boardModal">
<div class="modal-header">
{{log game}}
{{#if game}}<h3>Add a game</h3>
{{else}}<h3>Add a board</h3>{{/if}}
</div>
Expand Down
Loading

0 comments on commit 818e8d5

Please sign in to comment.