diff --git a/app/repository/collection-index-repository.js b/app/repository/collection-index-repository.js index 84eaaf6b..d9e99971 100644 --- a/app/repository/collection-index-repository.js +++ b/app/repository/collection-index-repository.js @@ -1,3 +1,4 @@ +const { BadlyFormattedParameterError, MissingParameterError } = require('../exceptions'); const CollectionIndex = require('../models/collection-index-model'); class CollectionIndexRepository { @@ -5,5 +6,33 @@ class CollectionIndexRepository { this.model = model; } + async retrieveAll(options) { + const collectionIndexes = await this.model.find() + .skip(options.offset) + .limit(options.limit) + .lean() + .exec(); + + return collectionIndexes; + } + + async retrieveById(id) { + try { + if (!id) { + throw new MissingParameterError('id'); + } + + const collectionIndex = await this.model.findOne({ "collection_index.id": id }); + + return collectionIndex; // Note: collectionIndex is null if not found + } catch (err) { + if (err.name === 'CastError') { + throw new BadlyFormattedParameterError; + } else { + throw err; + } + } + } + } -module.exports = CollectionIndexRepository(CollectionIndex); +module.exports = new CollectionIndexRepository(CollectionIndex); diff --git a/app/services/collection-indexes-service.js b/app/services/collection-indexes-service.js index 25bbe8d4..1b85b8a9 100644 --- a/app/services/collection-indexes-service.js +++ b/app/services/collection-indexes-service.js @@ -26,36 +26,12 @@ class CollectionIndexService { async retrieveAll(options) { - const collectionIndexes = await CollectionIndex.find() - .skip(options.offset) - .limit(options.limit) - .lean() - .exec(); - - return collectionIndexes; + return await this.repository.retrieveAll(options); } async retrieveById(id) { - try { - if (!id) { - const error = new Error(this.errors.missingParameter); - error.parameterName = 'id'; - throw error; - } - - const collectionIndex = await CollectionIndex.findOne({ "collection_index.id": id }); - - return collectionIndex; // Note: collectionIndex is null if not found - } catch (err) { - if (err.name === 'CastError') { - const error = new Error(this.errors.badlyFormattedParameter); - error.parameterName = 'id'; - throw error; - } else { - throw err; - } - } + return await this.repository.retrieveById(id); }