Skip to content

Commit

Permalink
Merge pull request #3 from LizaHCarter/usersetup
Browse files Browse the repository at this point in the history
Usersetup
  • Loading branch information
LizaHCarter committed Aug 30, 2014
2 parents f847078 + 0a87ec5 commit 4022adf
Show file tree
Hide file tree
Showing 23 changed files with 411 additions and 84 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
## Wine Seller
### Code Badges
build status icon
coverage status icon
[![Build Status](https://travis-ci.org/LizaHCarter/wine-seller.svg)](https://travis-ci.org/LizaHCarter/wine-seller)
[![Coverage Status](https://coveralls.io/repos/LizaHCarter/wine-seller/badge.png)](https://coveralls.io/r/LizaHCarter/wine-seller)

### Description
An app for sommeliers to trade vintage and rare wines.

### Models
```
Model 1
User
```

```
Model 2
Item
```

```
Bid
```

### Database
```
Collecion 1
Users
```

```
Items
```

```
Collection 2
Bids
```

### Features
Expand All @@ -37,7 +45,7 @@ $ npm test

### Contributors
- [Liza Carter](https://github.com/lizahcarter)
- [DS](https://github.com/dsroden)
- [Daniel Roden](https://github.com/dsroden)
- [Mark Supalla](https://github.com/marksupalla)
- [Adam Barnhard](https://github.com/abarnhard)

Expand Down
11 changes: 11 additions & 0 deletions app/controllers/items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

var Item = require('../models/item');

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

9 changes: 8 additions & 1 deletion 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');
var User = require('../models/user'),
Item = require('../models/item');

exports.new = function(req, res){
res.render('users/new');
Expand Down Expand Up @@ -41,3 +42,9 @@ exports.authenticate = function(req, res){
});
};

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

34 changes: 10 additions & 24 deletions app/models/bid.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
'use strict';

var bcrypt = require('bcrypt'),
Mongo = require('mongodb');
var Mongo = require('mongodb');

function User(){
function Bid(){
}

Object.defineProperty(User, 'collection', {
get: function(){return global.mongodb.collection('users');}
Object.defineProperty(Bid, 'collection', {
get: function(){return global.mongodb.collection('bids');}
});

User.findById = function(id, cb){
var _id = Mongo.ObjectID(id);
User.collection.findOne({_id:_id}, cb);
};

User.register = function(o, cb){
User.collection.findOne({email:o.email}, function(err, user){
if(user){return cb();}
o.password = bcrypt.hashSync(o.password, 10);
User.collection.save(o, cb);
});
Bid.countItemBids = function(itemId, cb){
Bid.collection.count({itemUpForBidId:itemId, isOpen:true}, cb);
};

User.authenticate = function(o, cb){
User.collection.findOne({email:o.email}, function(err, user){
if(!user){return cb();}
var isOk = bcrypt.compareSync(o.password, user.password);
if(!isOk){return cb();}
cb(user);
});
Bid.findById = function(id, cb){
var _id = Mongo.ObjectID(id);
Bid.collection.findOne({_id:_id}, cb);
};

module.exports = User;
module.exports = Bid;

55 changes: 34 additions & 21 deletions app/models/item.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
'use strict';

var bcrypt = require('bcrypt'),
Mongo = require('mongodb');

function User(){
var Mongo = require('mongodb'),
async = require('async'),
Bid = require('./bid');

function Item(o){
this.name = o.name;
this.location = o.location;
this.lat = o.lat * 1;
this.lng = o.lng * 1;
this.description = o.description;
this.tags = o.tags.split(',').map(function(s){return s.trim();});
this.photo = o.photo;
this.ownerId = o.ownerId;
this.isBiddable = false;
this.onSale = false;
this.datePosted = new Date();
}

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

User.findById = function(id, cb){
Item.findById = function(id, cb){
var _id = Mongo.ObjectID(id);
User.collection.findOne({_id:_id}, cb);
Item.collection.findOne({_id:_id}, cb);
};

User.register = function(o, cb){
User.collection.findOne({email:o.email}, function(err, user){
if(user){return cb();}
o.password = bcrypt.hashSync(o.password, 10);
User.collection.save(o, cb);
Item.findAllForUser = function(userId, cb){
Item.collection.find({ownerId:userId}).toArray(function(err, items){
async.map(items, getNumberOfBids, cb);
});
};

User.authenticate = function(o, cb){
User.collection.findOne({email:o.email}, function(err, user){
if(!user){return cb();}
var isOk = bcrypt.compareSync(o.password, user.password);
if(!isOk){return cb();}
cb(user);
});
Item.create = function(data, cb){
var i = new Item(data);
Item.collection.save(i, cb);
};

module.exports = User;
module.exports = Item;

function getNumberOfBids(item, cb){
Bid.countItemBids(item._id, function(err, count){
item.numBids = count;
cb(null, item);
});
}

3 changes: 3 additions & 0 deletions app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var morgan = require('morgan'),
security = require('../lib/security'),
debug = require('../lib/debug'),
home = require('../controllers/home'),
items = require('../controllers/items'),
users = require('../controllers/users');

module.exports = function(app, express){
Expand All @@ -30,6 +31,8 @@ module.exports = function(app, express){

app.use(security.bounce);
app.delete('/logout', users.logout);
app.get('/profile', users.profile);
app.post('/items', items.create);

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.

13 changes: 11 additions & 2 deletions app/static/css/style.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
@base: #f938ab;

body{
background-color: saturate(@base, 5%);
}

.profile-photo {
height: 200px;
width: 150px;
background-size: cover;
}

.item-photo {
height: 100px;
width: 100px;
background-size: cover;
}
34 changes: 34 additions & 0 deletions app/static/js/user/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* jshint unused:false, camelcase:false */
/* global google */

(function(){
'use strict';

$(document).ready(function(){
$('button[type=submit]').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();
});
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);
});
}

})();

12 changes: 0 additions & 12 deletions app/views/home/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ block content
h2 Home
.panel.panel-default
.panel-body
h4 Bootstrap Themes and Templates
ul
li: a(href='http://bootswatch.com/', target='_blank') http://bootswatch.com/
li: a(href='http://bootstrapzero.com/', target='_blank') http://bootstrapzero.com/
li: a(href='http://startbootstrap.com/', target='_blank') http://startbootstrap.com/
li: a(href='http://www.prepbootstrap.com/', target='_blank') http://www.prepbootstrap.com/
li: a(href='http://www.blacktie.co/', target='_blank') http://www.blacktie.co/
li: a(href='https://bootstrapmaster.com/', target='_blank') https://bootstrapmaster.com/
li: a(href='http://bootstrapstyler.com/', target='_blank') http://bootstrapstyler.com/
h4 Favicon Generators
ul
li: a(href='http://www.favicon.cc/', target='_blank') http://www.favicon.cc/

block scripts
script(src='/js/user/home.js')
Expand Down
8 changes: 7 additions & 1 deletion app/views/shared/nav.jade
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
span.icon-bar
span.icon-bar
span.icon-bar
a.navbar-brand(href='/') Template
a.navbar-brand(href='/') Wine Seller

.collapse.navbar-collapse#bs-navbar-collapse
ul.nav.navbar-nav.navbar-right
if user
li: a(href='/marketplace') Marketplace
li
a(href='/messages')
span Messages
span.badge 0
li: a(href='/profile') Profile
li
form.navbar-form(method='post', action='/logout')
.form-group
Expand Down
3 changes: 2 additions & 1 deletion app/views/shared/template.jade
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ html(lang='en')
meta(charset='utf-8')
meta(http-equiv='X-UA-Compatible', content='IE=edge')
meta(name='viewport', content='width=device-width, initial-scale=1')
title Template
title Wine Seller
link(href='/bootstrap/css/bootstrap.min.css', rel='stylesheet')
link(rel='stylesheet', href='/css/style.css')
body
Expand All @@ -20,6 +20,7 @@ html(lang='en')
script(src='/js/vendor/underscore-contrib.min.js')
script(src='/js/vendor/moment.min.js')
script(src='/bootstrap/js/bootstrap.min.js')
script(src='https://maps.googleapis.com/maps/api/js?key=AIzaSyAL8EZ1bCRT00vK8W8oALBD_LUlpEQvHKQ&sensor=true')
script(src='/js/user/main.js')

block scripts
Expand Down
11 changes: 10 additions & 1 deletion app/views/users/new.jade
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ block content
.row
.col-xs-6
form(role='form', method='post', action='/register')
.form-group
label(for='name') Name
input.form-control#name(type='text', name='name', autofocus=true)
.form-group
label(for='photo') Photo
input.form-control#photo(type='text', name='photo', placeholder='http://example.com/pic.jpg')
.form-group
label(for='email') Email
input.form-control#email(type='email', name='email', autofocus=true)
input.form-control#email(type='email', name='email')
.form-group
label(for='phone') Phone
input.form-control#phone(type='text', name='phone', placeholder='xxx-xxx-xxxx')
.form-group
label(for='password') Password
input.form-control#password(type='password', name='password')
Expand Down
Loading

0 comments on commit 4022adf

Please sign in to comment.