import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Like } from 'typeorm';
import { Certificate } from './entities/certificate.entity';

@Injectable()
export class CertificatesService {
  constructor(
    @InjectRepository(Certificate)
    private certificatesRepository: Repository<Certificate>,
  ) {}

  async findAll(search?: string): Promise<Certificate[]> {
    if (search) {
      return this.certificatesRepository.find({
        where: {
          certification_name: Like(`%${search}%`),
          status: 1
        },
        order: { certification_name: 'ASC' },
        take: 50,
      });
    }
    return this.certificatesRepository.find({
      where: { status: 1 },
      order: { certification_name: 'ASC' },
      take: 50,
    });
  }

  async findOne(id: number): Promise<Certificate> {
    return this.certificatesRepository.findOne({ where: { id } });
  }
}
