import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, IsNull } from 'typeorm'; import { MptModel } from './entities/mpt.entity'; @Injectable() export class MptService { constructor( @InjectRepository(MptModel) private readonly mptRepository: Repository, ) {} async findAll(): Promise { return this.mptRepository.find({ where: { delDt: IsNull() }, relations: ['farm'], order: { testDt: 'DESC' }, }); } async findByFarmId(farmNo: number): Promise { return this.mptRepository.find({ where: { fkFarmNo: farmNo, delDt: IsNull() }, relations: ['farm'], order: { testDt: 'DESC' }, }); } async findByCowShortNo(cowShortNo: string): Promise { return this.mptRepository.find({ where: { cowShortNo: cowShortNo, delDt: IsNull() }, relations: ['farm'], order: { testDt: 'DESC' }, }); } async findOne(id: number): Promise { const mpt = await this.mptRepository.findOne({ where: { pkMptNo: id, delDt: IsNull() }, relations: ['farm'], }); if (!mpt) { throw new NotFoundException(`MPT #${id} not found`); } return mpt; } async create(data: Partial): Promise { const mpt = this.mptRepository.create(data); return this.mptRepository.save(mpt); } async bulkCreate(data: Partial[]): Promise { const mpts = this.mptRepository.create(data); return this.mptRepository.save(mpts); } async update(id: number, data: Partial): Promise { await this.findOne(id); await this.mptRepository.update(id, data); return this.findOne(id); } async remove(id: number): Promise { const mpt = await this.findOne(id); await this.mptRepository.softRemove(mpt); } }