refactor Rxify nonprofits router
This commit is contained in:
@ -1,6 +1,13 @@
|
|||||||
|
var debug = require('debug')('freecc:nonprofits');
|
||||||
|
var observeMethod = require('../utils/rx').observeMethod;
|
||||||
|
var unDasherize = require('../utils').unDasherize;
|
||||||
|
var dasherize = require('../utils').dasherize;
|
||||||
|
|
||||||
module.exports = function(app) {
|
module.exports = function(app) {
|
||||||
var router = app.loopback.Router();
|
var router = app.loopback.Router();
|
||||||
var Nonprofit = app.models.Nonprofit;
|
var Nonprofit = app.models.Nonprofit;
|
||||||
|
var findNonprofits = observeMethod(Nonprofit, 'find');
|
||||||
|
var findOneNonprofit = observeMethod(Nonprofit, 'findOne');
|
||||||
|
|
||||||
router.get('/nonprofits/directory', nonprofitsDirectory);
|
router.get('/nonprofits/directory', nonprofitsDirectory);
|
||||||
router.get('/nonprofits/:nonprofitName', returnIndividualNonprofit);
|
router.get('/nonprofits/:nonprofitName', returnIndividualNonprofit);
|
||||||
@ -8,57 +15,55 @@ module.exports = function(app) {
|
|||||||
app.use(router);
|
app.use(router);
|
||||||
|
|
||||||
function nonprofitsDirectory(req, res, next) {
|
function nonprofitsDirectory(req, res, next) {
|
||||||
Nonprofit.find(
|
findNonprofits({ where: { estimatedHours: { gt: 0 } } }).subscribe(
|
||||||
{ where: { estimatedHours: { gt: 0 } } },
|
function(nonprofits) {
|
||||||
function(err, nonprofits) {
|
|
||||||
if (err) { return next(err); }
|
|
||||||
|
|
||||||
res.render('nonprofits/directory', {
|
res.render('nonprofits/directory', {
|
||||||
title: 'Nonprofits we help',
|
title: 'Nonprofits we help',
|
||||||
nonprofits: nonprofits
|
nonprofits: nonprofits
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
|
next
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function returnIndividualNonprofit(req, res, next) {
|
function returnIndividualNonprofit(req, res, next) {
|
||||||
var dashedName = req.params.nonprofitName;
|
var dashedName = req.params.nonprofitName;
|
||||||
var nonprofitName = dashedName.replace(/\-/g, ' ');
|
var nonprofitName = unDasherize(dashedName);
|
||||||
|
var query = { where: { name: {
|
||||||
|
like: nonprofitName,
|
||||||
|
options: 'i'
|
||||||
|
} } };
|
||||||
|
|
||||||
Nonprofit.find(
|
debug('looking for %s', nonprofitName);
|
||||||
{ where: { name: new RegExp(nonprofitName, 'i') } },
|
debug('query', query);
|
||||||
function(err, nonprofit) {
|
findOneNonprofit(query).subscribe(
|
||||||
if (err) {
|
function(nonprofit) {
|
||||||
return next(err);
|
if (!nonprofit) {
|
||||||
}
|
|
||||||
|
|
||||||
if (nonprofit.length < 1) {
|
|
||||||
req.flash('errors', {
|
req.flash('errors', {
|
||||||
msg: "404: We couldn't find a nonprofit with that name. " +
|
msg: "404: We couldn't find a nonprofit with that name. " +
|
||||||
'Please double check the name.'
|
'Please double check the name.'
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.redirect('/nonprofits');
|
return res.redirect('/nonprofits');
|
||||||
}
|
}
|
||||||
|
|
||||||
nonprofit = nonprofit.pop();
|
var dashedNameFull = dasherize(nonprofit.name);
|
||||||
var dashedNameFull = nonprofit.name.toLowerCase().replace(/\s/g, '-');
|
|
||||||
if (dashedNameFull !== dashedName) {
|
if (dashedNameFull !== dashedName) {
|
||||||
return res.redirect('../nonprofit/' + dashedNameFull);
|
return res.redirect('../nonprofit/' + dashedNameFull);
|
||||||
}
|
}
|
||||||
var buttonActive = false;
|
|
||||||
if (req.user) {
|
|
||||||
if (req.user.uncompletedBonfires.length === 0) {
|
|
||||||
if (req.user.completedCoursewares.length > 63) {
|
|
||||||
var hasShownInterest =
|
|
||||||
nonprofit.interestedCampers.filter(function ( obj ) {
|
|
||||||
return obj.username === req.user.username;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (hasShownInterest.length === 0) {
|
var buttonActive = false;
|
||||||
buttonActive = true;
|
if (
|
||||||
}
|
req.user &&
|
||||||
}
|
req.user.uncompletedBonfires.length === 0 &&
|
||||||
|
req.user.completedCoursewares.length > 63
|
||||||
|
) {
|
||||||
|
var hasShownInterest =
|
||||||
|
nonprofit.interestedCampers.filter(function(user) {
|
||||||
|
return user.username === req.user.username;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (hasShownInterest.length === 0) {
|
||||||
|
buttonActive = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,33 +102,8 @@ module.exports = function(app) {
|
|||||||
buttonActive: buttonActive,
|
buttonActive: buttonActive,
|
||||||
currentStatus: nonprofit.currentStatus
|
currentStatus: nonprofit.currentStatus
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
|
next
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
function interestedInNonprofit(req, res, next) {
|
|
||||||
if (req.user) {
|
|
||||||
Nonprofit.findOne(
|
|
||||||
{ name: new RegExp(req.params.nonprofitName.replace(/-/, ' '), 'i') },
|
|
||||||
function(err, nonprofit) {
|
|
||||||
if (err) { return next(err); }
|
|
||||||
nonprofit.interestedCampers.push({
|
|
||||||
username: req.user.username,
|
|
||||||
picture: req.user.picture,
|
|
||||||
timeOfInterest: Date.now()
|
|
||||||
});
|
|
||||||
nonprofit.save(function(err) {
|
|
||||||
if (err) { return next(err); }
|
|
||||||
req.flash('success', {
|
|
||||||
msg: 'Thanks for expressing interest in this nonprofit project! ' +
|
|
||||||
"We've added you to this project as an interested camper!"
|
|
||||||
});
|
|
||||||
res.redirect('back');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
var Rx = require('rx');
|
var Rx = require('rx');
|
||||||
var debug = require('debug')('freecc:rxUtils');
|
var debug = require('debug')('freecc:rxUtils');
|
||||||
|
var slice = Array.prototype.slice;
|
||||||
|
|
||||||
exports.saveUser = function saveUser(user) {
|
exports.saveUser = function saveUser(user) {
|
||||||
return new Rx.Observable.create(function(observer) {
|
return new Rx.Observable.create(function(observer) {
|
||||||
@ -23,3 +24,7 @@ exports.observableQueryFromModel =
|
|||||||
function observableQueryFromModel(Model, method, query) {
|
function observableQueryFromModel(Model, method, query) {
|
||||||
return Rx.Observable.fromNodeCallback(Model[method], Model)(query);
|
return Rx.Observable.fromNodeCallback(Model[method], Model)(query);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.observeMethod = function observeMethod(Model, method) {
|
||||||
|
return Rx.Observable.fromNodeCallback(Model[method], Model);
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user