Skip to content

Commit

Permalink
Renamed some functions to match base service and base repository.
Browse files Browse the repository at this point in the history
Cleaned up some formatting.
Moved pagination back to repository service (and used base service pagination function).
Modified error handling to match base service and base repository.
  • Loading branch information
Jack Sheriff committed Dec 20, 2023
1 parent 3f2d7dc commit b937d43
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ rules:
no-restricted-properties: error
no-restricted-syntax: error
no-return-assign: 'off'
no-return-await: error
no-return-await: 'off'
no-script-url: error
no-self-assign:
- error
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/references-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ exports.update = async function(req, res) {
const referenceData = req.body;

// Create the reference
try {
try {
const reference = await referencesService.update(referenceData);
if (!reference) {
return res.status(404).send('Reference not found.');
} else {
}
else {
logger.debug('Success: Updated reference with source_name ' + reference.source_name);
return res.status(200).send(reference);
}
Expand Down
1 change: 0 additions & 1 deletion app/repository/_base.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ class BaseRepository extends AbstractRepository {

// eslint-disable-next-line class-methods-use-this
async save(data) {

try {
const document = new this.model(data);
return await document.save();
Expand Down
69 changes: 21 additions & 48 deletions app/repository/references-repository.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

const Reference = require('../models/reference-model');
const { MissingParameterError, BadlyFormattedParameterError, DuplicateIdError } = require('../exceptions');

class ReferenceRepository {
const { BadlyFormattedParameterError, DuplicateIdError, DatabaseError } = require('../exceptions');

class ReferencesRepository {
constructor(model) {
this.model = model;
}
Expand Down Expand Up @@ -49,29 +48,10 @@ class ReferenceRepository {
aggregation.push(facet);

// Retrieve the documents
const results = await this.model.aggregate(aggregation);

if (options.includePagination) {
let derivedTotalCount = 0;
if (results[0].totalCount.length > 0) {
derivedTotalCount = results[0].totalCount[0].totalCount;
}
const returnValue = {
pagination: {
total: derivedTotalCount,
offset: options.offset,
limit: options.limit
},
data: results[0].documents
};
return returnValue;
}
else {
return results[0].documents;
}
return await this.model.aggregate(aggregation);
}

async create(data) {
async save(data) {
// Create the document
const reference = new this.model(data);

Expand All @@ -83,20 +63,17 @@ class ReferenceRepository {
catch(err) {
if (err.name === 'MongoServerError' && err.code === 11000) {
// 11000 = Duplicate index
throw new DuplicateIdError;
} else {
console.log(`name: ${err.name} code: ${err.code}`);
throw err;
throw new DuplicateIdError({
details: `Reference with source_name '${ data.source_name }' already exists.`
});
}
else {
throw new DatabaseError(err);
}

Check warning on line 72 in app/repository/references-repository.js

View check run for this annotation

Codecov / codecov/patch

app/repository/references-repository.js#L71-L72

Added lines #L71 - L72 were not covered by tests
}
}

async update(data) {
// Note: source_name is used as the key and cannot be updated
if (!data.source_name) {
throw new MissingParameterError;
}

async updateAndSave(data) {
try {
const document = await this.model.findOne({ 'source_name': data.source_name });
if (!document) {
Expand All @@ -112,26 +89,22 @@ class ReferenceRepository {
}
catch(err) {
if (err.name === 'CastError') {
throw new BadlyFormattedParameterError;
throw new BadlyFormattedParameterError({ parameterName: 'source_name' });
}
else if (err.name === 'MongoServerError' && err.code === 11000) {
// 11000 = Duplicate index
throw new DuplicateIdError;
} else {
throw err;
else {
throw new DatabaseError(err);
}
}

Check warning on line 97 in app/repository/references-repository.js

View check run for this annotation

Codecov / codecov/patch

app/repository/references-repository.js#L91-L97

Added lines #L91 - L97 were not covered by tests
}

async deleteBySourceName(sourceName) {
if (!sourceName) {
throw new MissingParameterError;
async findOneAndRemove(sourceName) {
try {
return await this.model.findOneAndRemove({ 'source_name': sourceName });
}
catch(err) {
throw new DatabaseError(err);
}

Check warning on line 106 in app/repository/references-repository.js

View check run for this annotation

Codecov / codecov/patch

app/repository/references-repository.js#L105-L106

Added lines #L105 - L106 were not covered by tests

const deletedReference = await this.model.findOneAndRemove({ 'source_name': sourceName });
return deletedReference;
}

}

module.exports = new ReferenceRepository(Reference);
module.exports = new ReferencesRepository(Reference);
2 changes: 1 addition & 1 deletion app/services/_base.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class BaseService extends AbstractService {
try {
results = await this.repository.retrieveAll(options);
} catch (err) {
const databaseError = new DatabaseError(err); // Let the DatabaseError buddle up
const databaseError = new DatabaseError(err); // Let the DatabaseError bubble up

Check warning on line 56 in app/services/_base.service.js

View check run for this annotation

Codecov / codecov/patch

app/services/_base.service.js#L56

Added line #L56 was not covered by tests
if (callback) {
return callback(databaseError);
}
Expand Down
34 changes: 21 additions & 13 deletions app/services/references-service.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
'use strict';

const ReferenceRepository = require('../repository/references-repository');
const baseService = require('./_base.service');
const ReferencesRepository = require('../repository/references-repository');
const { MissingParameterError } = require('../exceptions');

class ReferencesService {

constructor() {
this.repository = ReferenceRepository;
this.repository = ReferencesRepository;
}

async retrieveAll(options) {
const res = await this.repository.retrieveAll(options);
return res;
const results = await this.repository.retrieveAll(options);
const paginatedResults = baseService.paginate(options, results);

return paginatedResults;
}

async create(data) {
const res = await this.repository.create(data);
return res;
return await this.repository.save(data);
}

async update(data) {
const res = await this.repository.update(data);
return res;
// Note: source_name is used as the key and cannot be updated
if (!data.source_name) {
throw new MissingParameterError({ parameterName: 'source_name' });

Check warning on line 26 in app/services/references-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/references-service.js#L26

Added line #L26 was not covered by tests
}

return await this.repository.updateAndSave(data);
}

async deleteBySourceName(sourceName) {
const res = await this.repository.deleteBySourceName(sourceName);
return res;
}
if (!sourceName) {
throw new MissingParameterError({ parameterName: 'source_name' });

Check warning on line 34 in app/services/references-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/references-service.js#L34

Added line #L34 was not covered by tests
}

return await this.repository.findOneAndRemove(sourceName);
}
}

module.exports = new ReferencesService(ReferenceRepository);
module.exports = new ReferencesService(ReferencesRepository);

0 comments on commit b937d43

Please sign in to comment.