ticket-booking-be/src/cast/cast.service.ts

42 lines
1.2 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import Cast from './cast.entity';
import { HttpService } from '@nestjs/axios';
import { Utility } from 'src/common/Utility';
@Injectable()
export class CastService {
constructor(private readonly httpService: HttpService) { }
async findAll(): Promise<{ rows: Cast[], count: number }> {
return Cast.findAndCountAll();
}
async findByPk(id: number): Promise<Cast> {
return Cast.findByPk(id);
}
findOne(cast: Cast): Promise<Cast> {
return Cast.findOne({ where: cast as any });
}
filter(cast: Cast): Promise<Cast[]> {
return Cast.findAll({ where: cast as any });
}
async remove(id: number): Promise<number> {
return Cast.destroy({ where: { id: id } });
}
async upsert(cast: Cast, insertIfNotFound: boolean): Promise<Cast | [affectedCount: number]> {
if (cast.id) {
const existingCast = await this.findByPk(cast.id);
if (existingCast) {
return Cast.update(cast, { where: { id: cast.id } });
}
}
if (insertIfNotFound) {
return Cast.create(cast as any);
}
}
}