diff --git a/app/controllers/items.js b/app/controllers/items.js index 3f47c37..d3ca5de 100644 --- a/app/controllers/items.js +++ b/app/controllers/items.js @@ -15,9 +15,15 @@ exports.index = function(req, res){ }); }; -exports.markonsale = function(req, res){ +exports.markOnSale = function(req, res){ Item.markOnSale(req.params.itemId, function(){ res.redirect('/profile'); }); }; +exports.itemBidPage= function(req, res){ + Item.findTradeAndBiddableItems(req.params.itemId, res.locals.user._id, function(err,item, biddableItems){ + res.render('items/show-bid', {item:item, biddableItems:biddableItems}); + }); +} + diff --git a/app/models/item.js b/app/models/item.js index 22a2251..8d9997c 100644 --- a/app/models/item.js +++ b/app/models/item.js @@ -13,7 +13,7 @@ function Item(o){ this.tags = o.tags.split(',').map(function(s){return s.trim();}); this.photo = o.photo; this.ownerId = o.ownerId; - this.isBiddable = false; + this.isBiddable = true; this.onSale = false; this.datePosted = new Date(); } @@ -50,6 +50,14 @@ Item.markOnSale = function(itemId, cb){ Item.collection.update({_id:_id}, {$set: {onSale: true}}, cb); }; +Item.findTradeAndBiddableItems = function(itemId, userId, cb){ + Item.findById(itemId, function(err, itemForTrade){ + Item.collection.find({ownerId:userId, isBiddable: true}).toArray(function(err, bidItems){ + cb(null, itemForTrade, bidItems); + }); + }); +}; + module.exports = Item; function getNumberOfBids(item, cb){ diff --git a/app/routes/routes.js b/app/routes/routes.js index 222bf62..eec387b 100644 --- a/app/routes/routes.js +++ b/app/routes/routes.js @@ -36,7 +36,8 @@ module.exports = function(app, express){ app.get('/profile', users.profile); app.post('/items', items.create); app.get('/marketplace', items.index); - app.put('/items/:itemId', items.markonsale); + app.put('/items/:itemId', items.markOnSale); + app.get('/bid/:itemId', items.itemBidPage); console.log('Express: Routes Loaded'); }; diff --git a/db/bids.json b/db/bids.json index 60cf8db..c20c739 100644 --- a/db/bids.json +++ b/db/bids.json @@ -6,3 +6,13 @@ "offerOwnerId" : {"$oid":"000000000000000000000002"}, "isOpen" : true } +{ + "_id" : {"$oid":"b00000000000000000000002"}, + "itemUpForBidId" : {"$oid":"a00000000000000000000002"}, + "itemOfferedId" : {"$oid":"a00000000000000000000004"}, + "upForBidOwnerId" : {"$oid":"000000000000000000000001"}, + "offerOwnerId" : {"$oid":"000000000000000000000002"}, + "isOpen" : true +} + + diff --git a/db/items.json b/db/items.json index 0839ac6..4b498ea 100644 --- a/db/items.json +++ b/db/items.json @@ -35,9 +35,54 @@ "description" : "Wine that is kinda red, kinda white", "tags" : ["rose", "wine"], "ownerId" : {"$oid":"000000000000000000000002"}, + "isBiddable" : false, + "onSale" : false, + "photo" : "http://www.calliopewines.com/img/wines/2010-rose-bottle-large.jpg", + "datePosted" : {"$date":477295200000} +} +{ + "_id" : {"$oid":"a00000000000000000000004"}, + "name" : "Green Wine", + "location" : "Nashville, TN, USA", + "lat" : 36.166667, + "lng" : -85.78333299999997, + "description" : "Wine that is green", + "tags" : ["green", "wine"], + "ownerId" : {"$oid":"000000000000000000000002"}, + "isBiddable" : false, + "onSale" : false, + "photo" : "http://www.calliopewines.com/img/wines/2010-rose-bottle-large.jpg", + "datePosted" : {"$date":477295200000} +} +{ + "_id" : {"$oid":"a00000000000000000000005"}, + "name" : "Blue Wine", + "location" : "Nashville, TN, USA", + "lat" : 36.166667, + "lng" : -85.78333299999997, + "description" : "Wine that is blue", + "tags" : ["blue", "wine"], + "ownerId" : {"$oid":"000000000000000000000002"}, "isBiddable" : true, "onSale" : false, "photo" : "http://www.calliopewines.com/img/wines/2010-rose-bottle-large.jpg", "datePosted" : {"$date":477295200000} } +{ + "_id" : {"$oid":"a00000000000000000000006"}, + "name" : "Orange Wine", + "location" : "Nashville, TN, USA", + "lat" : 36.166667, + "lng" : -85.78333299999997, + "description" : "Wine that is orange", + "tags" : ["blue", "wine"], + "ownerId" : {"$oid":"000000000000000000000002"}, + "isBiddable" : false, + "onSale" : true, + "photo" : "http://www.calliopewines.com/img/wines/2010-rose-bottle-large.jpg", + "datePosted" : {"$date":477295200000} +} + + + diff --git a/test/acceptance/items.js b/test/acceptance/items.js index 463d960..5e89b71 100644 --- a/test/acceptance/items.js +++ b/test/acceptance/items.js @@ -69,5 +69,18 @@ describe('users', function(){ }); }); + describe('get /bid/:itemId', function(){ + it('should display the bid page for a specific item', function(done){ + request(app) + .get('/bid/a00000000000000000000006') + .set('cookie', cookie) + .end(function(err, res){ + expect(res.status).to.equal(200); + expect(res.text).to.include('Orange Wine'); + done(); + }); + }); + }); + }); diff --git a/test/unit/item.js b/test/unit/item.js index 0112e31..af1319d 100644 --- a/test/unit/item.js +++ b/test/unit/item.js @@ -56,7 +56,7 @@ describe('Item', function(){ var id = Mongo.ObjectID('000000000000000000000001'); Item.findAllForUser(id, function(err, items){ expect(items).to.have.length(2); - expect(items[1].numBids).to.equal(1); + expect(items[1].numBids).to.equal(2); done(); }); }); @@ -65,7 +65,7 @@ describe('Item', function(){ describe('.findForSale', function(){ it('should find items in database that are for sale based on query parameters', function(done){ Item.findForSale({}, function(err, items){ - expect(items).to.have.length(1); + expect(items).to.have.length(2); expect(items[0].name).to.equal('White Wine'); done(); }); @@ -83,5 +83,17 @@ describe('Item', function(){ }); }); + describe('.findTradeAndBiddableItems', function(){ + it('should find an item up for trade and also all biddable items of current user', function(done){ + var userId = Mongo.ObjectID('000000000000000000000002'); + Item.findTradeAndBiddableItems('a00000000000000000000002',userId, function(err, item, biddableItems){ + expect(item.onSale).to.be.true; + expect(item.name).to.equal('White Wine'); + expect(biddableItems).to.have.length(1); + done(); + }); + }); + }); + });