Skip to content

Commit

Permalink
fixed conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
marksupalla committed Aug 30, 2014
2 parents 1ef3605 + f08afd4 commit 32fc7a8
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 44 deletions.
12 changes: 12 additions & 0 deletions app/controllers/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ exports.create = function(req, res){
});
};

exports.index = function(req, res){
Item.findForSale(req.query, function(err, items){
res.render('items/index', {items:items});
});
};

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

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

Item.findForSale = function(query, cb){
var filter = {onSale:true},
sort = {};
if(query.sort){sort[query.sort] = query.order * 1;}
Item.collection.find(filter).sort(sort).toArray(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
2 changes: 2 additions & 0 deletions app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module.exports = function(app, express){
app.put('/profile', users.update);
app.get('/profile', users.profile);
app.post('/items', items.create);
app.get('/marketplace', items.index);
app.put('/items/:itemId', items.markonsale);

console.log('Express: Routes Loaded');
};
Expand Down
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.

4 changes: 4 additions & 0 deletions app/static/css/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ body{
width: 100px;
background-size: cover;
}

#marketplace-map {
height: 400px;
}
Binary file added app/static/img/marker.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions app/static/js/user/items-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* jshint unused:false, camelcase:false */
/* global google, cartographer, addMarker */

(function(){
'use strict';

var map;

$(document).ready(function(){
map = cartographer('marketplace-map', 39.8282, -98.5795, 4);
var rows = $('.item').toArray();
// console.log(rows);
if(rows.length){
rows.forEach(function(row){
// console.log(row);
addMarker(map, parseFloat($(row).attr('data-lat')), parseFloat($(row).attr('data-lng')), $(row).attr('data-loc'), '/img/marker.png');
});
}
});

})();

29 changes: 25 additions & 4 deletions app/static/js/user/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
(function(){
'use strict';
/* jshint unused:false, camelcase:false */
/* global google */

$(document).ready(function(){
function geocode(address, cb){
'use strict';
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address:address}, function(results, status){
//console.log('results', results);
var name = results[0].formatted_address,
lat = results[0].geometry.location.lat(),
lng = results[0].geometry.location.lng();
cb(name, lat, lng);
});
}

})();
function cartographer(cssId, lat, lng, zoom){
'use strict';
var mapOptions = {center: new google.maps.LatLng(lat, lng), zoom: zoom, mapTypeId: google.maps.MapTypeId.ROADMAP},
map = new google.maps.Map(document.getElementById(cssId), mapOptions);

return map;
}

function addMarker(map, lat, lng, name, icon){
'use strict';
var latLng = new google.maps.LatLng(lat, lng);
// console.log(latLng);
new google.maps.Marker({map: map, position: latLng, title: name, animation: google.maps.Animation.DROP, icon: icon});
}
21 changes: 5 additions & 16 deletions app/static/js/user/profile.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
/* jshint unused:false, camelcase:false */
/* global google */
/* global google, geocode */

(function(){
'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();
}

function geocode(address, cb){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address:address}, function(results, status){
//console.log('results', results);
var name = results[0].formatted_address,
lat = results[0].geometry.location.lat(),
lng = results[0].geometry.location.lng();
cb(name, lat, lng);
});
}

})();

28 changes: 28 additions & 0 deletions app/views/items/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extends ../shared/template
block content
h2 Profile
.panel.panel-default
.panel-body
.row
.col-xs-12
#marketplace-map
.row
.col-xs-12
table.table
thead
tr
th Photo
th Name
th Location
th Description
tbody
each item in items
tr.item(data-loc='#{item.location}', data-lat='#{item.lat}', data-lng='#{item.lng}')
td.item-photo(style='background-image:url(#{item.photo})')
td: a(href='/items/#{item._id}')= item.name
td= item.location
td= item.description

block scripts
script(src='/js/user/items-index.js')

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
73 changes: 73 additions & 0 deletions test/acceptance/items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* 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('get /marketplace', function(){
it('should display the marketplace page', function(done){
request(app)
.get('/marketplace')
.set('cookie', cookie)
.end(function(err, res){
expect(res.status).to.equal(200);
expect(res.text).to.include('Marketplace');
expect(res.text).to.include('White Wine');
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: 0 additions & 17 deletions test/acceptance/npm-debug.log

This file was deleted.

2 changes: 1 addition & 1 deletion 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
21 changes: 21 additions & 0 deletions test/unit/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,26 @@ 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[0].name).to.equal('White Wine');
done();
});
});
});

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 32fc7a8

Please sign in to comment.