Skip to content

Instantly share code, notes, and snippets.

@Lazhari
Created September 21, 2015 16:52
Show Gist options
  • Save Lazhari/ecf751987656ff9ac4c2 to your computer and use it in GitHub Desktop.
Save Lazhari/ecf751987656ff9ac4c2 to your computer and use it in GitHub Desktop.
Running CRUD methods in a remote method for a loopback model
module.exports = function(Coupon) {
Coupon.status = function(cb) {
var currentDate = new Date();
Coupon.count({expiredDate: {gt : currentDate}}, function(err, nbCoupons) {
return cb(err, nbCoupons);
});
};
Coupon.expirationStatus = function(couponId, cb) {
var currentDate = new Date();
Coupon.findById(couponId, function(err, instance) {
if(err) throw err;
if(instance && instance.expiredDate > currentDate) return cb(null, true);
else return cb(null, false);
});
};
Coupon.remoteMethod(
'status',
{
http:{path: '/status', verb:'get'},
returns: {arg : 'NumberCouponNotExpired', type: 'string'}
}
);
Coupon.remoteMethod(
'expirationStatus',
{
http: {path: '/getExpirationStatus', verb: 'get'},
accepts: {arg: 'id', type: 'any', http: {source: 'query'}},
returns : {arg: 'status', type: 'string'}
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment