import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, IsNull } from 'typeorm';
import { FarmModel } from './entities/farm.entity';

@Injectable()
export class FarmService {
  constructor(
    @InjectRepository(FarmModel)
    private readonly farmRepository: Repository<FarmModel>,
  ) { }

  // 전체 농장 조회
  async findAll(): Promise<FarmModel[]> {
    return this.farmRepository.find({
      where: { delDt: IsNull() },
      relations: ['user'],
      order: { regDt: 'DESC' },
    });
  }

  // 사용자별 농장 조회
  async findByUserId(userNo: number): Promise<FarmModel[]> {
    return this.farmRepository.find({
      where: { fkUserNo: userNo, delDt: IsNull() },
      relations: ['user'],
      order: { regDt: 'DESC' },
    });
  }
}
