import { BadRequestException , Injectable } from '@nestjs/common' ; import { CatDTO } from './dto/cats.dto' ; import { Cat } from './models/cats' ; @ Injectable ( ) export class CatService { private cats : Cat [ ] = [ new Cat ( 1 , 'Ventus' , 'Russian Blue' , 3 ) , new Cat ( 2 , 'Terra' , 'Siberian' , 6 ) , new Cat ( 3 , 'Aqua' , 'Maine Coon' , 5 ) , ] ; getAll ( ) { return this . cats ; } getById ( id ) { const foundCat = this . cats . find ( ( cat ) => cat . id === id ) ; if ( ! foundCat ) { throw new BadRequestException ( 'No cat found with id ' + id + '.' ) ; } return foundCat ; } addCat ( cat ) { const newCat = new Cat ( this . cats [ this . cats . length - 1 ] . id + 1 , cat . name , cat . breed , cat . age ) ; this . cats . push ( newCat ) ; return newCat ; } deleteCat ( id ) { const index = this . cats . findIndex ( ( cat ) => cat . id === id ) ; if ( index === - 1 ) { return false ; } this . cats = this . cats . filter ( ( cat , catIndex ) => catIndex !== index ) ; return true ; } }