mirror of
https://gitlab.com/mobicoop/v3/service/user.git
synced 2026-03-18 20:55:50 +00:00
user update
This commit is contained in:
121
.old/adapters/primaries/user.controller.ts
Normal file
121
.old/adapters/primaries/user.controller.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { Mapper } from '@automapper/core';
|
||||
import { InjectMapper } from '@automapper/nestjs';
|
||||
import { Controller, UseInterceptors, UsePipes } from '@nestjs/common';
|
||||
import { CommandBus, QueryBus } from '@nestjs/cqrs';
|
||||
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
||||
import { DatabaseException } from '../../../database/exceptions/database.exception';
|
||||
import { CreateUserCommand } from '../../commands/create-user.command';
|
||||
import { DeleteUserCommand } from '../../commands/delete-user.command';
|
||||
import { UpdateUserCommand } from '../../commands/update-user.command';
|
||||
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';
|
||||
import { User } from '../../domain/entities/user';
|
||||
import { FindAllUsersQuery } from '../../queries/find-all-users.query';
|
||||
import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
|
||||
import { UserPresenter } from './user.presenter';
|
||||
import { ICollection } from '../../../database/interfaces/collection.interface';
|
||||
import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe';
|
||||
import { CacheInterceptor, CacheKey } from '@nestjs/cache-manager';
|
||||
|
||||
@UsePipes(
|
||||
new RpcValidationPipe({
|
||||
whitelist: true,
|
||||
forbidUnknownValues: false,
|
||||
}),
|
||||
)
|
||||
@Controller()
|
||||
export class UserController {
|
||||
constructor(
|
||||
private readonly commandBus: CommandBus,
|
||||
private readonly queryBus: QueryBus,
|
||||
@InjectMapper() private readonly mapper: Mapper,
|
||||
) {}
|
||||
|
||||
@GrpcMethod('UsersService', 'FindAll')
|
||||
@UseInterceptors(CacheInterceptor)
|
||||
@CacheKey('UsersServiceFindAll')
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
@GrpcMethod('UsersService', 'FindOneByUuid')
|
||||
@UseInterceptors(CacheInterceptor)
|
||||
@CacheKey('UsersServiceFindOneByUuid')
|
||||
async findOneByUuid(data: FindUserByUuidRequest): Promise<UserPresenter> {
|
||||
try {
|
||||
const user = await this.queryBus.execute(new FindUserByUuidQuery(data));
|
||||
return this.mapper.map(user, User, UserPresenter);
|
||||
} catch (error) {
|
||||
throw new RpcException({
|
||||
code: 5,
|
||||
message: 'User not found',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@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({});
|
||||
}
|
||||
}
|
||||
|
||||
@GrpcMethod('UsersService', 'Delete')
|
||||
async deleteUser(data: FindUserByUuidRequest): Promise<void> {
|
||||
try {
|
||||
await this.commandBus.execute(new DeleteUserCommand(data.uuid));
|
||||
|
||||
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({});
|
||||
}
|
||||
}
|
||||
}
|
||||
18
.old/adapters/primaries/user.presenter.ts
Normal file
18
.old/adapters/primaries/user.presenter.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { AutoMap } from '@automapper/classes';
|
||||
|
||||
export class UserPresenter {
|
||||
@AutoMap()
|
||||
uuid: string;
|
||||
|
||||
@AutoMap()
|
||||
firstName: string;
|
||||
|
||||
@AutoMap()
|
||||
lastName: string;
|
||||
|
||||
@AutoMap()
|
||||
email: string;
|
||||
|
||||
@AutoMap()
|
||||
phone: string;
|
||||
}
|
||||
35
.old/adapters/primaries/user.proto
Normal file
35
.old/adapters/primaries/user.proto
Normal file
@@ -0,0 +1,35 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package user;
|
||||
|
||||
service UsersService {
|
||||
rpc FindOneByUuid(UserByUuid) returns (User);
|
||||
rpc FindAll(UserFilter) returns (Users);
|
||||
rpc Create(User) returns (User);
|
||||
rpc Update(User) returns (User);
|
||||
rpc Delete(UserByUuid) returns (Empty);
|
||||
}
|
||||
|
||||
message UserByUuid {
|
||||
string uuid = 1;
|
||||
}
|
||||
|
||||
message User {
|
||||
string uuid = 1;
|
||||
string firstName = 2;
|
||||
string lastName = 3;
|
||||
string email = 4;
|
||||
string phone = 5;
|
||||
}
|
||||
|
||||
message UserFilter {
|
||||
optional int32 page = 1;
|
||||
optional int32 perPage = 2;
|
||||
}
|
||||
|
||||
message Users {
|
||||
repeated User data = 1;
|
||||
int32 total = 2;
|
||||
}
|
||||
|
||||
message Empty {}
|
||||
16
.old/adapters/secondaries/message-publisher.ts
Normal file
16
.old/adapters/secondaries/message-publisher.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { IPublishMessage } from '../../../../interfaces/message-publisher';
|
||||
import { MESSAGE_BROKER_PUBLISHER } from '../../../../app.constants';
|
||||
import { MessageBrokerPublisher } from '@mobicoop/message-broker-module';
|
||||
|
||||
@Injectable()
|
||||
export class MessagePublisher implements IPublishMessage {
|
||||
constructor(
|
||||
@Inject(MESSAGE_BROKER_PUBLISHER)
|
||||
private readonly messageBrokerPublisher: MessageBrokerPublisher,
|
||||
) {}
|
||||
|
||||
publish = (routingKey: string, message: string): void => {
|
||||
this.messageBrokerPublisher.publish(routingKey, message);
|
||||
};
|
||||
}
|
||||
8
.old/adapters/secondaries/users.repository.ts
Normal file
8
.old/adapters/secondaries/users.repository.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UserRepository } from '../../../database/domain/user-repository';
|
||||
import { User } from '../../domain/entities/user';
|
||||
|
||||
@Injectable()
|
||||
export class UsersRepository extends UserRepository<User> {
|
||||
protected model = 'user';
|
||||
}
|
||||
Reference in New Issue
Block a user