Skip to content

Commit

Permalink
working on maps. tests are passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Melanie Fryman committed Sep 1, 2014
1 parent 3d47848 commit ecd1564
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 94 deletions.
26 changes: 23 additions & 3 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

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

Expand Down Expand Up @@ -54,12 +55,31 @@ exports.editProfile = function(req, res){
});
};

exports.newItem = function(req, res){
res.render('items/new');
};

exports.saveItem = function(req, res){
Item.create(res.locals.user._id, req.body, function(){
res.redirect('/items');
});
};

exports.profile = function(req, res){
res.render('users/profile');
Item.findAllByOwner(res.locals.user._id, function(err, items){
res.render('users/profile', {items:items, moment:moment});
});
};

exports.browse = function(req, res){
Item.browse({isAvailable:true}, function(err, items){
console.log(items);
res.render('items/browse', {items:items, moment:moment});
});
};

exports.client = function(req, res){
User.findOne({name:req.params.username}, function(err, client){
User.findOne({username:req.params.username}, function(err, client){
res.render('users/client', {client: client});
});
};
Expand All @@ -68,7 +88,7 @@ exports.client = function(req, res){
exports.send = function(req, res){
User.findById(req.params.userId, function(err, receiver){
res.locals.user.send(receiver, req.body, function(){
res.redirect('/user/' + receiver.email);
res.redirect('/users/' + receiver.username);
});
});
};
Expand Down
24 changes: 16 additions & 8 deletions app/models/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@ var Mongo = require('mongodb'),
Bid = require('./bid'),
async = require('async');

function Item(o){
function Item(userId, o){
this.name = o.name;
this.description = o.description;
this.photo = o.photo;
this.category = o.category;
this.tags = o.tags.split(',').map(function(s){return s.trim();});
this.tags = _.compact(this.tags);
this.datePosted = new Date();
this.ownerId = Mongo.ObjectID(o.ownerId);
this.ownerId = Mongo.ObjectID(userId);

// private data properties
this.isAvailable = true; // item is available in inventory
this.isForSale = false; // item is currently on sale, multiple bids can occur per 1 item
this.isOffered = false; // item has been offered to another user to trade & is currently NOT available
}

Object.defineProperty(Item, 'collection', {
get: function(){return global.mongodb.collection('items');}
});

Item.create = function(o, cb){
Item.create = function(id, o, cb){
console.log('------ MODEL OBJECT ------');
console.log(o);
var item = new Item(o);
var item = new Item(id, o);
console.log('------ MODEL ITEM ------');
console.log(item);
Item.collection.save(item, cb);
Expand All @@ -45,9 +44,10 @@ Item.findAllByOwner = function(userId, cb){
});
};

Item.findAvailable = function(userId, cb){
var ownerId = Mongo.ObjectID(userId);
Item.collection.find({ownerId:ownerId, isAvailable:true}).toArray(cb);
Item.browse = function(filter, cb){
Item.collection.find(filter).toArray(function(err, items){
async.map(items, iterator, cb);
});
};

Item.destroy = function(id, cb){
Expand All @@ -59,6 +59,14 @@ module.exports = Item;

// PRIVATE HELPER FUNCTIONS //

function iterator(item, cb){
require('./user').findById(item.ownerId, function(err, owner){
item.loc = owner.loc;
item.lat = owner.lat;
item.lng = owner.lng;
cb(null, item);
});
}

function getNumBids(item, cb){
Bid.countBids(item._id, function(err, count){
Expand Down
9 changes: 5 additions & 4 deletions app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,22 @@ User.prototype.messages = function(cb){
};

User.prototype.send = function(receiver, obj, cb){
switch(obj.mtype){
/* switch(obj.mtype){
case 'text':
sendText(receiver.phone, obj.message, cb);
break;
case 'email':
sendEmail(this.email, receiver.email, 'Message from Dibster', obj.message, cb);
break;
case 'internal':
case 'internal':*/
Message.send(this._id, receiver._id, obj.message, cb);
}
// }
};

module.exports = User;

// PRIVATE HELPER FUNCTIONS //

/*
function sendText(to, body, cb){
if(!to){return cb();}
Expand All @@ -108,3 +108,4 @@ function sendEmail(from, to, subject, message, cb){
mailgun.messages().send(data, cb);
}
*/
10 changes: 5 additions & 5 deletions app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ module.exports = function(app, express){
app.get('/profile/edit', users.edit);
app.put('/profile', users.editProfile);
app.get('/profile', users.profile);
app.get('/user/:username', users.client);
app.get('/users/:username', users.client);
app.post('/message/:userId', users.send);
app.get('/message/:msgId', users.readMessage);
app.get('/messages', users.displayMessages);
/*
app.get('/items/new', items.new);
app.post('/items', items.create);
*/
app.get('/items/new', users.newItem);
app.post('/items', users.saveItem);
app.get('/browse', users.browse);

console.log('Express: Routes Loaded');
};

2 changes: 1 addition & 1 deletion app/static/css/style.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions app/static/css/style.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
@base: #f938ab;

body{
background-color: saturate(@base, 5%);
.photo{
background-size: cover;
display: inline-block;
width: 200px;
height: 200px;
margin: 10px 10px 0px 0px;
}

.page-header{
margin-top:65px;
padding-bottom:0px;
}
Empty file added app/static/js/user/browse.js
Empty file.
10 changes: 5 additions & 5 deletions app/static/js/user/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
$('form').submit(getLocation);
});
function getLocation(e){
var lat = $('#locationLat').val();
var lat = $('#lat').val();
if(!lat){
var name = $('#location').val();
var name = $('#loc').val();
geocode(name);
e.preventDefault();
}
Expand All @@ -21,9 +21,9 @@
var loc = results[0].formatted_address,
lat = results[0].geometry.location.lat(),
lng = results[0].geometry.location.lng();
$('#location').val(loc);
$('#locationLat').val(lat);
$('#locationLng').val(lng);
$('#loc').val(loc);
$('#lat').val(lat);
$('#lng').val(lng);
$('form').submit();
console.log(name, lat, lng);
});
Expand Down
38 changes: 0 additions & 38 deletions app/views/item/new.jade

This file was deleted.

20 changes: 20 additions & 0 deletions app/views/items/browse.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extends ../shared/template
block content
h2 Browse da itumz
#map
table
thead
th name
th picture
th category
th tags
th date
tbody
each item in items
tr
td= item.name
td.photo.thumbnail(style='background-image:url(#{item.photo})')
td= item.category
td= item.tags
td= moment(item.datePosted).format('MM/DD/YYYY')

37 changes: 37 additions & 0 deletions app/views/items/new.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Edit profile view
extends ../shared/template
block content
.container
#banner.page-header
.row
.col-xs-12
h1#header Add an Item to Sell
.row
.col-xs-6
form(role='form', method='post', action='/items')
.form-group
label(for='name') Name
input.form-control#name(type='text', name='name', autofocus=true)
.form-group
label(for='description') Description
input.form-control#description(type='text', name='description')
.form-group
label(for='photo') Photo
input.form-control#photo(type='text', name='photo')
.form-group
label(for='category') Category
input.form-control#category(type='text', name='category')
.form-group
label(for='tags') Tags
input.form-control#tags(type='text', name='tags')
button.btn.btn-success(type='submit') Add Item
.col-xs-6

block scripts







Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ block content
td= item.name
td=item.description
img(src=item.photo)
td= user.Location1
td= user.Location
td=item.category
td=item.tags
.row
form(method='post', action='')
button(type='submit', class='btn btn-default') Bid
button.btn.btn-primary(type='submit') Bid

block scripts
8 changes: 4 additions & 4 deletions app/views/users/edit.jade
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ block content
label(for='photo') Photo
input.form-control#photo(type='text', name='photo', value=user.photo)
.form-group
label(for='location[name]') Location
input#location.form-control(type='text', placeholder='Nashville, TN' ='location[name]', value=user.location.name)
input#locationLat.form-control(type='hidden', name='location[lat]')
input#locationLng.form-control(type='hidden', name='location[lng]')
label(for='photo') Location
input.form-control#loc(type='text', name='loc', value=user.loc)
input.form-control#lat(type='hidden', name='lat')
input.form-control#lng(type='hidden', name='lng')
button.btn.btn-success(type='submit') Edit Profile
.col-xs-6

Expand Down
24 changes: 23 additions & 1 deletion app/views/users/profile.jade
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,32 @@ block content
| #{user.phone}
li.list-group-item
| Location:&nbsp
a(href=user.location.name, target='_blank')= user.location.name
| #{user.loc}
.col-xs-2
.col-xs-5
.photo
a.thumbnail(href='#')
img(src=user.photo)
.row
h3 Items (rob schneider)
.col-xs-12
table
thead
th name
th description
th photo
th category
th tags
th date posted
th isAvailable?
tbody
each item in items
tr
td: a(href='items/#{item._id}')= item.name
td= item.description
td.photo.thumbnail(style='background-image:url(#{item.photo})')
td= item.category
td= item.tags
td= moment(item.datePosted).format('MM/DD/YYYY')
td= isAvailable
block scripts
Loading

0 comments on commit ecd1564

Please sign in to comment.