2022-12-14 10:37:13 +00:00
|
|
|
import { Mapper } from '@automapper/core';
|
|
|
|
import { InjectMapper } from '@automapper/nestjs';
|
|
|
|
import {
|
|
|
|
BadRequestException,
|
|
|
|
Body,
|
|
|
|
ConflictException,
|
|
|
|
Controller,
|
|
|
|
NotFoundException,
|
|
|
|
} from '@nestjs/common';
|
|
|
|
import { CommandBus, QueryBus } from '@nestjs/cqrs';
|
2022-12-13 17:00:07 +00:00
|
|
|
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
2022-12-14 10:37:13 +00:00
|
|
|
import { DatabaseException } from 'src/modules/database/src/exceptions/DatabaseException';
|
|
|
|
import { CreateUserCommand } from '../../commands/create-user.command';
|
|
|
|
import { UpdateUserCommand } from '../../commands/update-user.command';
|
|
|
|
import { CreateUserRequest } from '../../domain/dto/create-user.request';
|
|
|
|
import { FindUserByUuidRequest } from '../../domain/dto/find-user-by-uuid.request';
|
|
|
|
import { UpdateUserRequest } from '../../domain/dto/update-user.request';
|
2022-12-13 17:00:07 +00:00
|
|
|
import { User } from '../../domain/entities/user';
|
|
|
|
import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
|
2022-12-14 10:37:13 +00:00
|
|
|
import { UserPresenter } from './user.presenter';
|
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
|
|
|
|
|
|
|
@GrpcMethod('UsersService', 'FindOneByUuid')
|
2022-12-14 10:37:13 +00:00
|
|
|
async findOneByUuid(data: FindUserByUuidRequest): Promise<UserPresenter> {
|
2022-12-13 17:00:07 +00:00
|
|
|
const user = await this._queryBus.execute(
|
|
|
|
new FindUserByUuidQuery(data.uuid),
|
|
|
|
);
|
|
|
|
if (user) {
|
2022-12-14 10:37:13 +00:00
|
|
|
return this._mapper.map(user, User, UserPresenter);
|
2022-12-13 17:00:07 +00:00
|
|
|
}
|
|
|
|
throw new RpcException({
|
|
|
|
code: 5,
|
|
|
|
message: 'User not found',
|
|
|
|
});
|
|
|
|
}
|
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-13 17:00:07 +00:00
|
|
|
}
|