Skip to content

Commit

Permalink
feat: Added attachment Adapter and repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Nino van Galen committed Jun 26, 2020
1 parent 8fc1b8f commit 782cb00
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 5 deletions.
67 changes: 67 additions & 0 deletions lib/database/adapters/AttachmentAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/

const { Attachment } = require('../../domain/entities');
const LogAdapter = require('./TagAdapter');

/**
* AttachmentAdapter
*/
class AttachmentAdapter {
/**
* Converts the given database object to an entity object.
*
* @param {Object} databaseObject Object to convert.
* @returns {Object} Converted entity object.
*/
static toEntity({ fileName, size, mimeType, originalName, path, encoding, log, createdAt, updatedAt }) {
const entityObject = Object.assign(new Attachment(), {
fileName: fileName,
size: size,
mimeType: mimeType,
originalName: originalName,
path: path,
encoding: encoding,
createdAt: new Date(createdAt).getTime(),
updatedAt: new Date(updatedAt).getTime(),
});

if (log) {
entityObject.log = LogAdapter.toEntity(log);
} else {
entityObject.log = undefined;
}

return entityObject;
}

/**
* Converts the given entity object to a database object.
*
* @param {Object} entityObject Object to convert.
* @returns {Object} Converted database object.
*/
static toDatabase(entityObject) {
return {
size: entityObject.size,
path: entityObject.path,
fileName: entityObject.filename,
originalName: entityObject.originalname,
mimeType: entityObject.mimetype,
encoding: entityObject.encoding,
logId: entityObject.log,
};
}
}

module.exports = AttachmentAdapter;
2 changes: 2 additions & 0 deletions lib/database/adapters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
* or submit itself to any jurisdiction.
*/

const AttachmentAdapter = require('./AttachmentAdapter');
const LogAdapter = require('./LogAdapter');
const LogTagsAdapter = require('./LogTagsAdapter');
const SubsystemAdapter = require('./SubsystemAdapter');
const TagAdapter = require('./TagAdapter');
const UserAdapter = require('./UserAdapter');

module.exports = {
AttachmentAdapter,
LogAdapter,
LogTagsAdapter,
SubsystemAdapter,
Expand Down
66 changes: 61 additions & 5 deletions lib/database/repositories/AttachmentRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,78 @@
* or submit itself to any jurisdiction.
*/

const { AttachmentAdapter } = require('../adapters');
const {
models: {
Attachment,
Log,
},
utilities: {
QueryBuilder,
},
} = require('../');
const Repository = require('./Repository');

/**
* Attachment repository
*/
class AttachmentRepository extends Repository {
class AttachmentRepository {
/**
* Returns the total number of entities.
*
* @returns {Promise<number>} Promise object representing the total number of entities.
*/
async count() {
return AttachmentRepository.count();
}

/**
* Returns all entities.
*
* @param {Object} queryBuilder The QueryBuilder to use.
* @returns {Promise} Promise object representing the full mock data.
*/
async findAll(queryBuilder) {
return Attachment.findAll(queryBuilder.toImplementation()).map(AttachmentAdapter.toEntity);
}

/**
* Returns all entities queried by log id.
*
* @param {number} logId the id to query on.
* @param {Object} queryBuilder The QueryBuilder to use.
* @returns {Promise} Promise object representing the full mock data
*/
async findAllByLogId(logId, queryBuilder = new QueryBuilder()) {
queryBuilder.include({
model: Log,
as: 'log',
required: true,
through: { where: { log_id: logId } },
});
return this.findAll(queryBuilder);
}

/**
* Find one model.
*
* @param {Object} queryBuilder The Querybuilder to use.
* @returns {Object|null} model or null
*/
async findOne(queryBuilder) {
queryBuilder.limit(1);
const result = await Attachment.findOne(queryBuilder.toImplementation());
return result ? AttachmentAdapter.toEntity(result) : null;
}

/**
* Creates a new `AttachmentRepository` instance.
* Insert entity.
*
* @param {Object} entity entity to insert.
* @returns {Promise} Promise object represents the just inserted Attachment.
*/
constructor() {
super(Attachment);
async insert(entity) {
const result = await Attachment.create(AttachmentAdapter.toDatabase(entity));
return AttachmentAdapter.toEntity(result);
}
}

Expand Down

0 comments on commit 782cb00

Please sign in to comment.