69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
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<MptModel>,
|
|
) {}
|
|
|
|
async findAll(): Promise<MptModel[]> {
|
|
return this.mptRepository.find({
|
|
where: { delDt: IsNull() },
|
|
relations: ['farm'],
|
|
order: { testDt: 'DESC' },
|
|
});
|
|
}
|
|
|
|
async findByFarmId(farmNo: number): Promise<MptModel[]> {
|
|
return this.mptRepository.find({
|
|
where: { fkFarmNo: farmNo, delDt: IsNull() },
|
|
relations: ['farm'],
|
|
order: { testDt: 'DESC' },
|
|
});
|
|
}
|
|
|
|
async findByCowShortNo(cowShortNo: string): Promise<MptModel[]> {
|
|
return this.mptRepository.find({
|
|
where: { cowShortNo: cowShortNo, delDt: IsNull() },
|
|
relations: ['farm'],
|
|
order: { testDt: 'DESC' },
|
|
});
|
|
}
|
|
|
|
async findOne(id: number): Promise<MptModel> {
|
|
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<MptModel>): Promise<MptModel> {
|
|
const mpt = this.mptRepository.create(data);
|
|
return this.mptRepository.save(mpt);
|
|
}
|
|
|
|
async bulkCreate(data: Partial<MptModel>[]): Promise<MptModel[]> {
|
|
const mpts = this.mptRepository.create(data);
|
|
return this.mptRepository.save(mpts);
|
|
}
|
|
|
|
async update(id: number, data: Partial<MptModel>): Promise<MptModel> {
|
|
await this.findOne(id);
|
|
await this.mptRepository.update(id, data);
|
|
return this.findOne(id);
|
|
}
|
|
|
|
async remove(id: number): Promise<void> {
|
|
const mpt = await this.findOne(id);
|
|
await this.mptRepository.softRemove(mpt);
|
|
}
|
|
}
|