Skip to content

Commit

Permalink
added new gambler
Browse files Browse the repository at this point in the history
  • Loading branch information
LizaHCarter committed Aug 15, 2014
1 parent 4e87bce commit 9094d4c
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 5 deletions.
16 changes: 16 additions & 0 deletions app/controllers/gamblers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
'use strict';
var Gambler = require('../models/gambler');

exports.init = function(req, res){
res.render('gamblers/init');
};

exports.create = function(req, res){
var gambler = new Gambler(req.body);
gambler.save(function(){
res.redirect('/gamblers');
});
};

exports.index = function(req, res){
Gambler.all(function(err, gamblers){
res.render('gamblers/index', {gamblers:gamblers});
});
};

exports.show = function(req, res){
Gambler.findById(req.params.id, function(gambler){
res.render('gamblers/show', {gambler:gambler});
});
};
26 changes: 26 additions & 0 deletions app/models/gambler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
'use strict';

var Mongo = require('mongodb'),
_ = require('lodash');

function Gambler(o){
this.results = this.results || {wins:'0', losses: '0'};
this.isDivorced = false;
}

Object.defineProperty(Gambler, 'collection', {
Expand All @@ -11,5 +16,26 @@ Gambler.all = function(cb){
Gambler.collection.find().toArray(cb);
};

Gambler.findById = function(id, cb){
var _id = Mongo.ObjectID(id);
Gambler.collection.findOne({_id:_id}, function(err, obj){
var gambler = changePrototype(obj);

cb(gambler);
});
};

Gambler.deleteById = function(id, cb){
var _id = Mongo.ObjectID(id);
Gambler.collection.findAndRemove({_id:_id}, cb);
};

Gambler.prototype.save = function(cb){
Gambler.collection.save(this, cb);
};

module.exports = Gambler;

function changePrototype(obj){
return _.create(Gambler.prototype, obj);
}
4 changes: 3 additions & 1 deletion app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ module.exports = function(app, express){
app.use(bodyParser.urlencoded({extended:true}));
app.use(methodOverride());

//app.get('/', home.index);
app.get('/gamblers/new', gamblers.init);
app.post('/gamblers', gamblers.create);
app.get('/gamblers', gamblers.index);
app.get('/gamblers/:id', gamblers.show);

console.log('Routes Loaded');
};
Expand Down
6 changes: 5 additions & 1 deletion app/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ li.photo button{
right: 10px;
}

.spouseHead, .assetsTitle {
.spouseHead, .assetsTitle, h2 label {
font-family: 'Luckiest Guy', cursive;
font-size: 20px;
margin-top: 20px;
Expand All @@ -89,3 +89,7 @@ h2{
font-family: 'Luckiest Guy', cursive;
font-size: 40px;
}

#newGambler input, #newSpouse input{
color: black
}
12 changes: 10 additions & 2 deletions app/static/js/user/gambler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@

function sellAsset(){
var id = $(this).closest('.gambler').attr('data-gambler-id'),
name = $(this).text().split(' ')[0];
console.log(id, name);
name = $(this).text().split(' ')[0],
type = 'delete',
url = '/gamblers/'+id+'/assets/'+name;

$.ajax({url:url, type:type, dataType:'json', success:function(data){
var $asset = $('.asset');
$asset.fadeOut();

setTimeout(function(){$asset.remove();}, 2000);
}});
}

})();
Expand Down
17 changes: 17 additions & 0 deletions app/views/gamblers/init.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extends ../shared/template
block content
h2.new New Gambler
form(method='post', action='/gamblers')
#newGambler
label New Gambler
input(name='name', type='text', placeholder='Bob Smith', autofocus=true)
input(name='photo', type='text', placeholder='photo url')
input(name='cash', type='number', placeholder='cash amount')
//input(name='results.wins', type='text', placeholder='wins')
//input(name='results.losses', type='text', placeholder='losses')
#newSpouse
label Spouse
input(name='spouse.name', type='text', placeholder='Sarah Smith')
input(name='spouse.photo', type='text', placeholder='photo url')
#add
button Add Gambler
11 changes: 11 additions & 0 deletions app/views/gamblers/show.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extends ../shared/template
block content
h2= gambler.name

#newGambler
#photo(style='background-image: url(#{gambler.photo});')
#cash $#{gambler.cash}
#spouse
.name= #{gambler.spouse.name}
.photo(style='background-image: url(#{gambler.spouse.photo});')

1 change: 1 addition & 0 deletions app/views/shared/template.jade
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ html
ul
li: a(href='/') Home
li: a(href='/gamblers') Gamblers
li: a(href='/gamblers/new') Add Gambler

block content

Expand Down
32 changes: 31 additions & 1 deletion test/unit/gambler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ var expect = require('chai').expect,
Gambler = require('../../app/models/gambler'),
dbConnect = require('../../app/lib/mongodb'),
cp = require('child_process'),
db = 'the-derby-test';
db = 'the-derby-test',
Mongo = require('mongodb');

describe('Gambler', function(){
before(function(done){
Expand Down Expand Up @@ -37,5 +38,34 @@ describe('Gambler', function(){
});
});
});
describe('.findById', function(){
it('should find a gambler by its id', function(done){
Gambler.findById(Mongo.ObjectID('000000000000000000000002'), function(gambler){
expect(gambler).to.be.instanceof(Gambler);
expect(gambler.name).to.equal('Mr. Green');
done();
});
});
});
describe('.deleteById', function(){
it('should delete a gambler by its id', function(done){
Gambler.deleteById(Mongo.ObjectID('000000000000000000000002'), function(gambler){
Gambler.all(function(err, gamblers){
expect(gamblers).to.have.length(2);
done();
});
});
});
});
describe('#save', function(){
it('should save a new gambler to the database', function(done){
var g = new Gambler();
g.save(function(){
expect(g._id).to.be.instanceof(Mongo.ObjectID);
done();
});
});
});
//Last braces
});

0 comments on commit 9094d4c

Please sign in to comment.