mirror of
https://gitlab.com/mobicoop/v3/service/ad.git
synced 2026-01-10 21:02:41 +00:00
Implement the GRPC controller to update ads
This commit is contained in:
@@ -33,12 +33,14 @@ import { DeleteAdGrpcController } from './interface/grpc-controllers/delete-ad.g
|
||||
import { FindAdByIdGrpcController } from './interface/grpc-controllers/find-ad-by-id.grpc.controller';
|
||||
import { FindAdsByIdsGrpcController } from './interface/grpc-controllers/find-ads-by-ids.grpc.controller';
|
||||
import { FindAdsByUserIdGrpcController } from './interface/grpc-controllers/find-ads-by-user-id.grpc.controller';
|
||||
import { UpdateAdGrpcController } from './interface/grpc-controllers/update-ad.grpc.controller';
|
||||
import { MatcherAdCreatedMessageHandler } from './interface/message-handlers/matcher-ad-created.message-handler';
|
||||
import { MatcherAdCreationFailedMessageHandler } from './interface/message-handlers/matcher-ad-creation-failed.message-handler';
|
||||
import { UserDeletedMessageHandler } from './interface/message-handlers/user-deleted.message-handler';
|
||||
|
||||
const grpcControllers = [
|
||||
CreateAdGrpcController,
|
||||
UpdateAdGrpcController,
|
||||
DeleteAdGrpcController,
|
||||
FindAdByIdGrpcController,
|
||||
FindAdsByIdsGrpcController,
|
||||
|
||||
@@ -7,7 +7,7 @@ service AdService {
|
||||
rpc FindAllByIds(AdsById) returns (Ads);
|
||||
rpc FindAllByUserId(UserById) returns (Ads);
|
||||
rpc Create(Ad) returns (AdById);
|
||||
rpc Update(Ad) returns (Ad);
|
||||
rpc Update(Ad) returns (Empty);
|
||||
rpc Delete(AdById) returns (Empty);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { IsUUID } from 'class-validator';
|
||||
import { CreateAdRequestDto } from './create-ad.request.dto';
|
||||
|
||||
export class UpdateAdRequestDto extends CreateAdRequestDto {
|
||||
@IsUUID(4)
|
||||
id: string;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import {
|
||||
NotFoundException,
|
||||
RpcExceptionCode,
|
||||
RpcValidationPipe,
|
||||
} from '@mobicoop/ddd-library';
|
||||
import { UpdateAdCommand } from '@modules/ad/core/application/commands/update-ad/update-ad.command';
|
||||
import { Controller, UsePipes } from '@nestjs/common';
|
||||
import { CommandBus } from '@nestjs/cqrs';
|
||||
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
||||
import { GRPC_SERVICE_NAME } from '@src/app.constants';
|
||||
import { UpdateAdRequestDto } from './dtos/update-ad.request.dto';
|
||||
|
||||
@UsePipes(
|
||||
new RpcValidationPipe({
|
||||
whitelist: false,
|
||||
forbidUnknownValues: false,
|
||||
}),
|
||||
)
|
||||
@Controller()
|
||||
export class UpdateAdGrpcController {
|
||||
constructor(private readonly commandBus: CommandBus) {}
|
||||
|
||||
@GrpcMethod(GRPC_SERVICE_NAME, 'Update')
|
||||
async update(data: UpdateAdRequestDto): Promise<void> {
|
||||
try {
|
||||
const cmdProps = {
|
||||
adId: data.id,
|
||||
...data,
|
||||
};
|
||||
delete (cmdProps as { id?: string }).id;
|
||||
|
||||
await this.commandBus.execute(new UpdateAdCommand(cmdProps));
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundException) {
|
||||
throw new RpcException({
|
||||
code: RpcExceptionCode.NOT_FOUND,
|
||||
message: error.message,
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user