import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Relais } from 'src/entities/relais.entity';
import { CreateRelaisDto } from './dto/create-relais.dto';
import { UpdateRelaisDto } from './dto/update-relais.dto';

@Injectable()
export class RelaisService {
  constructor(
    @InjectRepository(Relais)
    private readonly relaisRepository: Repository<Relais>,
  ) {}

  create(createRelaisDto: CreateRelaisDto) {
    const relais = this.relaisRepository.create(createRelaisDto);
    return this.relaisRepository.save(relais);
  }

  findAll() {
    return this.relaisRepository.find({ order: { nom: 'ASC' } });
  }

  async findOne(id: string) {
    const relais = await this.relaisRepository.findOne({ where: { id } });
    if (!relais) {
      throw new NotFoundException(`Relais #${id} not found`);
    }
    return relais;
  }

  async update(id: string, updateRelaisDto: UpdateRelaisDto) {
    const relais = await this.findOne(id);
    Object.assign(relais, updateRelaisDto);
    return this.relaisRepository.save(relais);
  }

  async remove(id: string) {
    const relais = await this.findOne(id);
    return this.relaisRepository.remove(relais);
  }
}
