Created
September 21, 2015 16:52
-
-
Save Lazhari/ecf751987656ff9ac4c2 to your computer and use it in GitHub Desktop.
Running CRUD methods in a remote method for a loopback model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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