Skip to content

Commit

Permalink
moving function calls to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
vsun757 committed Feb 26, 2024
1 parent fe8027d commit dee3550
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
31 changes: 30 additions & 1 deletion app/repository/collection-index-repository.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
const { BadlyFormattedParameterError, MissingParameterError } = require('../exceptions');
const CollectionIndex = require('../models/collection-index-model');
class CollectionIndexRepository {

constructor(model) {
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);
28 changes: 2 additions & 26 deletions app/services/collection-indexes-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down

0 comments on commit dee3550

Please sign in to comment.