katoikia-app/servicio-reservaciones/src/reservations/reservations.service.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@nestjs/common';
2022-07-25 04:38:48 +00:00
import {
Reservation,
ReservationDocument,
} from '../schemas/reservation.schema';
2022-07-01 20:01:21 +00:00
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
@Injectable()
export class ReservationsService {
2022-07-01 20:01:21 +00:00
constructor(
2022-07-25 04:38:48 +00:00
@InjectModel(Reservation.name)
private readonly reservationModel: Model<ReservationDocument>,
2022-07-01 20:01:21 +00:00
) {}
create(reservation: ReservationDocument) {
console.log(reservation);
return this.reservationModel.create(reservation);
}
2022-07-25 04:38:48 +00:00
async findAll(): Promise<Reservation[]> {
2022-07-01 20:01:21 +00:00
return this.reservationModel
2022-07-25 04:38:48 +00:00
.find()
.setOptions({ sanitizeFilter: true })
2022-07-01 20:01:21 +00:00
.exec();
}
2022-07-01 20:01:21 +00:00
async findOne(id: string): Promise<Reservation> {
return this.reservationModel.findOne({ _id: id }).exec();
}
2022-07-01 20:01:21 +00:00
async findOneByDNI(dni: string): Promise<Reservation> {
return this.reservationModel.findOne({ dni: dni }).exec();
}
2022-07-01 20:01:21 +00:00
async update(id: string, reservation: ReservationDocument) {
return this.reservationModel.findOneAndUpdate({ _id: id }, reservation, {
new: true,
});
}
2022-07-01 20:01:21 +00:00
async remove(id: string) {
return this.reservationModel.findByIdAndRemove({ _id: id }).exec();
}
}