From 6915dd9562ef4aed73dc5fb346ce4f52be618cfa Mon Sep 17 00:00:00 2001 From: Mariela Date: Fri, 1 Jul 2022 03:09:48 -0600 Subject: [PATCH] update microservicios-invitados agregar schema y metodos de microservicios agregar conexion a bd y api gateway --- api-gateway/src/app.controller.ts | 27 ++++++++++++ api-gateway/src/app.module.ts | 10 +++++ api-gateway/src/app.service.ts | 41 ++++++++++++++++++ servicio-invitados/package-lock.json | 1 + servicio-invitados/package.json | 1 + servicio-invitados/src/app.controller.spec.ts | 22 ---------- servicio-invitados/src/app.controller.ts | 12 ------ servicio-invitados/src/app.module.ts | 24 ++++++++--- servicio-invitados/src/app.service.ts | 8 ---- .../src/guests/guests.controller.ts | 42 +++++++++++++++++++ .../src/guests/guests.module.ts | 13 ++++++ .../src/guests/guests.service.ts | 42 +++++++++++++++++++ servicio-invitados/src/main.ts | 20 ++++++--- .../src/schemas/guest.schema.ts | 40 ++++++++++++++++++ .../src/users/users.controller.ts | 6 ++- 15 files changed, 255 insertions(+), 54 deletions(-) delete mode 100644 servicio-invitados/src/app.controller.spec.ts delete mode 100644 servicio-invitados/src/app.controller.ts delete mode 100644 servicio-invitados/src/app.service.ts create mode 100644 servicio-invitados/src/guests/guests.controller.ts create mode 100644 servicio-invitados/src/guests/guests.module.ts create mode 100644 servicio-invitados/src/guests/guests.service.ts create mode 100644 servicio-invitados/src/schemas/guest.schema.ts diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index 34b3027a..bf7ef2ad 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -114,4 +114,31 @@ export class AppController { ) { return this.appService.findCommonArea(paramCommonAreaId); } + + + //#API userService - create user + @Post('guest/createGuest') + createAGuest( + @Body('name') name: string, + @Body('last_name') last_name: string, + @Body('dni') dni: string, + @Body('number_plate') number_plate: string, + @Body('phone') phone: number, + @Body('status') status: string, + @Body('date_entry') date_entry: Date, + ) { + return this.appService.createGuest(name, last_name, dni, number_plate, phone, status, date_entry); + } + + @Get('guest/allGuests') + allGuests() { + return this.appService.allGuests(); + } + + @Get('guest/find/:dni') + findGuest( + @Param('dni') paramGuestDNI: string + ) { + return this.appService.findGuest(paramGuestDNI); + } } \ No newline at end of file diff --git a/api-gateway/src/app.module.ts b/api-gateway/src/app.module.ts index 9b0f4167..6fd20d79 100644 --- a/api-gateway/src/app.module.ts +++ b/api-gateway/src/app.module.ts @@ -35,6 +35,16 @@ import { AppService } from './app.service'; } } ]), + ClientsModule.register([ + { + name: "SERVICIO_INVITADOS", + transport: Transport.TCP, + options: { + host: "127.0.0.1", + port: 3004 + } + } + ]), ], controllers: [AppController], providers: [AppService], diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 538f1ff5..a09c9998 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -9,6 +9,7 @@ export class AppService { @Inject('SERVICIO_USUARIOS') private readonly clientUserApp: ClientProxy, @Inject('SERVICIO_COMUNIDADES') private readonly clientCommunityApp: ClientProxy, @Inject('SERVICIO_AREAS_COMUNES') private readonly clientCommonAreaApp: ClientProxy, + @Inject('SERVICIO_INVITADOS') private readonly clientGuestApp: ClientProxy, ) { } // ====================== USERS =============================== @@ -142,4 +143,44 @@ export class AppService { ); } + + // ====================== GUESTS =============================== + + + //POST parameter from API + createGuest(name: string, last_name: string, dni: string, number_plate: string, phone: number + , status: string, date_entry: Date) { + const pattern = { cmd: 'createGuest' }; + const payload = { + name: name, last_name: last_name, dni: dni, number_plate: number_plate, phone: phone, + status: status, date_entry: date_entry + }; + return this.clientGuestApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + allGuests() { + const pattern = { cmd: 'findAllGuests' }; + const payload = {}; + return this.clientGuestApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + //GET parameter from API + findGuest(paramGuestDNI: string) { + const pattern = { cmd: 'findGuestDNI' }; + const payload = { dni: paramGuestDNI }; + return this.clientGuestApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + } \ No newline at end of file diff --git a/servicio-invitados/package-lock.json b/servicio-invitados/package-lock.json index d7de12f4..833d2234 100644 --- a/servicio-invitados/package-lock.json +++ b/servicio-invitados/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@nestjs/common": "^8.0.0", "@nestjs/core": "^8.0.0", + "@nestjs/mapped-types": "*", "@nestjs/microservices": "^8.4.7", "@nestjs/mongoose": "^9.1.1", "@nestjs/platform-express": "^8.0.0", diff --git a/servicio-invitados/package.json b/servicio-invitados/package.json index 21c404d6..13c8c4f2 100644 --- a/servicio-invitados/package.json +++ b/servicio-invitados/package.json @@ -23,6 +23,7 @@ "dependencies": { "@nestjs/common": "^8.0.0", "@nestjs/core": "^8.0.0", + "@nestjs/mapped-types": "*", "@nestjs/microservices": "^8.4.7", "@nestjs/mongoose": "^9.1.1", "@nestjs/platform-express": "^8.0.0", diff --git a/servicio-invitados/src/app.controller.spec.ts b/servicio-invitados/src/app.controller.spec.ts deleted file mode 100644 index d22f3890..00000000 --- a/servicio-invitados/src/app.controller.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; - -describe('AppController', () => { - let appController: AppController; - - beforeEach(async () => { - const app: TestingModule = await Test.createTestingModule({ - controllers: [AppController], - providers: [AppService], - }).compile(); - - appController = app.get(AppController); - }); - - describe('root', () => { - it('should return "Hello World!"', () => { - expect(appController.getHello()).toBe('Hello World!'); - }); - }); -}); diff --git a/servicio-invitados/src/app.controller.ts b/servicio-invitados/src/app.controller.ts deleted file mode 100644 index cce879ee..00000000 --- a/servicio-invitados/src/app.controller.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Controller, Get } from '@nestjs/common'; -import { AppService } from './app.service'; - -@Controller() -export class AppController { - constructor(private readonly appService: AppService) {} - - @Get() - getHello(): string { - return this.appService.getHello(); - } -} diff --git a/servicio-invitados/src/app.module.ts b/servicio-invitados/src/app.module.ts index 86628031..174c7ba4 100644 --- a/servicio-invitados/src/app.module.ts +++ b/servicio-invitados/src/app.module.ts @@ -1,10 +1,24 @@ import { Module } from '@nestjs/common'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; +import { GuestsModule } from './guests/guests.module'; +import { MongooseModule } from '@nestjs/mongoose'; +import { ClientsModule, Transport } from "@nestjs/microservices"; + @Module({ - imports: [], - controllers: [AppController], - providers: [AppService], + imports: [ + ClientsModule.register([ + { + name: "SERVICIO_INVITADOS", + transport: Transport.TCP, + options: { + host: "127.0.0.1", + port: 3004 + } + } + ]), + MongooseModule.forRoot(`mongodb+srv://proyecto_4:proyecto_4@proyecto4.yv4fb.mongodb.net/servicio_invitados?retryWrites=true&w=majority`), + GuestsModule], + controllers: [], + providers: [], }) export class AppModule {} diff --git a/servicio-invitados/src/app.service.ts b/servicio-invitados/src/app.service.ts deleted file mode 100644 index 927d7cca..00000000 --- a/servicio-invitados/src/app.service.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Injectable } from '@nestjs/common'; - -@Injectable() -export class AppService { - getHello(): string { - return 'Hello World!'; - } -} diff --git a/servicio-invitados/src/guests/guests.controller.ts b/servicio-invitados/src/guests/guests.controller.ts new file mode 100644 index 00000000..05f370c0 --- /dev/null +++ b/servicio-invitados/src/guests/guests.controller.ts @@ -0,0 +1,42 @@ +import { Controller } from '@nestjs/common'; +import { MessagePattern, Payload } from '@nestjs/microservices'; +import { GuestsService } from './guests.service'; +import { Guest, GuestDocument } from 'src/schemas/guest.schema'; + +@Controller() +export class GuestsController { + constructor(private readonly guestsService: GuestsService) {} + + @MessagePattern( {cmd:'createGuest'}) + create(@Payload() guest: GuestDocument) { + return this.guestsService.create(guest); + } + + @MessagePattern( {cmd:'findAllGuests'}) + findAll() { + return this.guestsService.findAll(); + } + + @MessagePattern( {cmd:'findOneGuest'}) + findOneById(@Payload() id: string) { + let _id = id['_id']; + return this.guestsService.findOneId(_id); + } + + @MessagePattern( {cmd:'findGuestDNI'}) + findOneByDNI(@Payload() id: string) { + let dni = id['dni']; + return this.guestsService.findOne(dni); + } + + @MessagePattern( {cmd:'updateGuest'}) + update(@Payload() guest: GuestDocument) { + return this.guestsService.update(guest.id, guest); + } + + @MessagePattern( {cmd:'removeGuest'}) + remove(@Payload() id: string) { + let dni = id['dni']; + return this.guestsService.remove(dni); + } +} diff --git a/servicio-invitados/src/guests/guests.module.ts b/servicio-invitados/src/guests/guests.module.ts new file mode 100644 index 00000000..8d64ad14 --- /dev/null +++ b/servicio-invitados/src/guests/guests.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { GuestsService } from './guests.service'; +import { GuestsController } from './guests.controller'; +import { MongooseModule } from '@nestjs/mongoose'; +import { Guest, GuestSchema } from 'src/schemas/guest.schema'; +@Module({ + imports: [ + MongooseModule.forFeature([{ name: Guest.name, schema: GuestSchema }]), + ], + controllers: [GuestsController], + providers: [GuestsService] +}) +export class GuestsModule {} diff --git a/servicio-invitados/src/guests/guests.service.ts b/servicio-invitados/src/guests/guests.service.ts new file mode 100644 index 00000000..db6bc9d5 --- /dev/null +++ b/servicio-invitados/src/guests/guests.service.ts @@ -0,0 +1,42 @@ +import { Injectable } from '@nestjs/common'; +import { Guest, GuestDocument } from 'src/schemas/guest.schema'; +import { Model } from 'mongoose'; +import { InjectModel } from '@nestjs/mongoose'; + +@Injectable() +export class GuestsService { + constructor( + @InjectModel(Guest.name) private readonly guestModel: Model, + ) {} + + async create(guest: GuestDocument): Promise { + return this.guestModel.create(guest); + } + + async findAll(): Promise { + return this.guestModel + .find() + .setOptions({ sanitizeFilter: true }) + .exec(); + } + + + findOneId(id: string): Promise { + return this.guestModel.findOne({ _id: id }).exec(); + } + + + findOne(id: string): Promise { + return this.guestModel.findOne({ dni: id }).exec(); + } + + update(id: string, guest: GuestDocument) { + return this.guestModel.findOneAndUpdate({ _id: id }, guest, { + new: true, + }); + } + + async remove(id: string) { + return this.guestModel.findByIdAndRemove({ _id: id }).exec(); + } +} diff --git a/servicio-invitados/src/main.ts b/servicio-invitados/src/main.ts index 13cad38c..49b5c536 100644 --- a/servicio-invitados/src/main.ts +++ b/servicio-invitados/src/main.ts @@ -1,8 +1,18 @@ -import { NestFactory } from '@nestjs/core'; -import { AppModule } from './app.module'; +import { NestFactory } from "@nestjs/core"; +import { Transport } from "@nestjs/microservices"; +import { AppModule } from "./app.module"; +import { Logger } from "@nestjs/common"; + +const logger = new Logger(); async function bootstrap() { - const app = await NestFactory.create(AppModule); - await app.listen(3000); + const app = await NestFactory.createMicroservice(AppModule, { + transport: Transport.TCP, + options: { + host: "127.0.0.1", + port: 3004 + } + }); + app.listen().then(() => logger.log("Microservice Invitados is listening" )); } -bootstrap(); +bootstrap(); \ No newline at end of file diff --git a/servicio-invitados/src/schemas/guest.schema.ts b/servicio-invitados/src/schemas/guest.schema.ts new file mode 100644 index 00000000..99cff0fe --- /dev/null +++ b/servicio-invitados/src/schemas/guest.schema.ts @@ -0,0 +1,40 @@ +import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; +import { Document } from 'mongoose'; + + +export type GuestDocument = Guest & Document; + +@Schema({ collection: 'guests' }) +export class Guest { + + @Prop() + name: string; + + @Prop() + last_name: string; + + @Prop() + dni: string; + + @Prop() + phone: number; + + @Prop() + number_plate: string; + + @Prop() + status: string; + + @Prop() + date_entry: Date; + + @Prop() + tenant_id: string; + + @Prop() + community_id: string; ///creo que se debe de agregar para facilitar al guarda ver + // ver los invitados de x comunidad +} + + +export const GuestSchema = SchemaFactory.createForClass(Guest); diff --git a/servicio-usuarios/src/users/users.controller.ts b/servicio-usuarios/src/users/users.controller.ts index 5c59d0b6..18559396 100644 --- a/servicio-usuarios/src/users/users.controller.ts +++ b/servicio-usuarios/src/users/users.controller.ts @@ -23,7 +23,8 @@ export class UsersController { } @MessagePattern({ cmd: 'findUserDNI' }) - findOne(@Payload() dni: string) { + findOne(@Payload() id: string) { + let dni = id['dni']; return this.userService.findOneByDNI(dni); } @@ -34,6 +35,7 @@ export class UsersController { @MessagePattern({ cmd: 'removeUser' }) remove(@Payload() id: string) { - return this.userService.remove(id); + let dni = id['dni']; + return this.userService.remove(dni); } }