user/src/modules/users/adapters/primaries/users.controller.ts

117 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-12-14 10:37:13 +00:00
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
2022-12-23 14:14:51 +00:00
import { Controller, UsePipes } from '@nestjs/common';
2022-12-14 10:37:13 +00:00
import { CommandBus, QueryBus } from '@nestjs/cqrs';
2022-12-13 17:00:07 +00:00
import { GrpcMethod, RpcException } from '@nestjs/microservices';
2023-01-24 15:02:20 +00:00
import { DatabaseException } from '../../../database/src/exceptions/database.exception';
2022-12-14 10:37:13 +00:00
import { CreateUserCommand } from '../../commands/create-user.command';
2022-12-14 11:49:43 +00:00
import { DeleteUserCommand } from '../../commands/delete-user.command';
2022-12-14 10:37:13 +00:00
import { UpdateUserCommand } from '../../commands/update-user.command';
2022-12-16 16:31:08 +00:00
import { CreateUserRequest } from '../../domain/dtos/create-user.request';
import { FindAllUsersRequest } from '../../domain/dtos/find-all-users.request';
import { FindUserByUuidRequest } from '../../domain/dtos/find-user-by-uuid.request';
import { UpdateUserRequest } from '../../domain/dtos/update-user.request';
2022-12-13 17:00:07 +00:00
import { User } from '../../domain/entities/user';
2022-12-14 14:08:01 +00:00
import { FindAllUsersQuery } from '../../queries/find-all-users.query';
2022-12-13 17:00:07 +00:00
import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
2022-12-14 10:37:13 +00:00
import { UserPresenter } from './user.presenter';
2022-12-14 14:08:01 +00:00
import { ICollection } from '../../../database/src/interfaces/collection.interface';
2023-01-26 09:38:46 +00:00
import { RpcValidationPipe } from '../../../../utils/rpc.validation-pipe';
2022-12-13 17:00:07 +00:00
2022-12-23 14:49:35 +00:00
@UsePipes(
new RpcValidationPipe({
whitelist: true,
forbidUnknownValues: false,
}),
)
2022-12-13 17:00:07 +00:00
@Controller()
export class UsersController {
2022-12-14 10:37:13 +00:00
constructor(
private readonly _commandBus: CommandBus,
private readonly _queryBus: QueryBus,
@InjectMapper() private readonly _mapper: Mapper,
) {}
2022-12-13 17:00:07 +00:00
2022-12-14 14:08:01 +00:00
@GrpcMethod('UsersService', 'FindAll')
async findAll(data: FindAllUsersRequest): Promise<ICollection<User>> {
const userCollection = await this._queryBus.execute(
new FindAllUsersQuery(data),
);
return Promise.resolve({
data: userCollection.data.map((user: User) =>
this._mapper.map(user, User, UserPresenter),
),
total: userCollection.total,
});
}
2022-12-13 17:00:07 +00:00
@GrpcMethod('UsersService', 'FindOneByUuid')
2022-12-14 10:37:13 +00:00
async findOneByUuid(data: FindUserByUuidRequest): Promise<UserPresenter> {
2022-12-23 14:14:51 +00:00
try {
2023-01-11 10:26:58 +00:00
const user = await this._queryBus.execute(new FindUserByUuidQuery(data));
2022-12-14 10:37:13 +00:00
return this._mapper.map(user, User, UserPresenter);
2022-12-23 14:14:51 +00:00
} catch (error) {
throw new RpcException({
code: 5,
message: 'User not found',
});
2022-12-13 17:00:07 +00:00
}
}
2022-12-14 10:37:13 +00:00
@GrpcMethod('UsersService', 'Create')
async createUser(data: CreateUserRequest): Promise<UserPresenter> {
try {
const user = await this._commandBus.execute(new CreateUserCommand(data));
return this._mapper.map(user, User, UserPresenter);
} catch (e) {
if (e instanceof DatabaseException) {
if (e.message.includes('Already exists')) {
throw new RpcException({
code: 6,
message: 'User already exists',
});
}
}
throw new RpcException({});
}
}
@GrpcMethod('UsersService', 'Update')
async updateUser(data: UpdateUserRequest): Promise<UserPresenter> {
try {
const user = await this._commandBus.execute(new UpdateUserCommand(data));
return this._mapper.map(user, User, UserPresenter);
} catch (e) {
if (e instanceof DatabaseException) {
if (e.message.includes('not found')) {
throw new RpcException({
code: 5,
message: 'User not found',
});
}
}
throw new RpcException({});
}
}
2022-12-14 11:49:43 +00:00
@GrpcMethod('UsersService', 'Delete')
async deleteUser(data: FindUserByUuidRequest): Promise<void> {
try {
2022-12-22 13:24:51 +00:00
await this._commandBus.execute(new DeleteUserCommand(data.uuid));
2022-12-14 11:49:43 +00:00
return Promise.resolve();
} catch (e) {
if (e instanceof DatabaseException) {
if (e.message.includes('not found')) {
throw new RpcException({
code: 5,
message: 'User not found',
});
}
}
throw new RpcException({});
}
}
2022-12-13 17:00:07 +00:00
}