update microservicios-areas-comunes

agregar schema y metodos de microservicios
agregar conexion a bd y api gateway
This commit is contained in:
Mariela 2022-07-01 01:39:52 -06:00
parent 0526b7ac18
commit f11aa85bd6
13 changed files with 237 additions and 37 deletions

View File

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

View File

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

View File

@ -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<string>(pattern, payload)
.pipe(
map((message: string) => ({ message })),
);
}
allCommonAreas() {
const pattern = { cmd: 'findAllCommonAreas' };
const payload = {};
return this.clientCommonAreaApp
.send<string>(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<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,11 +0,0 @@
import { Controller, Get } from '@nestjs/common';
@Controller()
export class AppController {
constructor() {}
@Get()
getHello(): string {
return "hello world";
}
}

View File

@ -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: [],
})

View File

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

View File

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

View File

@ -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<CommonAreaDocument>,
) {}
async create(commonArea: CommonAreaDocument): Promise<CommonArea> {
return this.commonAreaModel.create(commonArea);
}
async findAll(): Promise<CommonArea[]> {
return this.commonAreaModel
.find()
.setOptions({ sanitizeFilter: true })
.exec();
}
findOne(id: string): Promise<CommonArea> {
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();
}
}

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: 3003
}
});
app.listen().then(() => logger.log("Microservice Áreas Comunes is listening" ));
}
bootstrap();
bootstrap();

View File

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

View File

@ -1,5 +1,4 @@
import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';