refactor modules

This commit is contained in:
sbriat 2023-06-29 10:47:47 +02:00
parent aa06b8c54c
commit 74a855c1b9
3 changed files with 85 additions and 51 deletions

View File

@ -53,7 +53,5 @@ import { HealthModule } from '@modules/health/health.module';
HealthModule, HealthModule,
AdModule, AdModule,
], ],
controllers: [],
providers: [],
}) })
export class AppModule {} export class AppModule {}

View File

@ -1,4 +1,4 @@
import { Module } from '@nestjs/common'; import { Module, Provider } from '@nestjs/common';
import { CreateAdGrpcController } from './interface/grpc-controllers/create-ad.grpc.controller'; import { CreateAdGrpcController } from './interface/grpc-controllers/create-ad.grpc.controller';
import { CqrsModule } from '@nestjs/cqrs'; import { CqrsModule } from '@nestjs/cqrs';
import { import {
@ -25,24 +25,27 @@ import { FindAdByIdQueryHandler } from './core/queries/find-ad-by-id/find-ad-by-
import { PublishMessageWhenAdIsCreatedDomainEventHandler } from './core/event-handlers/publish-message-when-ad-is-created.domain-event-handler'; import { PublishMessageWhenAdIsCreatedDomainEventHandler } from './core/event-handlers/publish-message-when-ad-is-created.domain-event-handler';
import { PublishLogMessageWhenAdIsCreatedDomainEventHandler } from './core/event-handlers/publish-log-message-when-ad-is-created.domain-event-handler'; import { PublishLogMessageWhenAdIsCreatedDomainEventHandler } from './core/event-handlers/publish-log-message-when-ad-is-created.domain-event-handler';
@Module({ const grpcControllers = [CreateAdGrpcController, FindAdByIdGrpcController];
imports: [CqrsModule],
controllers: [CreateAdGrpcController, FindAdByIdGrpcController], const eventHandlers: Provider[] = [
providers: [
CreateAdService,
FindAdByIdQueryHandler,
PublishMessageWhenAdIsCreatedDomainEventHandler, PublishMessageWhenAdIsCreatedDomainEventHandler,
PublishLogMessageWhenAdIsCreatedDomainEventHandler, PublishLogMessageWhenAdIsCreatedDomainEventHandler,
PrismaService, ];
AdMapper,
const commandHandlers: Provider[] = [CreateAdService];
const queryHandlers: Provider[] = [FindAdByIdQueryHandler];
const mappers: Provider[] = [AdMapper];
const repositories: Provider[] = [
{ {
provide: AD_REPOSITORY, provide: AD_REPOSITORY,
useClass: AdRepository, useClass: AdRepository,
}, },
{ ];
provide: PARAMS_PROVIDER,
useClass: DefaultParamsProvider, const messageBrokers: Provider[] = [
},
{ {
provide: MESSAGE_BROKER_PUBLISHER, provide: MESSAGE_BROKER_PUBLISHER,
useClass: MessageBrokerPublisher, useClass: MessageBrokerPublisher,
@ -51,6 +54,15 @@ import { PublishLogMessageWhenAdIsCreatedDomainEventHandler } from './core/event
provide: MESSAGE_PUBLISHER, provide: MESSAGE_PUBLISHER,
useClass: MessagePublisher, useClass: MessagePublisher,
}, },
];
const orms: Provider[] = [PrismaService];
const utilities: Provider[] = [
{
provide: PARAMS_PROVIDER,
useClass: DefaultParamsProvider,
},
{ {
provide: TIMEZONE_FINDER, provide: TIMEZONE_FINDER,
useClass: TimezoneFinder, useClass: TimezoneFinder,
@ -59,6 +71,20 @@ import { PublishLogMessageWhenAdIsCreatedDomainEventHandler } from './core/event
provide: TIME_CONVERTER, provide: TIME_CONVERTER,
useClass: TimeConverter, useClass: TimeConverter,
}, },
];
@Module({
imports: [CqrsModule],
controllers: [...grpcControllers],
providers: [
...eventHandlers,
...commandHandlers,
...queryHandlers,
...mappers,
...repositories,
...messageBrokers,
...orms,
...utilities,
], ],
exports: [ exports: [
PrismaService, PrismaService,

View File

@ -1,4 +1,4 @@
import { Module } from '@nestjs/common'; import { Module, Provider } from '@nestjs/common';
import { HealthHttpController } from './interface/http-controllers/health.http.controller'; import { HealthHttpController } from './interface/http-controllers/health.http.controller';
import { TerminusModule } from '@nestjs/terminus'; import { TerminusModule } from '@nestjs/terminus';
import { MESSAGE_BROKER_PUBLISHER, MESSAGE_PUBLISHER } from 'src/app.constants'; import { MESSAGE_BROKER_PUBLISHER, MESSAGE_PUBLISHER } from 'src/app.constants';
@ -10,15 +10,20 @@ import { AD_REPOSITORY } from './health.di-tokens';
import { HealthGrpcController } from './interface/grpc-controllers/health.grpc.controller'; import { HealthGrpcController } from './interface/grpc-controllers/health.grpc.controller';
import { AdModule } from '@modules/ad/ad.module'; import { AdModule } from '@modules/ad/ad.module';
@Module({ const grpcControllers = [HealthGrpcController];
imports: [TerminusModule, AdModule],
controllers: [HealthGrpcController, HealthHttpController], const httpControllers = [HealthHttpController];
providers: [
RepositoriesHealthIndicatorUseCase, const useCases: Provider[] = [RepositoriesHealthIndicatorUseCase];
const repositories: Provider[] = [
{ {
provide: AD_REPOSITORY, provide: AD_REPOSITORY,
useClass: AdRepository, useClass: AdRepository,
}, },
];
const messageBrokers: Provider[] = [
{ {
provide: MESSAGE_BROKER_PUBLISHER, provide: MESSAGE_BROKER_PUBLISHER,
useClass: MessageBrokerPublisher, useClass: MessageBrokerPublisher,
@ -27,6 +32,11 @@ import { AdModule } from '@modules/ad/ad.module';
provide: MESSAGE_PUBLISHER, provide: MESSAGE_PUBLISHER,
useClass: MessagePublisher, useClass: MessagePublisher,
}, },
], ];
@Module({
imports: [TerminusModule, AdModule],
controllers: [...grpcControllers, ...httpControllers],
providers: [...useCases, ...repositories, ...messageBrokers],
}) })
export class HealthModule {} export class HealthModule {}