Skip to content

Commit

Permalink
Merge aaa1026 into ac2e144
Browse files Browse the repository at this point in the history
  • Loading branch information
amarachukwu-agbo committed May 1, 2018
2 parents ac2e144 + aaa1026 commit 3702b4e
Show file tree
Hide file tree
Showing 19 changed files with 1,496 additions and 1,163 deletions.
2 changes: 1 addition & 1 deletion .coveralls.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
repo_token: p6ReD172PdYitrH3pVlD8SFd2E6fstcZQ
repo_token: SFXDaMFDDKpJZz3M9Z1h3GIeHkh4Lhx7f
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ public/lib
.sass-cache/
.DS_store
c4h_keys.txt
bower_components
/data
/.env
.nyc_output
coverage
/data
/.env
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ node_js:
services:
- mongodb

after-script:
npm run coverage
after_script:
- npm run coverage
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![Continuous Integration](https://camo.githubusercontent.com/23ee7a697b291798079e258bbc25434c4fac4f8b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f50726f7465637465645f62792d486f756e642d6138373364312e737667)](https://houndci.com)
[![Coverage Status](https://coveralls.io/repos/github/andela/thorin-cfh/badge.svg?branch=chore%2F156882154%2Fcoveralls-set-up)](https://coveralls.io/github/andela/thorin-cfh?branch=chore%2F156882154%2Fcoveralls-set-up)
[![Coverage Status](https://coveralls.io/repos/github/andela/thorin-cfh/badge.svg?branch=staging)](https://coveralls.io/github/andela/thorin-cfh?branch=staging)
[![Build Status](https://travis-ci.org/andela/thorin-cfh.svg?branch=staging)](https://travis-ci.org/andela/thorin-cfh)

Cards for Humanity - [http://cfh.io](http://cfh.io)
Expand Down Expand Up @@ -57,4 +57,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 changes: 39 additions & 0 deletions app/controllers/games.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

import gameModel from '../models/game';

/**
*
* @param {object} req - The HTTP request
* @param {object} res - The HTTP response
* @returns {void}
*/
const saveGame = (req, res) => {
const {
players, winner, gameStarter, roundsPlayed,
} = req.body;
const gameID = req.params.id;

const gameEntry = new gameModel({
players,
gameID,
winner,
gameStarter,
roundsPlayed,
});

gameEntry.save()
.then((game) => {
res.status('201').json({
msg: 'Success, Game saved',
game
});
})
.catch((error) => {
res.status('400').json({
msg: 'Failure, Game not saved',
error
});
});
};

export default saveGame;
20 changes: 0 additions & 20 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import mongoose from 'mongoose';
import jwt from 'jsonwebtoken';

const avatars = require('./avatars').all();

require('dotenv').config({ path: '.env' });

const User = mongoose.model('User');
Expand Down Expand Up @@ -168,24 +166,6 @@ exports.createUser = function (req, res) {
});
};

/**
* Assign avatar to user
*/
exports.avatars = function (req, res) {
// Update the current user's profile to include the avatar choice they've made
if (req.user && req.user._id && req.body.avatar !== undefined &&
/\d/.test(req.body.avatar) && avatars[req.body.avatar]) {
User.findOne({
_id: req.user._id
})
.exec((err, user) => {
user.avatar = avatars[req.body.avatar];
user.save();
});
}
return res.redirect('/#!/app');
};


exports.addDonation = function (req, res) {
if (req.body && req.user && req.user._id) { //eslint-disable-line
Expand Down
32 changes: 32 additions & 0 deletions app/models/game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import mongoose, { Schema } from 'mongoose';
import '../../config/config';

/**
* Game Schema
*/
const GameSchema = new Schema(
{
players: {
type: Array,
required: true,
},
gameID: {
type: String,
required: true,
},
winner: {
type: String,
required: true,
},
gameStarter: {
type: String,
required: true,
},
roundsPlayed: {
type: Number,
}
},
{ timestamps: { createdAt: 'playedAt' } }
);

export default mongoose.model('Game', GameSchema);
1 change: 1 addition & 0 deletions app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const UserSchema = new Schema({
name: String,
email: {
type: String,
required: true,
unique: true
},
username: String,
Expand Down
10 changes: 3 additions & 7 deletions app/views/includes/foot.jade
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ script(type='text/javascript', src='/lib/underscore/underscore-min.js')
script(type='text/javascript', src='/lib/bootstrap/js/bootstrap.js')

//AngularJS
script(type='text/javascript', src='https://code.angularjs.org/1.1.5/angular.min.js')
script(type='text/javascript', src='https://code.angularjs.org/1.1.5/angular-resource.js')
script(type='text/javascript', src='https://code.angularjs.org/1.1.5/angular-cookies.js')
script(type='text/javascript', src='http://code.angularjs.org/1.1.5/angular.js')
script(type='text/javascript', src='http://code.angularjs.org/1.1.5/angular-resource.js')
script(type='text/javascript', src='http://code.angularjs.org/1.1.5/angular-cookies.js')
script(type='text/javascript', src='https://cdn.jsdelivr.net/npm/jwt-decode@2.2.0/build/jwt-decode.min.js')
script(type='text/javascript' src="https://unpkg.com/axios/dist/axios.min.js")
link(rel="stylesheet" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css")
script(type="'text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js")


//Angular UI
script(type='text/javascript', src='/lib/angular-bootstrap/ui-bootstrap-tpls.js')
Expand All @@ -38,7 +35,6 @@ script(type='text/javascript', src='/js/services/game.js')
script(type='text/javascript', src='/js/controllers/index.js')
script(type='text/javascript', src='/js/controllers/header.js')
script(type='text/javascript', src='/js/controllers/game.js')
script(type='text/javascript', src='/js/init.js')

//Socket.io Client Library
script(type='text/javascript', src='/socket.io/socket.io.js')
Expand Down
29 changes: 29 additions & 0 deletions config/middlewares/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ class Validate {
}
next(); // there were no errors!
}

/**
* verify the game data
*
* @param {Object} req HTTP request object
* @param {Object} res HTTP response object
* @param {Object} next HTTP response object
*
* @returns {void}
*/
static gameValidation(req, res, next) {
// Check if all fields are present in the game data
req.checkBody('players', 'players must be an array').isArray().notEmpty();
req.checkBody('winner', 'winner must be a string').isString();
req.checkBody('roundsPlayed', 'roundsPlayed must be an integer').isInt();
req.checkBody('gameStarter', 'gameStarter must be a string').isString();

const results = req.validationErrors();

if (results) {
const errors = [];
results.map(result => errors.push(result.msg));
return res.status(400).json({
message: 'Failure, Game not saved',
errors
});
}
next();
}
}


Expand Down
17 changes: 10 additions & 7 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/* eslint-disable global-require */
import { validateSignIn } from './middlewares/validateSignIn';
import validator from './middlewares/validator';
import auth from './middlewares/checkToken';
import saveGame from '../app/controllers/games';

module.exports = function (app, passport) {
// User Routes
Expand All @@ -11,9 +13,6 @@ module.exports = function (app, passport) {
app.get('/signin', users.signin);
app.get('/signup', users.signup);
app.get('/signout', users.signout);
app.get('/chooseavatars', users.checkAvatar);

app.post('/users/avatars', users.avatars);

// Setting up the users api
app.post('/users', users.create);
Expand Down Expand Up @@ -90,10 +89,6 @@ module.exports = function (app, passport) {
// Finish with setting up the answerId param
app.param('answerId', answers.answer);

// Avatar Routes
const avatars = require('../app/controllers/avatars');
app.get('/avatars', avatars.allJSON);

// Question Routes
const questions = require('../app/controllers/questions');
app.get('/questions', questions.all);
Expand All @@ -104,4 +99,12 @@ module.exports = function (app, passport) {
// Home route
app.get('/play', index.play);
app.get('/', index.render);

// Start game API endpoint
app.post(
'/api/games/:id/start',
auth.verifyToken,
validator.gameValidation,
saveGame
);
};
1 change: 0 additions & 1 deletion config/socket/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ Game.prototype.prepareGame = function() {
}
self.questions = results[0];
self.answers = results[1];

self.startGame();
});
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"bower": "node node_modules/gulp/bin/gulp bower",
"start:dev": "babel-node node_modules/gulp/bin/gulp",
"start": "babel-node node_modules/gulp/bin/gulp",
"test": "nyc --reportrer=html --reporter=text mocha --require babel-core/register ./test/**/*.js --timeout 60000 --exit",
"test": "nyc mocha --require babel-core/register ./test/**/*.js --timeout 60000 --exit",
"test:server": "babel-node node_modules/.bin/gulp test ",
"test:client": "babel-node node_modules/gulp/bin/gulp test:client ",
"postinstall": "node node_modules/grunt-cli/bin/grunt install",
Expand Down
Loading

0 comments on commit 3702b4e

Please sign in to comment.