Skip to content

Commit

Permalink
bids are working!
Browse files Browse the repository at this point in the history
  • Loading branch information
Melanie Fryman committed Sep 2, 2014
1 parent fa82bd4 commit e205757
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 51 deletions.
11 changes: 9 additions & 2 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var User = require('../models/user'),
Item = require('../models/item'),
Bid = require('../models/bid'),
Message = require('../models/message'),
moment = require('moment');

Expand Down Expand Up @@ -50,7 +51,11 @@ exports.edit = function(req, res){

exports.dashboard = function(req, res){
Item.findAllByOwner(res.locals.user._id, function(err, items){
res.render('users/dashboard', {items:items, moment:moment});
Bid.findDibs(res.locals.user._id, function(err, dibs){
Bid.findBids(res.locals.user._id, function(err, bids){
res.render('users/dashboard', {items:items, dibs:dibs, bids:bids, moment:moment});
});
});
});
};

Expand All @@ -63,7 +68,9 @@ exports.editProfile = function(req, res){

exports.client = function(req, res){
User.findOne({username:req.params.username}, function(err, client){
res.render('users/client', {client: client});
Item.findAllByOwner(client._id, function(err, items){
res.render('users/client', {client: client, items:items, moment:moment});
});
});
};

Expand Down
33 changes: 33 additions & 0 deletions app/helpers/view-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

var _ = require('lodash');

exports.url = function(query, key, value, text){
var q = _.cloneDeep(query);
q[key] = value;

var link = '<a href="/marketplace?',
properties = Object.keys(q).map(function(prop){
return prop + '=' + q[prop];
});

link += properties.join('&');

link += '">' + text + '</a>';
return link;
};

exports.tags = function(query, tags){
var links = tags.map(function(tag){
return exports.url({}, 'tag', tag, tag);
});

return links.join(', ');
};

exports.sort = function(query, name, display){
var order = query.order ? query.order * -1 : 1,
tag = query.tag ? '&tag=' + query.tag : '',
link = '<a "class"="marketplace-date" "href="/marketplace?sort=' + name + '&order=' + order + tag + '">' + display + '</a>';
return link;
};
53 changes: 49 additions & 4 deletions app/models/bid.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ var Mongo = require('mongodb'),
async = require('async');

function Bid(o){
this.sItem = o.sItem;
this.seller = o.seller;
this.bidder = o.bidder;
this.bItem = o.bItem;
this.sItem = Mongo.ObjectID(o.sItem);
this.seller = Mongo.ObjectID(o.seller);
this.bidder = Mongo.ObjectID(o.bidder);
this.bItem = Mongo.ObjectID(o.bItem);
this.date = new Date();
}

Expand All @@ -23,6 +23,24 @@ Bid.create = function(o, cb){
Bid.countBids = function(itemId, cb){
Bid.collection.count({itemForBidId:itemId, isOpen:true}, cb);
};
// find the "dibs" for the dashboard
Bid.findDibs = function(id, cb){
console.log(id);
var _id = Mongo.ObjectID(id);
console.log(_id);
Bid.collection.find({bidder:_id}).toArray(function(err, dibs){
async.map(dibs, iterator, cb);
});
};
// find the "bids" for the dashboard
Bid.findBids = function(id, cb){
console.log(id);
var _id = Mongo.ObjectID(id);
console.log(_id);
Bid.collection.find({seller:_id}).toArray(function(err, bids){
async.map(bids, iterator2, cb);
});
};

Bid.findById = function(id, cb){
var _id = Mongo.ObjectID(id);
Expand All @@ -41,6 +59,33 @@ Bid.getBids = function(itemForBidId, cb){

module.exports = Bid;

function iterator(dib, cb){
require('./item').findById(dib.sItem, function(err, sItem){
dib.photo = sItem.photo;
dib.name = sItem.name;
require('./user').findById(dib.seller, function(err, seller){
dib.sellerName = seller.username;
require('./item').findById(dib.bItem, function(err, bItem){
dib.bName = bItem.name;
cb(null, dib);
});
});
});
}

function iterator2(bid, cb){
require('./item').findById(bid.sItem, function(err, sItem){
bid.photo = sItem.photo;
bid.name = sItem.name;
require('./user').findById(bid.bidder, function(err, bidder){
bid.bidderName = bidder.username;
require('./item').findById(bid.bItem, function(err, bItem){
bid.bName = bItem.name;
cb(null, bid);
});
});
});
}
// PRIVATE HELPER FUNCTION //
function attachItem(bid, cb){
require('./item').findById(bid.itemOfferedId.toString(), function(err, item){
Expand Down
30 changes: 4 additions & 26 deletions app/models/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ module.exports = Item;

function iterator(item, cb){
require('./user').findById(item.ownerId, function(err, owner){
item.loc = owner.loc;
item.lat = owner.lat;
item.lng = owner.lng;
item.loc = owner.loc;
item.lat = owner.lat;
item.lng = owner.lng;
item.ownerName = owner.username;
cb(null, item);
});
}
Expand All @@ -102,26 +103,3 @@ function getNumBids(item, cb){
cb(null, item);
});
}
/*
// Harder feature (upload photos)
function moveFiles(photos, count, relDir){
var baseDir = __dirname + '/../static',
absDir = baseDir + relDir;
if(!fs.existsSync(absDir)){fs.mkdirSync(absDir);}
var tmpPhotos = photos.map(function(photo, index){
if(!photo.size){return;}
var ext = path.extname(photo.path),
name = count + index + ext,
absPath = absDir + '/' + name,
relPath = relDir + '/' + name;
fs.renameSync(photo.path, absPath);
return relPath;
});
return _.compact(tmpPhotos);
}
*/
2 changes: 1 addition & 1 deletion app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ User.prototype.send = function(receiver, obj, cb){
sendEmail(this.email, receiver.email, 'Message from Dibster', obj.message, cb);
break;
case 'internal':*/
Message.send(this._id, receiver._id, obj.message, cb);
Message.send(this._id, receiver._id, obj.message, cb);
// }
};

Expand Down
3 changes: 2 additions & 1 deletion app/views/items/browse.jade
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ block content
thead
th Picture
th Name
th Owner
th Location
th Date Posted
th Category
Expand All @@ -17,7 +18,7 @@ block content
tr(data-loc=item.loc, data-lat=item.lat, data-lng=item.lng)
td: a(href='/items/#{item._id}'): img(class='pic-show',src= item.photo)
td: a(href='/items/#{item._id}')= item.name
td= item.name
td: a(href='/users/#{item.ownerName}')= item.ownerName
td= item.loc
td= moment(item.datePosted).format('MM/DD/YYYY')
td= item.category
Expand Down
6 changes: 3 additions & 3 deletions app/views/items/showItem.jade
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ block content
if user._id.toString() !== item.ownerId.toString()
.row
.col-xs-12
form(role='form', method='post', action='/bid')
form(role='form', method='post', action='/items/bid')
h2.legend Want this item?
| Offer an item to trade:
.form-group
input.form-control#sItem(type='hidden' name='sItem' value=item._id)
input.form-control#seller(type='hidden' name='seller' value=item.ownerId.toString())
input.form-control#bidder(type='hidden' name='bidder' value=user._id.toString())
input.form-control#seller(type='hidden' name='seller' value=item.ownerId)
input.form-control#bidder(type='hidden' name='bidder' value=user._id)
label(for='bItem') Your items for bid
select.form-control#bItem(name='bItem')
each item in items
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/nav.jade
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
span.badge #{(unread > 0) ? unread : null}
li: a(href='/items/new') Add Item
li.divider
li: a(href='/profile')= user.email
li: a(href='/dashboard')= user.email
li
form.navbar-form(method='post', action='/logout')
.form-group
Expand Down
45 changes: 41 additions & 4 deletions app/views/users/client.jade
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Public profile view
extends ../shared/template
block content
.container
Expand All @@ -7,9 +6,27 @@ block content
.col-xs-12
h1#header #{client.username}'s Profile
.row
.col-xs-5
.panel.panel-primary
.panel-heading
h3.panel-title Contact Information
.panel-body
ul.list-group
li.list-group-item
| Email:&nbsp
| #{client.email}
li.list-group-item
| Phone:&nbsp
| #{client.phone}
li.list-group-item
| Location:&nbsp
| #{client.loc}
.col-xs-2
a.thumbnail(href='#')
img(src=client.photo)
.col-xs-5
.photo
a.thumbnail(href='#')
img(src=client.photo)
.row
.col-xs-4
.panel.panel-primary
.panel-heading
Expand All @@ -23,5 +40,25 @@ block content
textarea.form-control#message(name='message', autofocus=true)
.form-group
button.btn.btn-info.btn-sm(type='submit') Send Message
.col-xs-6
.row
h3 Items
.col-xs-12.user-item-list
span.h3.cap #{client.username}'s
| Items
.table-responsive
table.table.table-hover.user-items
thead
th Picture
th Name
th Date Added
th Category
th Tags
tbody
each item in items
tr
td: a(href='/items/#{item._id}'): img(class='pic-show',src= item.photo)
td: a(href='/items/#{item._id}')= item.name
td= moment(item.datePosted).format('MM/DD/YYYY')
td= item.category
td= item.tags.join(', ')
block scripts
50 changes: 42 additions & 8 deletions app/views/users/dashboard.jade
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Private profile view
extends ../shared/template
block content
.container
Expand All @@ -10,10 +9,7 @@ block content
.col-xs-5
.panel.panel-primary
.panel-heading
//- if user._id === client._id
h3.panel-title: a(href='/profile/edit') Edit Profile
//- else
//- h3.panel-title Contact Infomation
.panel-body
ul.list-group
li.list-group-item
Expand Down Expand Up @@ -44,8 +40,6 @@ block content
th Category
th Description
th Tags
th isAvailable?
th Remove
tbody
each item in items
tr
Expand All @@ -55,10 +49,50 @@ block content
td= item.category
td= item.description
td= item.tags.join(', ')
td= item.isAvailable
//- if user._id === owner._id
td
form(action='/items/#{item._id}', method='POST', onsubmit="return confirm('Are you sure you want to delete item?')")
input(type='hidden', name='_method', value='delete')
button.alert.del-but x
.row
h3 Dibs
.col-xs-12.user-item-list
span.h3.cap #{user.username}'s
| Dibs
.table-responsive
table.table.table-hover.user-items
thead
th Picture
th Name
th Seller
th Your Item
th Date
tbody
each dib in dibs
tr
td: a(href='#{dib.photo}'): img(class='pic-show', src= dib.photo)
td: a(href='/items/#{dib.sItem}')= dib.name
td: a(href='/users/#{dib.sellerName}')= dib.sellerName
td: a(href='/items/#{dib.bItem}')= dib.bName
td= moment(dib.date).format('MM/DD/YYYY')
.row
h3 Bids
.col-xs-12.user-item-list
span.h3.cap #{user.username}'s
| Bids
.table-responsive
table.table.table-hover.user-items
thead
th Picture
th Name
th Bidder
th Their Item
th Date
tbody
each bid in bids
tr
td: a(href='{bid.photo}'): img(class='pic-show', src= bid.photo)
td: a(href='/items/#{bid.sItem}')= bid.name
td: a(href='/users/#{bid.bidderName}')= bid.bidderName
td: a(href='/items/#{bid.bItem}')= bid.bName
td= moment(bid.date).format('MM/DD/YYYY')
block scripts
2 changes: 1 addition & 1 deletion test/acceptance/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('users', function(){
});
});

describe('get /profile/edit', function(){
describe('get /profile/edit', function(){
it('should show the edit profile page', function(done){
request(app)
.get('/profile/edit')
Expand Down

0 comments on commit e205757

Please sign in to comment.