update microservicios-invitados

agregar schema y metodos de microservicios
agregar conexion a bd y api gateway
This commit is contained in:
Mariela 2022-07-01 03:09:48 -06:00
parent f11aa85bd6
commit 6915dd9562
15 changed files with 255 additions and 54 deletions

View File

@ -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);
}
}

View File

@ -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],

View File

@ -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<string>(pattern, payload)
.pipe(
map((message: string) => ({ message })),
);
}
allGuests() {
const pattern = { cmd: 'findAllGuests' };
const payload = {};
return this.clientGuestApp
.send<string>(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<string>(pattern, payload)
.pipe(
map((message: string) => ({ message })),
);
}
}

View File

@ -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",

View File

@ -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",

View File

@ -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>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});

View File

@ -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();
}
}

View File

@ -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 {}

View File

@ -1,8 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

View File

@ -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);
}
}

View File

@ -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 {}

View File

@ -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<GuestDocument>,
) {}
async create(guest: GuestDocument): Promise<Guest> {
return this.guestModel.create(guest);
}
async findAll(): Promise<Guest[]> {
return this.guestModel
.find()
.setOptions({ sanitizeFilter: true })
.exec();
}
findOneId(id: string): Promise<Guest> {
return this.guestModel.findOne({ _id: id }).exec();
}
findOne(id: string): Promise<Guest> {
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();
}
}

View File

@ -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();

View File

@ -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);

View File

@ -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);
}
}