From 770f7925a42096577995becbe07e83e1ddbf7234 Mon Sep 17 00:00:00 2001 From: Fabricio Leonardo Sodano Pascazi Date: Fri, 4 Sep 2020 11:18:04 +1000 Subject: [PATCH] =?UTF-8?q?Reorganiza=20m=C3=B3dulos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/associations.js | 6 ------ src/config/di.js | 3 --- .../abstractController.test.js | 0 src/module/area/controller/areaController.js | 2 +- src/module/area/mapper/areaMapper.js | 15 ++++++++++++++- .../{repository/sqlite => model}/areaModel.js | 0 src/module/area/module.js | 2 +- src/module/area/repository/sqlite/areaMapper.js | 11 ----------- .../area/repository/sqlite/areaRepository.js | 2 +- .../__tests__}/clubMapper.test.js | 2 +- src/module/club/mapper/clubMapper.js | 9 +++++++++ .../__tests__}/clubModel.test.js | 10 +++++----- .../{repository/sqlite => model}/clubModel.js | 9 +++++++++ src/module/club/module.js | 2 +- .../clubRepository.test.js | 4 ++-- src/module/club/repository/sqlite/clubMapper.js | 11 ----------- .../club/repository/sqlite/clubRepository.js | 6 +++--- 17 files changed, 47 insertions(+), 47 deletions(-) delete mode 100644 src/config/associations.js rename src/module/{__test__ => __tests__}/abstractController.test.js (100%) rename src/module/area/{repository/sqlite => model}/areaModel.js (100%) delete mode 100644 src/module/area/repository/sqlite/areaMapper.js rename src/module/club/{repository/sqlite/__test__ => mapper/__tests__}/clubMapper.test.js (82%) rename src/module/club/{repository/sqlite/__test__ => model/__tests__}/clubModel.test.js (55%) rename src/module/club/{repository/sqlite => model}/clubModel.js (89%) rename src/module/club/repository/sqlite/{__test__ => __tests__}/clubRepository.test.js (95%) delete mode 100644 src/module/club/repository/sqlite/clubMapper.js diff --git a/src/config/associations.js b/src/config/associations.js deleted file mode 100644 index 19c602e..0000000 --- a/src/config/associations.js +++ /dev/null @@ -1,6 +0,0 @@ -/** - * @param {import('sequelize').Sequelize} sequelizeInstance - */ -module.exports = async function setupAssociations(ClubModel, AreaModel) { - ClubModel.belongsTo(AreaModel, { foreignKey: 'area_id' }); -}; diff --git a/src/config/di.js b/src/config/di.js index 49e115f..5f1ae7b 100644 --- a/src/config/di.js +++ b/src/config/di.js @@ -9,8 +9,6 @@ const SequelizeStore = require('connect-session-sequelize')(session.Store); const { ClubController, ClubService, ClubRepository, ClubModel } = require('../module/club/module'); const { AreaController, AreaService, AreaRepository, AreaModel } = require('../module/area/module'); -const setupSequelizeModelAssociations = require('./associations'); - function configureMainSequelizeDatabase() { const sequelize = new Sequelize({ dialect: 'sqlite', @@ -118,6 +116,5 @@ module.exports = function configureDI() { addCommonDefinitions(container); addAreaModuleDefinitions(container); addClubModuleDefinitions(container); - setupSequelizeModelAssociations(container.get('ClubModel'), container.get('AreaModel')); return container; }; diff --git a/src/module/__test__/abstractController.test.js b/src/module/__tests__/abstractController.test.js similarity index 100% rename from src/module/__test__/abstractController.test.js rename to src/module/__tests__/abstractController.test.js diff --git a/src/module/area/controller/areaController.js b/src/module/area/controller/areaController.js index 9a79e45..2e32b31 100644 --- a/src/module/area/controller/areaController.js +++ b/src/module/area/controller/areaController.js @@ -1,4 +1,4 @@ -const fromDataToEntity = require('../mapper/areaMapper'); +const { fromDataToEntity } = require('../mapper/areaMapper'); const AreaIdNotDefinedError = require('./error/areaIdNotDefinedError'); const AbstractController = require('../../abstractController'); diff --git a/src/module/area/mapper/areaMapper.js b/src/module/area/mapper/areaMapper.js index abd6558..a13c4dc 100644 --- a/src/module/area/mapper/areaMapper.js +++ b/src/module/area/mapper/areaMapper.js @@ -1,5 +1,18 @@ const Area = require('../entity/area'); -module.exports = function fromDataToEntity({ id, name }) { +/** + * @param {import('./areaModel')} model + * @returns {import('../../entity/area')} + */ +function fromModelToEntity(model) { + return new Area(model.toJSON()); +} + +function fromDataToEntity({ id, name }) { return new Area({ id, name }); +} + +module.exports = { + fromModelToEntity, + fromDataToEntity, }; diff --git a/src/module/area/repository/sqlite/areaModel.js b/src/module/area/model/areaModel.js similarity index 100% rename from src/module/area/repository/sqlite/areaModel.js rename to src/module/area/model/areaModel.js diff --git a/src/module/area/module.js b/src/module/area/module.js index 7dd49ad..cf3d2af 100644 --- a/src/module/area/module.js +++ b/src/module/area/module.js @@ -1,7 +1,7 @@ const AreaController = require('./controller/areaController'); const AreaRepository = require('./repository/sqlite/areaRepository'); const AreaService = require('./service/areaService'); -const AreaModel = require('./repository/sqlite/areaModel'); +const AreaModel = require('./model/areaModel'); /** * @param {import('express').Application} app diff --git a/src/module/area/repository/sqlite/areaMapper.js b/src/module/area/repository/sqlite/areaMapper.js deleted file mode 100644 index cb9f176..0000000 --- a/src/module/area/repository/sqlite/areaMapper.js +++ /dev/null @@ -1,11 +0,0 @@ -const Area = require('../../entity/area'); - -module.exports = { - /** - * @param {import('./areaModel')} model - * @returns {import('../../entity/area')} - */ - fromModelToEntity(model) { - return new Area(model.toJSON()); - }, -}; diff --git a/src/module/area/repository/sqlite/areaRepository.js b/src/module/area/repository/sqlite/areaRepository.js index 612cb33..41b5c11 100644 --- a/src/module/area/repository/sqlite/areaRepository.js +++ b/src/module/area/repository/sqlite/areaRepository.js @@ -1,4 +1,4 @@ -const { fromModelToEntity } = require('./areaMapper'); +const { fromModelToEntity } = require('../../mapper/areaMapper'); const AbstractAreaRepository = require('../abstractAreaRepository'); const AreaNotFoundError = require('../error/areaNotFoundError'); const AreaIdNotDefinedError = require('../error/areaIdNotDefinedError'); diff --git a/src/module/club/repository/sqlite/__test__/clubMapper.test.js b/src/module/club/mapper/__tests__/clubMapper.test.js similarity index 82% rename from src/module/club/repository/sqlite/__test__/clubMapper.test.js rename to src/module/club/mapper/__tests__/clubMapper.test.js index 632223c..ecc7de6 100644 --- a/src/module/club/repository/sqlite/__test__/clubMapper.test.js +++ b/src/module/club/mapper/__tests__/clubMapper.test.js @@ -1,5 +1,5 @@ const { fromModelToEntity } = require('../clubMapper'); -const ClubEntity = require('../../../entity/club'); +const ClubEntity = require('../../entity/club'); test('Convierte un modelo a una entidad del dominio', () => { expect( diff --git a/src/module/club/mapper/clubMapper.js b/src/module/club/mapper/clubMapper.js index 3a0d4f9..2cb6796 100644 --- a/src/module/club/mapper/clubMapper.js +++ b/src/module/club/mapper/clubMapper.js @@ -44,6 +44,15 @@ function fromDataToEntity({ }); } +/** + * @param {import('./clubModel')} model + * @returns {import('../../entity/club')} + */ +function fromModelToEntity(model) { + return new Club(model.toJSON()); +} + module.exports = { fromDataToEntity, + fromModelToEntity, }; diff --git a/src/module/club/repository/sqlite/__test__/clubModel.test.js b/src/module/club/model/__tests__/clubModel.test.js similarity index 55% rename from src/module/club/repository/sqlite/__test__/clubModel.test.js rename to src/module/club/model/__tests__/clubModel.test.js index b454221..5f971ec 100644 --- a/src/module/club/repository/sqlite/__test__/clubModel.test.js +++ b/src/module/club/model/__tests__/clubModel.test.js @@ -1,13 +1,13 @@ const Sequelize = require('sequelize'); const ClubModel = require('../clubModel'); +const AreaModel = require('../../../area/model/areaModel'); const sequelizeInstance = new Sequelize('sqlite::memory'); -test('Después de hacerle un setup a Club Model y sincronizar el modelo, la tabla Clubs existe ', async () => { - const TEST_ID = 1; +test('Después de hacerle un setup a Club Model y sincronizar el modelo, la tabla Clubs existe', async () => { ClubModel.setup(sequelizeInstance); + AreaModel.setup(sequelizeInstance); + ClubModel.setupAssociations(AreaModel); await ClubModel.sync({ force: true }); - await ClubModel.create({ id: 1 }); - const club = await ClubModel.findOne({ where: { id: TEST_ID } }); - expect(club.id).toEqual(TEST_ID); + expect(await ClubModel.findAll()).toEqual([]); }); diff --git a/src/module/club/repository/sqlite/clubModel.js b/src/module/club/model/clubModel.js similarity index 89% rename from src/module/club/repository/sqlite/clubModel.js rename to src/module/club/model/clubModel.js index 252ff98..cd82a3b 100644 --- a/src/module/club/repository/sqlite/clubModel.js +++ b/src/module/club/model/clubModel.js @@ -63,6 +63,15 @@ module.exports = class ClubModel extends Model { timestamps: false, } ); + return ClubModel; } + + /** + * + * @param {import('../../area/model/areaModel')} AreaModel + */ + static setupAssociations(AreaModel) { + ClubModel.belongsTo(AreaModel, { foreignKey: 'area_id' }); + } }; diff --git a/src/module/club/module.js b/src/module/club/module.js index e694cef..34c9f19 100644 --- a/src/module/club/module.js +++ b/src/module/club/module.js @@ -1,7 +1,7 @@ const ClubController = require('./controller/clubController'); const ClubRepository = require('./repository/sqlite/clubRepository'); const ClubService = require('./service/clubService'); -const ClubModel = require('./repository/sqlite/clubModel'); +const ClubModel = require('./model/clubModel'); /** * @param {import('express').Application} app diff --git a/src/module/club/repository/sqlite/__test__/clubRepository.test.js b/src/module/club/repository/sqlite/__tests__/clubRepository.test.js similarity index 95% rename from src/module/club/repository/sqlite/__test__/clubRepository.test.js rename to src/module/club/repository/sqlite/__tests__/clubRepository.test.js index 00865c7..0ad8a26 100644 --- a/src/module/club/repository/sqlite/__test__/clubRepository.test.js +++ b/src/module/club/repository/sqlite/__tests__/clubRepository.test.js @@ -1,7 +1,7 @@ const { Sequelize } = require('sequelize'); const ClubRepository = require('../clubRepository'); -const ClubModel = require('../clubModel'); -const AreaModel = require('../../../../area/repository/sqlite/areaModel'); +const ClubModel = require('../../../model/clubModel'); +const AreaModel = require('../../../../area/model/areaModel'); const ClubEntity = require('../../../entity/club'); const ClubNotFoundError = require('../../error/clubNotFoundError'); const ClubIdNotDefinedError = require('../../error/clubIdNotDefinedError'); diff --git a/src/module/club/repository/sqlite/clubMapper.js b/src/module/club/repository/sqlite/clubMapper.js deleted file mode 100644 index 8c41528..0000000 --- a/src/module/club/repository/sqlite/clubMapper.js +++ /dev/null @@ -1,11 +0,0 @@ -const Club = require('../../entity/club'); - -module.exports = { - /** - * @param {import('./clubModel')} model - * @returns {import('../../entity/club')} - */ - fromModelToEntity(model) { - return new Club(model.toJSON()); - }, -}; diff --git a/src/module/club/repository/sqlite/clubRepository.js b/src/module/club/repository/sqlite/clubRepository.js index 5f20da9..55eb66d 100644 --- a/src/module/club/repository/sqlite/clubRepository.js +++ b/src/module/club/repository/sqlite/clubRepository.js @@ -1,12 +1,12 @@ -const { fromModelToEntity } = require('./clubMapper'); +const { fromModelToEntity } = require('../../mapper/clubMapper'); const AbstractClubRepository = require('../abstractClubRepository'); const ClubNotFoundError = require('../error/clubNotFoundError'); const ClubIdNotDefinedError = require('../error/clubIdNotDefinedError'); module.exports = class ClubRepository extends AbstractClubRepository { /** - * @param {typeof import('./clubModel')} clubModel - * * @param {typeof import('../../../area/repository/sqlite/areaModel')} areaModel + * @param {typeof import('../../model/clubModel')} clubModel + * * @param {typeof import('../../../area/model/areaModel')} areaModel */ constructor(clubModel, areaModel) { super();