Skip to content

Commit

Permalink
Added markOnSale class method, updated profile.jade
Browse files Browse the repository at this point in the history
  • Loading branch information
DSRoden committed Aug 30, 2014
1 parent 4022adf commit e6ac1fc
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 25 deletions.
6 changes: 6 additions & 0 deletions app/controllers/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ exports.create = function(req, res){
});
};

exports.markonsale = function(req, res){
Item.markOnSale(req.params.itemId, function(){
res.redirect('/profile');
});
};

5 changes: 5 additions & 0 deletions app/models/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Item.create = function(data, cb){
Item.collection.save(i, cb);
};

Item.markOnSale = function(itemId, cb){
var _id = Mongo.ObjectID(itemId);
Item.collection.update({_id:_id}, {$set: {onSale: true}}, cb);
};

module.exports = Item;

function getNumberOfBids(item, cb){
Expand Down
1 change: 1 addition & 0 deletions app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = function(app, express){
app.delete('/logout', users.logout);
app.get('/profile', users.profile);
app.post('/items', items.create);
app.put('/items/:itemId', items.markonsale);

console.log('Express: Routes Loaded');
};
Expand Down
8 changes: 4 additions & 4 deletions app/static/js/user/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
'use strict';

$(document).ready(function(){
$('button[type=submit]').click(geocodeAndSubmit);
$('#submitItem').click(geocodeAndSubmit);
});

function geocodeAndSubmit(e){
var location = $('input[name=location]').val();
geocode(location, function(name, lat, lng){
$('input[name=location]').val(name);
$('form').append('<input name="lat" value="'+lat+'" type="hidden">');
$('form').append('<input name="lng" value="'+lng+'" type="hidden">');
$('form').submit();
$('#newItem').append('<input name="lat" value="'+lat+'" type="hidden">');
$('#newItem').append('<input name="lng" value="'+lng+'" type="hidden">');
$('#newItem').submit();
});
e.preventDefault();
}
Expand Down
17 changes: 12 additions & 5 deletions app/views/users/profile.jade
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ block content
if user.photo
.profile-photo(style='background-image:url(#{user.photo});')
.col-xs-4
form(role='form', method='post', action='/items')
form#newItem(role='form', method='post', action='/items')
.form-group
label Name
input.form-control(name='name', type='text', placeholder='Red Wine')
Expand All @@ -39,7 +39,7 @@ block content
.form-group
label Description
textarea.form-control(name='description')
button.btn.btn-default(type='submit') Add Vintage
button#submitItem.btn.btn-default(type='submit') Add Vintage
.row
.col-xs-6
table.table
Expand All @@ -48,15 +48,22 @@ block content
th Photo
th Name
th Out for Trade?
th Mark For Sale
th Want to Trade?
tbody
each item in items
unless item.onSale
tr
td.item-photo(style='background-image:url(#{item.photo});')
td= item.name
td= item.isBiddable
td Checkbox + submit button
if(!item.isBiddable)
td No
if(item.isBiddable)
td Yes
td
form(method='post', action='/items/#{item._id}')
input(type='hidden', name='_method', value='put')
button(type='submit') Trade

.col-xs-1
.col-xs-5
table.table
Expand Down
59 changes: 59 additions & 0 deletions test/acceptance/items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* global describe, before, beforeEach, it */

'use strict';

process.env.DB = 'wine-seller-test';

var expect = require('chai').expect,
cp = require('child_process'),
app = require('../../app/index'),
cookie = null,
request = require('supertest');

describe('users', function(){
before(function(done){
request(app).get('/').end(done);
});

beforeEach(function(done){
cp.execFile(__dirname + '/../scripts/clean-db.sh', [process.env.DB], {cwd:__dirname + '/../scripts'}, function(err, stdout, stderr){
request(app)
.post('/login')
.send('email=nodeapptest%2Bbob%40gmail.com&password=1234')
.end(function(err, res){
cookie = res.headers['set-cookie'][0];
done();
});
});
});

describe('post /items', function(){
it('should redirect to the profile page', function(done){
request(app)
.post('/items')
.set('cookie', cookie)
.send('name=Red+Wine&photo=http%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fc%2Fcc%2FFrench_beaujolais_red_wine_bottle.jpg&tags=red%2C+wine&location=Nashville%2C+TN%2C+USA&description=Red+Wine')
.end(function(err, res){
expect(res.status).to.equal(302);
expect(res.headers.location).to.equal('/profile');
done();
});
});
});

describe('put /profile', function(){
it('should redirect to the profile page after item is put on sale', function(done){
request(app)
.post('/items/a00000000000000000000001')
.send('_method=put')
.set('cookie', cookie)
.end(function(err, res){
expect(res.status).to.equal(302);
expect(res.headers.location).to.equal('/profile');
done();
});
});
});

});

17 changes: 1 addition & 16 deletions test/acceptance/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

'use strict';

process.env.DB = 'template-test';
process.env.DB = 'wine-seller-test';

var expect = require('chai').expect,
cp = require('child_process'),
Expand Down Expand Up @@ -89,20 +89,5 @@ describe('users', function(){
});
});
});

describe('post /items', function(){
it('should redirect to the profile page', function(done){
request(app)
.post('/items')
.set('cookie', cookie)
.send('name=Red+Wine&photo=http%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fc%2Fcc%2FFrench_beaujolais_red_wine_bottle.jpg&tags=red%2C+wine&location=Nashville%2C+TN%2C+USA&description=Red+Wine')
.end(function(err, res){
expect(res.status).to.equal(302);
expect(res.headers.location).to.equal('/profile');
done();
});
});
});

});

10 changes: 10 additions & 0 deletions test/unit/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,15 @@ describe('Item', function(){
});
});

describe('.markForSale', function(){
it('should set an item onSale to true', function(done){
Item.markOnSale('a00000000000000000000001', function(){
Item.findById('a00000000000000000000001', function(err, item){
expect(item.onSale).to.be.true;
done();
});
});
});
});
});

0 comments on commit e6ac1fc

Please sign in to comment.