import { Injectable } from '@nestjs/common' ; import { InjectRepository } from '@nestjs/typeorm' ; import { Repository } from 'typeorm' ; import { CatDTO } from './cat.dto' ; import { Cat } from './cat.entity' ; @ Injectable ( ) export class CatService { constructor ( @ InjectRepository ( Cat ) private readonly catRepo ) { } async getAll ( ) { return this . catRepo . find ( ) ; } async getOne ( id ) { return this . catRepo . findOneOrFail ( { id } ) ; } async getOneByName ( name ) { return this . catRepo . findOneOrFail ( { name } ) ; } async insertOne ( cat ) { const newCat = this . catRepo . create ( cat ) ; await this . catRepo . save ( newCat ) ; return newCat ; } async updateOne ( cat ) { const { id } = cat ; await this . catRepo . update ( { id } , cat ) ; return this . getOne ( id ) ; } async deleteOne ( id ) { try { await this . catRepo . delete ( { id } ) ; return { deleted : true } ; } catch ( err ) { return { deleted : false , message : err . message } ; } } }