From 6f97c905d6eaf69e78868fe68e495bb33f961dd4 Mon Sep 17 00:00:00 2001 From: Manuel Wiedenmann Date: Sat, 13 Apr 2019 13:05:47 +0200 Subject: [PATCH] Add count property and DRY --- lib/contracts/contribution.js | 13 ++++--------- lib/contracts/contributor.js | 13 ++++--------- lib/contracts/proposal.js | 13 ++++--------- lib/contracts/record.js | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 27 deletions(-) create mode 100644 lib/contracts/record.js diff --git a/lib/contracts/contribution.js b/lib/contracts/contribution.js index 9033d0c..373b983 100644 --- a/lib/contracts/contribution.js +++ b/lib/contracts/contribution.js @@ -1,14 +1,9 @@ -const Base = require('./base'); +const Record = require('./record'); const ContributionSerializer = require('../serializers/contribution'); -const paged = require('../utils/pagination'); -class Contribution extends Base { - all(options = {}) { - return this.functions.contributionsCount() - .then((count) => { - let records = paged(count, options).map((id) => this.getById(id)); - return Promise.all(records); - }); +class Contribution extends Record { + get count () { + return this.functions.contributionsCount(); } getById(id) { diff --git a/lib/contracts/contributor.js b/lib/contracts/contributor.js index 75f2b2e..8afb820 100644 --- a/lib/contracts/contributor.js +++ b/lib/contracts/contributor.js @@ -1,14 +1,9 @@ -const Base = require('./base'); +const Record = require('./record'); const ContributorSerializer = require('../serializers/contributor'); -const paged = require('../utils/pagination'); -class Contributor extends Base { - all(options = {}) { - return this.functions.contributorsCount() - .then((count) => { - let records = paged(count, options).map((id) => this.getById(id)); - return Promise.all(records); - }); +class Contributor extends Record { + get count () { + return this.functions.contributorsCount(); } getById(id) { diff --git a/lib/contracts/proposal.js b/lib/contracts/proposal.js index 6252d84..71bbacc 100644 --- a/lib/contracts/proposal.js +++ b/lib/contracts/proposal.js @@ -1,14 +1,9 @@ -const Base = require('./base'); +const Record = require('./record'); const ContributionSerializer = require('../serializers/contribution'); -const paged = require('../utils/pagination'); -class Proposal extends Base { - all(options = {}) { - return this.functions.proposalsCount() - .then((count) => { - let records = paged(count, options).map((id) => this.getById(id)); - return Promise.all(records); - }); +class Proposal extends Record { + get count () { + return this.functions.proposalsCount(); } getById(id) { diff --git a/lib/contracts/record.js b/lib/contracts/record.js new file mode 100644 index 0000000..a5d8e64 --- /dev/null +++ b/lib/contracts/record.js @@ -0,0 +1,14 @@ +const Base = require('./base'); +const paged = require('../utils/pagination'); + +class Record extends Base { + all(options = {}) { + return this.count + .then((count) => { + let records = paged(count, options).map((id) => this.getById(id)); + return Promise.all(records); + }); + } +} + +module.exports = Record