send messages on CUD

This commit is contained in:
Gsk54
2022-12-22 14:24:51 +01:00
parent a08468bddc
commit 55383c879d
16 changed files with 624 additions and 17 deletions

View File

@@ -0,0 +1,12 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export abstract class IMessageUser {
exchange: string;
constructor(exchange: string) {
this.exchange = exchange;
}
abstract publish(routingKey: string, message: string): void;
}

View File

@@ -1,6 +1,7 @@
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import { CommandHandler } from '@nestjs/cqrs';
import { UserMessager } from '../../adapters/secondaries/user.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { CreateUserCommand } from '../../commands/create-user.command';
import { CreateUserRequest } from '../dtos/create-user.request';
@@ -10,6 +11,7 @@ import { User } from '../entities/user';
export class CreateUserUseCase {
constructor(
private readonly _repository: UsersRepository,
private readonly _messager: UserMessager,
@InjectMapper() private readonly _mapper: Mapper,
) {}
@@ -20,6 +22,12 @@ export class CreateUserUseCase {
User,
);
return this._repository.create(entity);
const user = await this._repository.create(entity);
if (user) {
this._messager.publish('user.create', JSON.stringify(user));
}
return user;
}
}

View File

@@ -1,12 +1,26 @@
import { CommandHandler } from '@nestjs/cqrs';
import { UserMessager } from '../../adapters/secondaries/user.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { DeleteUserCommand } from '../../commands/delete-user.command';
import { User } from '../entities/user';
@CommandHandler(DeleteUserCommand)
export class DeleteUserUseCase {
constructor(private readonly _repository: UsersRepository) {}
constructor(
private readonly _repository: UsersRepository,
private readonly _messager: UserMessager,
) {}
async execute(command: DeleteUserCommand): Promise<void> {
return this._repository.delete(command.uuid);
async execute(command: DeleteUserCommand): Promise<User> {
const user = await this._repository.delete(command.uuid);
if (user) {
this._messager.publish(
'user.delete',
JSON.stringify({ uuid: user.uuid }),
);
}
return user;
}
}

View File

@@ -1,6 +1,7 @@
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import { CommandHandler } from '@nestjs/cqrs';
import { UserMessager } from '../../adapters/secondaries/user.messager';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { UpdateUserCommand } from '../../commands/update-user.command';
import { UpdateUserRequest } from '../dtos/update-user.request';
@@ -10,6 +11,7 @@ import { User } from '../entities/user';
export class UpdateUserUseCase {
constructor(
private readonly _repository: UsersRepository,
private readonly _messager: UserMessager,
@InjectMapper() private readonly _mapper: Mapper,
) {}
@@ -20,6 +22,17 @@ export class UpdateUserUseCase {
User,
);
return this._repository.update(command.updateUserRequest.uuid, entity);
const user = await this._repository.update(
command.updateUserRequest.uuid,
entity,
);
if (user) {
this._messager.publish(
'user.update',
JSON.stringify(command.updateUserRequest),
);
}
return user;
}
}