autobaan/src/reservations/service.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import dayjs from '../common/dayjs'
import { Reservation } from './entity'
@Injectable()
export class ReservationsService {
constructor(
@InjectRepository(Reservation)
private reservationsRepository: Repository<Reservation>,
) {}
async getAll() {
return await this.reservationsRepository.find()
}
async getById(id: string) {
return await this.reservationsRepository.findOneBy({ id })
}
async getByDate(date = dayjs()) {
return await this.reservationsRepository
2023-06-27 16:06:19 +02:00
.createQueryBuilder()
.where(`DATE(dateRangeStart) = DATE(:date)`, { date })
.getMany()
}
async getByDateOnWaitingList(date = dayjs()) {
return await this.reservationsRepository
.createQueryBuilder()
.where(`DATE(dateRangeStart) <= DATE(:date)`, { date })
.andWhere(`DATE(dateRangeEnd) >= DATE(:date)`, { date })
.andWhere('waitListed = true')
.getMany()
}
async create(reservation: Reservation) {
return await this.reservationsRepository.save(reservation)
}
async update(reservationId: string, update: Partial<Reservation>) {
return await this.reservationsRepository.update(reservationId, update)
}
async deleteById(id: string) {
return await this.reservationsRepository.delete({ id })
}
}