diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index a6dc7ab8..dfbd7223 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -170,4 +170,31 @@ export class AppController { ) { return this.appService.findPayment(paramPaymentDNI); } + + // #==== API Payment + //#API userService - create user + @Post('reservation/createReservation') + createReservation( + @Body('start_time') start_time: string, + @Body('finish_time') finish_time: string, + @Body('status') status: string, + @Body('date_entry') date_entry: Date, + @Body('user_id') user_id: string, + @Body('common_area_id') common_area_id: string, + ) { + return this.appService.createReservation(start_time, finish_time, status, + date_entry, user_id, common_area_id); + } + + @Get('reservation/allReservations') + allReservations() { + return this.appService.allReservations(); + } + + @Get('reservation/find/:id') + findReservation( + @Param('id') paramReservation: string + ) { + return this.appService.findReservation(paramReservation); + } } \ No newline at end of file diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 6e09a235..79b66292 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -226,4 +226,44 @@ export class AppService { } + // ====================== RESERVATIONS =============================== + + //POST parameter from API + createReservation(start_time: string, finish_time: string, status: string, + date_entry: Date, user_id: string, common_area_id: string) { + const pattern = { cmd: 'createReservation' }; + const payload = { + start_time: start_time, finish_time: finish_time, status: status, + date_entry: date_entry, user_id: user_id, common_area_id: common_area_id + }; + return this.clientReservationApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + allReservations() { + const pattern = { cmd: 'findAllReservations' }; + const payload = {}; + return this.clientReservationApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + //GET parameter from API + findReservation(paramReservationId: string) { + const pattern = { cmd: 'findOneReservation' }; + const payload = { id: paramReservationId }; + return this.clientReservationApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + + } \ No newline at end of file diff --git a/servicio-reservaciones/src/reservations/reservations.controller.ts b/servicio-reservaciones/src/reservations/reservations.controller.ts index 7e03266c..a608315f 100644 --- a/servicio-reservaciones/src/reservations/reservations.controller.ts +++ b/servicio-reservaciones/src/reservations/reservations.controller.ts @@ -1,35 +1,38 @@ import { Controller } from '@nestjs/common'; import { MessagePattern, Payload } from '@nestjs/microservices'; import { ReservationsService } from './reservations.service'; -import { Reservation, ReservationDocument} from '../schemas/reservation.schema'; - +import { Reservation, ReservationDocument } from '../schemas/reservation.schema'; @Controller() export class ReservationsController { - constructor(private readonly reservationsService: ReservationsService) {} + constructor(private readonly reservationsService: ReservationsService) { } - @MessagePattern({cmd:'createReservation'}) + @MessagePattern({ cmd: 'createReservation' }) create(@Payload() reservation: ReservationDocument) { return this.reservationsService.create(reservation); } - @MessagePattern({cmd: 'findAllReservations'}) + @MessagePattern({ cmd: 'findAllReservations' }) findAll() { + console.log(this.reservationsService.findAll()); + return this.reservationsService.findAll(); } - @MessagePattern({cmd: 'findOneReservation'}) - findOne(@Payload() id: number) { - return this.reservationsService.findOne(id); + @MessagePattern({ cmd: 'findOneReservation' }) + findOne(@Payload() id: string) { + let _id = id['id']; + return this.reservationsService.findOne(_id); } - @MessagePattern({cmd: 'updateReservation'}) + @MessagePattern({ cmd: 'updateReservation' }) update(@Payload() reservation: ReservationDocument) { return this.reservationsService.update(reservation.id, reservation); } - @MessagePattern({cmd: 'removeReservation'}) - remove(@Payload() id: number) { - return this.reservationsService.remove(id); + @MessagePattern({ cmd: 'removeReservation' }) + remove(@Payload() id: string) { + let _id = id['id']; + return this.reservationsService.remove(_id); } } diff --git a/servicio-reservaciones/src/reservations/reservations.module.ts b/servicio-reservaciones/src/reservations/reservations.module.ts index d4e8f41a..3592b785 100644 --- a/servicio-reservaciones/src/reservations/reservations.module.ts +++ b/servicio-reservaciones/src/reservations/reservations.module.ts @@ -1,8 +1,14 @@ import { Module } from '@nestjs/common'; import { ReservationsService } from './reservations.service'; +import { MongooseModule } from '@nestjs/mongoose'; + import { ReservationsController } from './reservations.controller'; +import { Reservation, ReservationSchema} from '../schemas/reservation.schema'; @Module({ + imports: [ + MongooseModule.forFeature([{ name: Reservation.name, schema: ReservationSchema }]), + ], controllers: [ReservationsController], providers: [ReservationsService] }) diff --git a/servicio-reservaciones/src/reservations/reservations.service.ts b/servicio-reservaciones/src/reservations/reservations.service.ts index 271490fa..1c93b63f 100644 --- a/servicio-reservaciones/src/reservations/reservations.service.ts +++ b/servicio-reservaciones/src/reservations/reservations.service.ts @@ -1,25 +1,42 @@ import { Injectable } from '@nestjs/common'; import { Reservation, ReservationDocument} from '../schemas/reservation.schema'; +import { Model } from 'mongoose'; +import { InjectModel } from '@nestjs/mongoose'; @Injectable() export class ReservationsService { - create(Reservation: ReservationDocument) { - return 'This action adds a new reservation'; + constructor( + @InjectModel(Reservation.name) private readonly reservationModel: Model, + ) {} + + create(reservation: ReservationDocument) { + console.log(reservation); + + return this.reservationModel.create(reservation); } - findAll() { - return `This action returns all reservations`; + async findAll(): Promise { + return this.reservationModel + .find() + .setOptions({ sanitizeFilter: true }) + .exec(); } - findOne(id: number) { - return `This action returns a #${id} reservation`; + async findOne(id: string): Promise { + return this.reservationModel.findOne({ _id: id }).exec(); } - update(id: number, Reservation: ReservationDocument) { - return `This action updates a #${id} reservation`; + async findOneByDNI(dni: string): Promise { + return this.reservationModel.findOne({ dni: dni }).exec(); } - remove(id: number) { - return `This action removes a #${id} reservation`; + async update(id: string, reservation: ReservationDocument) { + return this.reservationModel.findOneAndUpdate({ _id: id }, reservation, { + new: true, + }); + } + + async remove(id: string) { + return this.reservationModel.findByIdAndRemove({ _id: id }).exec(); } } diff --git a/servicio-reservaciones/src/schemas/reservation.schema.ts b/servicio-reservaciones/src/schemas/reservation.schema.ts index 19d6d88c..c9b2d918 100644 --- a/servicio-reservaciones/src/schemas/reservation.schema.ts +++ b/servicio-reservaciones/src/schemas/reservation.schema.ts @@ -1,14 +1,31 @@ import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; -import { Document } from 'mongoose'; +import { Document, ObjectId } from 'mongoose'; export type ReservationDocument = Reservation & Document; +@Schema({ collection: 'reservations' }) export class Reservation { + + @Prop() + start_time: string; + @Prop() + finish_time: string; + @Prop() + status: string; + + @Prop() + date_entry: Date; + + @Prop() + common_area_id: string; + + @Prop() + user_id: string }