import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Locations } from './entities/locations.entity';
@Injectable()
export class LocationsService {
  [x: string]: any;
  constructor(
    @InjectRepository(Locations)
    private readonly locationsRepo: Repository<Locations>,
  ) {}

  async findAll(): Promise<Locations[]> {
    return this.locationsRepo.find();
  }

  async findOne(id: number): Promise<Locations> {
    const location = await this.locationsRepo.findOne({ where: { id } });
    if (!location) throw new NotFoundException('Location not found');
    return location;
  }

  async create(data: Partial<Locations>): Promise<Locations> {
    const newLocation = this.locationsRepo.create(data);
    return this.locationsRepo.save(newLocation);
  }

  async update(id: number, data: Partial<Locations>): Promise<Locations> {
    const location = await this.findOne(id);
    Object.assign(location, data);
    return this.locationsRepo.save(location);
  }

  async remove(id: number): Promise<{ message: string }> {
    const location = await this.findOne(id);
    await this.locationsRepo.remove(location);
    return { message: 'Location deleted successfully' };
  }
}
