Skip to content

Commit 784c17d

Browse files
authored
feat: added GET /api/tags endpoint
2 parents b15508e + 7911fcf commit 784c17d

27 files changed

Lines changed: 522 additions & 28 deletions

lib/database/adapters/LogAdapter.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
const { Log } = require('../../domain/entities');
15+
const TagAdapter = require('./TagAdapter');
1516

1617
/**
1718
* LogAdapter
@@ -23,20 +24,28 @@ class LogAdapter {
2324
* @param {Object} databaseObject Object to convert.
2425
* @returns {Object} Converted entity object.
2526
*/
26-
static toEntity(databaseObject) {
27-
return Object.assign(new Log(), {
28-
entryId: databaseObject.id,
29-
authorID: 'Batman',
30-
title: databaseObject.title,
31-
creationTime: new Date(databaseObject.createdAt).getTime(),
32-
tags: [`Tag${databaseObject.id}`, `Tag${++databaseObject.id}`],
33-
origin: databaseObject.origin,
27+
static toEntity({ id, title, tags, createdAt, origin, subtype }) {
28+
const entityObject = Object.assign(new Log(), {
29+
entryId: id,
30+
authorID: 'John Doe',
31+
title: title,
32+
creationTime: new Date(createdAt).getTime(),
3433
content: [
3534
{ content: 'Batman wrote this...', sender: 'Batman' },
3635
{ content: 'Nightwing wrote this...', sender: 'Nightwing' },
3736
{ content: 'Gordon wrote this...', sender: 'Commissioner Gordon' },
3837
],
38+
origin: origin,
39+
subtype: subtype,
3940
});
41+
42+
if (tags) {
43+
entityObject.tags = tags.map(TagAdapter.toEntity);
44+
} else {
45+
entityObject.tags = [];
46+
}
47+
48+
return entityObject;
4049
}
4150

4251
/**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
14+
/**
15+
* TagAdapter
16+
*/
17+
class TagAdapter {
18+
/**
19+
* Converts the given database object to an entity object.
20+
*
21+
* @param {Object} databaseObject Object to convert.
22+
* @returns {Object} Converted entity object.
23+
*/
24+
static toEntity({ id, text }) {
25+
return { id, text };
26+
}
27+
28+
/**
29+
* Converts the given entity object to a database object.
30+
*
31+
* @param {Object} entityObject Object to convert.
32+
* @returns {Object} Converted database object.
33+
*/
34+
static toDatabase(entityObject) {
35+
return entityObject;
36+
}
37+
}
38+
39+
module.exports = TagAdapter;

lib/database/adapters/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
*/
1313

1414
const LogAdapter = require('./LogAdapter');
15+
const TagAdapter = require('./TagAdapter');
1516

1617
module.exports = {
1718
LogAdapter,
19+
TagAdapter,
1820
};

lib/database/models/log.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = (sequelize, Sequelize) => {
5151
Log.belongsTo(models.Log, { foreignKey: 'parent_log_id', as: 'parentLog' });
5252

5353
Log.belongsToMany(models.Run, { through: 'log_runs' });
54-
Log.belongsToMany(models.Tag, { through: 'log_tags' });
54+
Log.belongsToMany(models.Tag, { as: 'tags', through: 'log_tags' });
5555
};
5656

5757
return Log;

lib/database/repositories/LogRepository.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ class LogRepository {
4040
/**
4141
* Returns a specific entity.
4242
*
43-
* @param {Number} id ID primary key of the entity to find.
43+
* @param {Object} queryBuilder The QueryBuilder to use.
4444
* @returns {Promise|Null} Promise object representing the full mock data
4545
*/
46-
async findOneById(id) {
47-
const result = await Log.findByPk(id);
46+
async findOne(queryBuilder) {
47+
queryBuilder.limit(1);
48+
const result = await Log.findOne(queryBuilder.toImplementation());
4849
return result ? LogAdapter.toEntity(result) : null;
4950
}
5051

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
14+
const { TagAdapter } = require('../adapters');
15+
const { models: { Tag } } = require('../');
16+
17+
/**
18+
* Sequelize implementation of the TagRepository.
19+
*/
20+
class TagRepository {
21+
/**
22+
* Returns the total number of entities.
23+
*
24+
* @returns {Promise<Number>} Promise object representing the total number of entities.
25+
*/
26+
async count() {
27+
return Tag.count();
28+
}
29+
30+
/**
31+
* Returns all entities.
32+
*
33+
* @param {Object} queryBuilder The QueryBuilder to use.
34+
* @returns {Promise} Promise object representing the full mock data
35+
*/
36+
async findAll(queryBuilder) {
37+
return Tag.findAll(queryBuilder.toImplementation()).map(TagAdapter.toEntity);
38+
}
39+
40+
/**
41+
* Returns a specific entity.
42+
*
43+
* @param {Object} queryBuilder The QueryBuilder to use.
44+
* @returns {Promise|Null} Promise object representing the full mock data
45+
*/
46+
async findOne(queryBuilder) {
47+
queryBuilder.limit(1);
48+
const result = await Tag.findOne(queryBuilder.toImplementation());
49+
return result ? TagAdapter.toEntity(result) : null;
50+
}
51+
52+
/**
53+
* Insert entity.
54+
*
55+
* @param {Object} entity entity to insert.
56+
* @returns {Promise} Promise object represents the just inserted Tag.
57+
*/
58+
async insert(entity) {
59+
const result = await Tag.create(TagAdapter.toDatabase(entity));
60+
return TagAdapter.toEntity(result);
61+
}
62+
}
63+
64+
module.exports = new TagRepository();

lib/database/repositories/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
*/
1313

1414
const LogRepository = require('./LogRepository');
15+
const TagRepository = require('./TagRepository');
1516

1617
module.exports = {
1718
LogRepository,
19+
TagRepository,
1820
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
14+
module.exports = {
15+
up: (queryInterface, _Sequelize) => queryInterface.bulkInsert('tags', [
16+
{
17+
text: 'FOOD',
18+
},
19+
{
20+
text: 'RUN',
21+
},
22+
{
23+
text: 'MAINTENANCE',
24+
},
25+
]),
26+
27+
down: (queryInterface, _Sequelize) => queryInterface.bulkDelete('tags', null, {}),
28+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
14+
module.exports = {
15+
up: (queryInterface, _Sequelize) => queryInterface.bulkInsert('log_tags', [
16+
{
17+
log_id: 3,
18+
tag_id: 1,
19+
},
20+
{
21+
log_id: 2,
22+
tag_id: 2,
23+
},
24+
]),
25+
26+
down: (queryInterface, _Sequelize) => queryInterface.bulkDelete('log_tags', null, {}),
27+
};

lib/database/utilities/QueryBuilder.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ class QueryBuilder {
2222
this.options = {};
2323
}
2424

25+
/**
26+
* The numbers of items to return.
27+
*
28+
* @param {*} expression The include expression.
29+
* @returns {Object} The current QueryBuilder instance.
30+
*/
31+
include(expression) {
32+
if (!this.options.include) {
33+
this.options.include = [];
34+
}
35+
36+
this.options.include.push(expression);
37+
return this;
38+
}
39+
2540
/**
2641
* The numbers of items to return.
2742
*

0 commit comments

Comments
 (0)