import { RepositoryBase, IRepositoryBase } from '@tomei/general';
import { RentalModel } from '../../models/rental.entity';

export class RentalRepository
  extends RepositoryBase<RentalModel>
  implements IRepositoryBase<RentalModel>
{
  constructor() {
    super(RentalModel);
  }

  async findByPk(id: string, transaction?: any): Promise<RentalModel | null> {
    try {
      const result = await RentalModel.findByPk(id, {
        transaction: transaction,
      });

      return result;
    } catch (error) {
      throw new Error(`An Error occured when fetching : ${error.message}`);
    }
  }

  async delete(RentalId: string, dbTransaction?: any) {
    try {
      const options = {
        where: {
          RentalId: RentalId,
        },
        transaction: dbTransaction,
      };
      await RentalModel.destroy(options);
    } catch (error) {
      throw new Error(`An Error occured when delete : ${error.message}`);
    }
  }

  async findAndCountAll(options?: any) {
    try {
      let Rentals: any;
      if (options) {
        Rentals = await RentalModel.findAndCountAll(options);
      } else {
        Rentals = await RentalModel.findAndCountAll();
      }
      return Rentals;
    } catch (error) {
      throw new Error(`An Error occured when retriving : ${error.message}`);
    }
  }
}
