Skip to content

Commit

Permalink
updated the asset publishing steps
Browse files Browse the repository at this point in the history
  • Loading branch information
piatra committed Jul 16, 2013
1 parent cca133d commit 7c37785
Show file tree
Hide file tree
Showing 19 changed files with 239 additions and 119 deletions.
3 changes: 3 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ app.get('/newasset', routes.newasset);
app.post('/newasset/process/', assets.processAsset);
app.post('/newasset/save', assets.saveAsset);

app.get('/assets/created', assets.getUserAssets);
app.get('/assets/:count', assets.getLatestAssets);

http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
30 changes: 29 additions & 1 deletion lib/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ module.exports = function (options) {
});
}


function setAssetProvider (message, email, cb) {
user.get({owner:1}, {email:email || '[email protected]'}, function (err, doc) {
if (err) {
Expand Down Expand Up @@ -137,9 +136,38 @@ module.exports = function (options) {
db.save.asset(graph, cb);
}

function getUserAssets (query, cb) {
db.get.userAssets(query, function (err, assets) {
if (err) {
console.log(err);
cb({'error':err});
} else {
var docs = [];
var l = assets.length;
assets.forEach(function (asset) {
db.get.userListings(asset['_id'], function (err, doc) {
if (err) {
console.log(err);
cb({'error':err});
} else {
docs.push({
asset: asset,
listing: doc[0] // its just one el [doc]
})
}
if (--l == 0) {
cb(docs);
}
})
})
}
})
}

// sign is a facade for creating + signing
assetUtils.sign = sign;
assetUtils.save = saveAssetAndListing;
assetUtils.getUserAssets = getUserAssets;

return assetUtils;
};
50 changes: 41 additions & 9 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,38 @@ else
var schemas = require('./schemas')(db);

var dbUtils = {
find: function (schema, query, fields, cb) {
if (typeof fields === 'function') {
cb = fields;
fields = {};
}
schema.find(query, fields, cb);
},
next: function (cb) {
return function (err, doc) {
if (err) {
console.log(err);
cb(err);
} else {
cb(null, doc);
}
}
},
save : {
asset : function (graph, cb) {
var listing = new schemas.Listing(graph.listing);
console.log(graph.asset);
var asset = new schemas.Asset(graph.asset);

listing.save(function (err) {
asset.save(function (err, d) {
if (err) cb(err);
else asset.save(function (err) {
if (err) cb(err);
else cb(null);
})
else {
listing.assetId = d['_id'];
listing.save(function (err) {
if (err) cb(err);
else cb(null);
})
}
});
}
},
Expand All @@ -45,14 +65,26 @@ var dbUtils = {
},
get : {
user: function (fields, user, cb) {
schemas.User.findOne(user, fields, function (err, docs) {
console.log('get ', fields, 'from', user, '=>', docs);
schemas.User.findOne(user, fields, dbUtils.next(cb));
},
userAssets: function (query, cb) {
dbUtils.get.user({
owner: 1
}, query, function (err, doc) {
if (err) {
console.log(err);
cb(err);
} else {
cb (null, docs);
console.log(doc);
console.log(query);
schemas.Asset.find({
assetProvider: doc.owner
}, dbUtils.next(cb));
}
});
})
},
userListings: function (id, cb) {
dbUtils.find(schemas.Listing, {assetId: id}, dbUtils.next(cb));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var AssetSchema = {

var ListingSchema = {
id: String,
assetId: Schema.Types.ObjectId,
type: Array,
vendor: String,
payee: Array,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpayments-marketplace",
"version": "0.0.1-9",
"version": "0.0.1-12",
"author": "@ndreio Andrei Oprea",
"description": "An implementation using the payswarm module",
"private": true,
Expand All @@ -16,7 +16,7 @@
"jade": "*",
"stylus": "*",
"request": "~2.21.0",
"payswarm": "~0.10.3",
"payswarm": "*",
"async": "~0.2.9",
"mongoose": "~3.6.11",
"q": "~0.9.6",
Expand All @@ -30,4 +30,4 @@
"devDependencies": {
"requirejs": "~2.1.6"
}
}
}
42 changes: 34 additions & 8 deletions public/javascripts/event-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ define(['modal', 'message'], function (modal, message) {
}
],
form: '/newasset/save'
}, {width: '700px', 'margin-left':'-350px'}).appendTo('body');
}).appendTo('body');

$('.js-handler--publish-asset').on('click', assets.publish);
}

function setAssetCount (count) {
$('.topcoat-notification').text(count);
}

function errorHandler (err) {
Expand All @@ -32,20 +35,25 @@ define(['modal', 'message'], function (modal, message) {
}

function assetPublished (asset) {
console.log('created');
message.show({
message: 'Asset has been created. Click here to see it',
href: asset.href
modal.remove();
var $msg = message.show('Your asset has been created. View your assets in the top right corner')
$msg.appendTo('.messages');
}

function setListAssets (docs) {
docs.forEach(function (d) {
if (!d.listing) return;
var title = d.asset.title + '<br><em><small>' + d.listing.payee[0].comment + '</small></em>';
$('<li></li>').addClass('topcoat-list__item')
.html(title).appendTo('.js-handler--asset-list');
});
}

var assets = {
create: function (e) {
console.log('create!!!');
e.preventDefault();
var $form = $('form');
var data = $('form').serialize();
$.post($form.attr('action'), data)
$.post($('form').attr('action'), data)
.done(assetCreated)
.fail(errorHandler)
;
Expand All @@ -59,6 +67,24 @@ define(['modal', 'message'], function (modal, message) {
.done(assetPublished)
.fail(errorHandler)
;
},
count: function (email) {
$.get(location.origin + '/assets/created')
.done(function (resp) {
setAssetCount(resp.length);
setListAssets(resp);
})
.fail(errorHandler)
;
},
loadLatest: function () {
$.get(location.origin + '/assets/5')
.done(function (resp) {
setAssetCount(resp.length);
setListAssets(resp);
})
.fail(errorHandler)
;
}
}

Expand Down
22 changes: 18 additions & 4 deletions public/javascripts/event-handlers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
define(['message', 'event-login', 'modal', 'event-assets'], function (message, loginEv, modal, assetsEv) {
define([
'message',
'event-login',
'event-assets'
], function (message, loginEv, assetsEv) {

var verify = {
assertion : function (assertion) {
Expand All @@ -11,24 +15,34 @@ define(['message', 'event-login', 'modal', 'event-assets'], function (message, l

var evHandler = {
init : function () {

var email = ($('img', $('.js-handler--login')).length)
? null
: $('.js-handler--login').text().trim()
;

console.log(email);

navigator.id.watch({
onlogin: function(assertion) {
if ($('img', $('.js-handler--login')).length)
verify.assertion(assertion);
else
console.log('has session is logged in');
assetsEv.count(email);
},
onlogout: function() {
console.log('logout');
},
loggedInUser: undefined
loggedInUser: email
});

$('.js-handler--login').on('click', function () {
navigator.id.request();
});

$('.js-handler--create-asset').on('click', assetsEv.create);
$('.js-handler--create-asset').on('submit', assetsEv.create);

assetsEv.loadLatest($('.container--newest'));

}
};
Expand Down
11 changes: 9 additions & 2 deletions public/javascripts/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ define([], function () {
show : function (msgData) {
var close = $('<span/>').addClass('close-handler')
.html('&times;').on('click', message.remove);
var msg = $('<p/>').addClass('message').html(message.create(msgData))

var msg = $('<p></p>').addClass('message');

if (typeof msgData == 'string')
msg.html(msgData).append(close)
.appendTo(msgData.appendTo);
else
msg.html(message.create(msgData))
.append(close).appendTo(msgData.appendTo);

return msg;
return msg.length ? $(msg[0]) : $(msg);
},
create: function (msgData) {
var a = $('<a/>').attr('href', msgData.href)
Expand Down
7 changes: 4 additions & 3 deletions public/javascripts/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ define([], function () {

if (options) {
$modal.css(options);
} else {
$modal.css({width: '700px', 'margin-left':'-350px'});
}

if (data.form) {
Expand Down Expand Up @@ -41,9 +43,8 @@ define([], function () {
$('<div/>').addClass('modal-overlay')
.appendTo('body').fadeIn();
},
remove : function (e) {
e.preventDefault();
$(this).parents('.modal').fadeOut('fast');
remove : function () {
$('.modal').fadeOut('fast');
$('.modal-overlay').fadeOut('slow')
},
createForm : function (path, $modal) {
Expand Down
23 changes: 23 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,26 @@ table td {
padding: 0.5rem;
margin: 0.5rem;
}
.topcoat-notification {
display: inline-block;
text-align: center;
padding: 0.15em 0.5em 0.2em;
margin-left: 6px;
border-radius: 2px;
background-color: #ec514e;
color: #fff;
font: 0.875em "Source Sans", helvetica, arial, sans-serif;
}
.topcoat-icon-button {
margin-left: 5px;
}
.topcoat-list__container {
display: none;
}
.topcoat-icon-button:hover .topcoat-list__container {
display: block;
}
.list--medium {
max-height: 200px;
overflow: scroll;
}
26 changes: 25 additions & 1 deletion public/stylesheets/style.styl
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,28 @@ table th, table td
overflow scroll
box-shadow inset 0 0 20px rgba(0,0,0,.2)
padding .5rem
margin .5rem
margin .5rem

.topcoat-notification
display: inline-block;
text-align: center;
padding: 0.15em 0.5em 0.2em;
margin-left: 6px;
border-radius: 2px;
background-color: #ec514e;
color: #fff;
font: 0.875em "Source Sans", helvetica, arial, sans-serif;

.topcoat-icon-button
margin-left 5px

.topcoat-list__container
display: none

.topcoat-icon-button:hover
.topcoat-list__container
display: block

.list--medium
max-height 200px
overflow scroll
Loading

0 comments on commit 7c37785

Please sign in to comment.