From f11aa85bd60b99a2d3d35398d30682900064e682 Mon Sep 17 00:00:00 2001 From: Mariela Date: Fri, 1 Jul 2022 01:39:52 -0600 Subject: [PATCH] update microservicios-areas-comunes agregar schema y metodos de microservicios agregar conexion a bd y api gateway --- api-gateway/src/app.controller.ts | 52 +++++++++++++------ api-gateway/src/app.module.ts | 10 ++++ api-gateway/src/app.service.ts | 45 ++++++++++++++-- servicio-areas-comunes/package-lock.json | 1 + servicio-areas-comunes/package.json | 1 + servicio-areas-comunes/src/app.controller.ts | 11 ---- servicio-areas-comunes/src/app.module.ts | 19 ++++++- .../common_areas/common_areas.controller.ts | 34 ++++++++++++ .../src/common_areas/common_areas.module.ts | 14 +++++ .../src/common_areas/common_areas.service.ts | 37 +++++++++++++ servicio-areas-comunes/src/main.ts | 20 +++++-- .../src/schemas/common_area.schema.ts | 29 +++++++++++ .../src/schemas/tenant.schema.ts | 1 - 13 files changed, 237 insertions(+), 37 deletions(-) delete mode 100644 servicio-areas-comunes/src/app.controller.ts create mode 100644 servicio-areas-comunes/src/common_areas/common_areas.controller.ts create mode 100644 servicio-areas-comunes/src/common_areas/common_areas.module.ts create mode 100644 servicio-areas-comunes/src/common_areas/common_areas.service.ts create mode 100644 servicio-areas-comunes/src/schemas/common_area.schema.ts diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index 6ba1f9af..34b3027a 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -22,7 +22,7 @@ export class AppController { user_type, status, date_entry); } - @Post('user/create') + @Post('user/createUser') createUser( @Body('dni') dni: string, @Body('name') name: string, @@ -39,23 +39,20 @@ export class AppController { user_type, status, date_entry, community_id); } - - - @Get('user/all') + @Get('user/allUsers') allUsers() { return this.appService.allUsers(); } - @Get('user/find/:dni') findUser( @Param('dni') paramUserDNI: string - ){ + ) { return this.appService.findUser(paramUserDNI); } -// #==== API Communities + // #==== API Communities //#API orderService - create order @Post('community/createCommunity') createCommunity( @@ -71,25 +68,50 @@ export class AppController { @Body('houses') houses: [{}], ) { - - - - return this.appService.createCommunity(name, province, canton, + return this.appService.createCommunity(name, province, canton, district, num_houses, phone, quote, status, date_entry, houses); } - @Get('community/allCommunities') allcommunities() { return this.appService.allCommunities(); } - @Get('community/findCommunity/:id') findCommunity( - @Param('dni') paramCommunityId: string - ){ + @Param('id') paramCommunityId: string + ) { return this.appService.findCommunity(paramCommunityId); } + + + // #==== API Common Areas + //#API orderService - create order + @Post('commonArea/createCommonArea') + createCommonArea( + @Body('name') name: string, + @Body('hourMin') hourMin: string, + @Body('hourMax') hourMax: string, + @Body('bookable') bookable: number, + @Body('community_id') community_id: string, + ) { + + return this.appService.createCommonArea(name, hourMin, hourMax, + bookable, community_id); + } + + + @Get('commonArea/allCommonAreas') + allCommonAreas() { + return this.appService.allCommonAreas(); + } + + + @Get('commonArea/findCommonArea/:id') + findCommonArea( + @Param('id') paramCommonAreaId: string + ) { + return this.appService.findCommonArea(paramCommonAreaId); + } } \ No newline at end of file diff --git a/api-gateway/src/app.module.ts b/api-gateway/src/app.module.ts index 3d7740ab..9b0f4167 100644 --- a/api-gateway/src/app.module.ts +++ b/api-gateway/src/app.module.ts @@ -25,6 +25,16 @@ import { AppService } from './app.service'; } } ]), + ClientsModule.register([ + { + name: "SERVICIO_AREAS_COMUNES", + transport: Transport.TCP, + options: { + host: "127.0.0.1", + port: 3003 + } + } + ]), ], controllers: [AppController], providers: [AppService], diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 3e41ba03..538f1ff5 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -8,6 +8,7 @@ export class AppService { constructor( @Inject('SERVICIO_USUARIOS') private readonly clientUserApp: ClientProxy, @Inject('SERVICIO_COMUNIDADES') private readonly clientCommunityApp: ClientProxy, + @Inject('SERVICIO_AREAS_COMUNES') private readonly clientCommonAreaApp: ClientProxy, ) { } // ====================== USERS =============================== @@ -18,7 +19,7 @@ export class AppService { const pattern = { cmd: 'createUser' }; const payload = { dni: dni, name: name, last_name: last_name, email: email, phone: phone, - password: password, user_type: user_type, status: status, date_entry: date_entry, + password: password, user_type: user_type, status: status, date_entry: date_entry, community_id: community_id }; return this.clientUserApp @@ -81,8 +82,6 @@ export class AppService { ); } - - allCommunities() { const pattern = { cmd: 'findAllCommunities' }; const payload = {}; @@ -103,4 +102,44 @@ export class AppService { map((message: string) => ({ message })), ); } + + + + // ====================== COMMON AREAS =============================== + //POST parameter from API + createCommonArea(name: string, hourMin: string, hourMax: string, + bookable: number, community_id: string) { + const pattern = { cmd: 'createCommonArea' }; + const payload = { + name: name, hourMin: hourMin, hourMax: hourMax, bookable: bookable, + community_id: community_id + }; + return this.clientCommonAreaApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + allCommonAreas() { + const pattern = { cmd: 'findAllCommonAreas' }; + const payload = {}; + return this.clientCommonAreaApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + //GET parameter from API + findCommonArea(paramCommonAreaId: string) { + const pattern = { cmd: 'findOneCommonArea' }; + const payload = { id: paramCommonAreaId }; + return this.clientCommonAreaApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + } \ No newline at end of file diff --git a/servicio-areas-comunes/package-lock.json b/servicio-areas-comunes/package-lock.json index 208c9c56..7dd8c0fa 100644 --- a/servicio-areas-comunes/package-lock.json +++ b/servicio-areas-comunes/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-areas-comunes/package.json b/servicio-areas-comunes/package.json index d98babf3..b4301260 100644 --- a/servicio-areas-comunes/package.json +++ b/servicio-areas-comunes/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-areas-comunes/src/app.controller.ts b/servicio-areas-comunes/src/app.controller.ts deleted file mode 100644 index ff8098bb..00000000 --- a/servicio-areas-comunes/src/app.controller.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Controller, Get } from '@nestjs/common'; - -@Controller() -export class AppController { - constructor() {} - - @Get() - getHello(): string { - return "hello world"; - } -} diff --git a/servicio-areas-comunes/src/app.module.ts b/servicio-areas-comunes/src/app.module.ts index 03932cc6..df28f9f9 100644 --- a/servicio-areas-comunes/src/app.module.ts +++ b/servicio-areas-comunes/src/app.module.ts @@ -1,8 +1,23 @@ import { Module } from '@nestjs/common'; -import { AppController } from './app.controller'; +import { CommonAreasModule } from './common_areas/common_areas.module'; +import { MongooseModule } from '@nestjs/mongoose'; +import { ClientsModule, Transport } from "@nestjs/microservices"; + @Module({ - imports: [], + imports: [ + ClientsModule.register([ + { + name: "SERVICIO_AREAS_COMUNES", + transport: Transport.TCP, + options: { + host: "127.0.0.1", + port: 3003 + } + } + ]), + MongooseModule.forRoot(`mongodb+srv://proyecto_4:proyecto_4@proyecto4.yv4fb.mongodb.net/servicio_areas_comunes?retryWrites=true&w=majority`), + CommonAreasModule], controllers: [], providers: [], }) diff --git a/servicio-areas-comunes/src/common_areas/common_areas.controller.ts b/servicio-areas-comunes/src/common_areas/common_areas.controller.ts new file mode 100644 index 00000000..eeccaf0f --- /dev/null +++ b/servicio-areas-comunes/src/common_areas/common_areas.controller.ts @@ -0,0 +1,34 @@ +import { Controller } from '@nestjs/common'; +import { MessagePattern, Payload } from '@nestjs/microservices'; +import { CommonAreaDocument } from 'src/schemas/common_area.schema'; +import { CommonAreasService } from './common_areas.service'; + +@Controller() +export class CommonAreasController { + constructor(private readonly commonAreasService: CommonAreasService) {} + + @MessagePattern({cmd: 'createCommonArea'}) + create(@Payload() commonArea: CommonAreaDocument) { + return this.commonAreasService.create(commonArea); + } + + @MessagePattern({cmd: 'findAllCommonAreas'}) + findAll() { + return this.commonAreasService.findAll(); + } + + @MessagePattern({cmd: 'findOneCommonArea'}) + findOne(@Payload() id: string) { + return this.commonAreasService.findOne(id); + } + + @MessagePattern({cmd: 'updateCommonArea'}) + update(@Payload() commonArea: CommonAreaDocument) { + return this.commonAreasService.update(commonArea.id, commonArea); + } + + @MessagePattern({cmd: 'removeCommonArea'}) + remove(@Payload() id: string) { + return this.commonAreasService.remove(id); + } +} diff --git a/servicio-areas-comunes/src/common_areas/common_areas.module.ts b/servicio-areas-comunes/src/common_areas/common_areas.module.ts new file mode 100644 index 00000000..f0420e37 --- /dev/null +++ b/servicio-areas-comunes/src/common_areas/common_areas.module.ts @@ -0,0 +1,14 @@ +import { Module } from '@nestjs/common'; +import { CommonAreasService } from './common_areas.service'; +import { CommonAreasController } from './common_areas.controller'; +import { MongooseModule } from '@nestjs/mongoose'; + +import { CommonArea, CommonAreaSchema } from '../schemas/common_area.schema'; +@Module({ + imports: [ + MongooseModule.forFeature([{ name: CommonArea.name, schema: CommonAreaSchema }]), + ], + controllers: [CommonAreasController], + providers: [CommonAreasService] +}) +export class CommonAreasModule {} diff --git a/servicio-areas-comunes/src/common_areas/common_areas.service.ts b/servicio-areas-comunes/src/common_areas/common_areas.service.ts new file mode 100644 index 00000000..9e0ec1c4 --- /dev/null +++ b/servicio-areas-comunes/src/common_areas/common_areas.service.ts @@ -0,0 +1,37 @@ +import { Injectable } from '@nestjs/common'; +import { CommonArea, CommonAreaDocument } from 'src/schemas/common_area.schema'; +import { InjectModel } from '@nestjs/mongoose'; +import { Model } from 'mongoose'; + +@Injectable() +export class CommonAreasService { + + constructor( + @InjectModel(CommonArea.name) private readonly commonAreaModel: Model, + ) {} + + async create(commonArea: CommonAreaDocument): Promise { + return this.commonAreaModel.create(commonArea); + } + + async findAll(): Promise { + return this.commonAreaModel + .find() + .setOptions({ sanitizeFilter: true }) + .exec(); + } + + findOne(id: string): Promise { + return this.commonAreaModel.findOne({ _id: id }).exec(); + } + + update(id: string, commonArea: CommonAreaDocument) { + return this.commonAreaModel.findOneAndUpdate({ _id: id }, commonArea, { + new: true, + }); + } + + async remove(id: string) { + return this.commonAreaModel.findByIdAndRemove({ _id: id }).exec(); + } +} diff --git a/servicio-areas-comunes/src/main.ts b/servicio-areas-comunes/src/main.ts index 13cad38c..ece9658e 100644 --- a/servicio-areas-comunes/src/main.ts +++ b/servicio-areas-comunes/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: 3003 + } + }); + app.listen().then(() => logger.log("Microservice Áreas Comunes is listening" )); } -bootstrap(); +bootstrap(); \ No newline at end of file diff --git a/servicio-areas-comunes/src/schemas/common_area.schema.ts b/servicio-areas-comunes/src/schemas/common_area.schema.ts new file mode 100644 index 00000000..23e51916 --- /dev/null +++ b/servicio-areas-comunes/src/schemas/common_area.schema.ts @@ -0,0 +1,29 @@ + +import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; +import { Document } from 'mongoose'; +import { Timestamp } from 'rxjs'; +import { TimerOptions } from 'timers'; + + +export type CommonAreaDocument = CommonArea & Document; + +@Schema({ collection: 'common_areas' }) +export class CommonArea { + + @Prop() + name: string; + + @Prop() + hourMin: string; //hora militar, separado por dos puntos + + @Prop() + hourMax: string; //hora militar, separado por dos puntos + + @Prop() + bookable: number; //saber si es necesario reservarlo o no + + @Prop() + community_id: string; +} + +export const CommonAreaSchema = SchemaFactory.createForClass(CommonArea); \ No newline at end of file diff --git a/servicio-comunidad-viviendas/src/schemas/tenant.schema.ts b/servicio-comunidad-viviendas/src/schemas/tenant.schema.ts index 489c2326..e0d66fa4 100644 --- a/servicio-comunidad-viviendas/src/schemas/tenant.schema.ts +++ b/servicio-comunidad-viviendas/src/schemas/tenant.schema.ts @@ -1,5 +1,4 @@ import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; -import { Document } from 'mongoose';