import { HttpResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs' ; import { FoodItem } from '../app/shared/models/foodItem.model' ; @ Injectable ( ) export class FoodServiceMock { private internalFoodList : any [ ] = [ ] ; constructor ( ) { const fooditem = new FoodItem ( ) ; fooditem . id = this . getRandomNumber ( 0 , 9999 ) . toString ( ) ; fooditem . created = new Date ( ) ; fooditem . calories = this . getRandomNumber ( 0 , 99999 ) ; fooditem . name = 'FoodItem1' ; this . internalFoodList . push ( fooditem ) ; } getAllFood ( ) { return Observable . create ( ( observer ) => { observer . next ( this . internalFoodList ) ; observer . complete ( ) ; } ) ; } getSingleFood ( id ) { return Observable . create ( ( observer ) => { observer . next ( this . internalFoodList . find ( x => x . id === id ) ) ; observer . complete ( ) ; } ) ; } addFood ( foodItem ) { return Observable . create ( ( observer ) => { this . internalFoodList . push ( foodItem ) ; observer . next ( foodItem ) ; observer . complete ( ) ; } ) ; } updateFood ( id , foodToUpdate ) { return Observable . create ( ( observer ) => { this . internalFoodList . forEach ( ( item ) => { if ( item . id === id ) { item = foodToUpdate ; } } ) ; observer . next ( foodToUpdate ) ; observer . complete ( ) ; } ) ; } deleteFood ( id ) { return Observable . create ( ( observer ) => { const itemToRemove = this . internalFoodList . find ( x => x . id === id ) ; const indexToRemove = this . internalFoodList . indexOf ( itemToRemove ) ; this . internalFoodList . splice ( indexToRemove , 1 ) ; observer . next ( new HttpResponse ( { status : 204 } ) ) ; observer . complete ( ) ; } ) ; } getRandomMeal ( ) { return Observable . create ( ( observer ) => { observer . next ( [ new FoodItem ( ) , new FoodItem ( ) , new FoodItem ( ) ] ) ; observer . complete ( ) ; } ) ; } private getRandomNumber ( min , max ) { return Math . floor ( Math . random ( ) * max ) + min ; } }