6: Get Data from a Server
[examples/angular-tour-of-heroes] / src / app / in-memory-data.service.ts
1 import { Injectable } from '@angular/core';
2 import { InMemoryDbService } from 'angular-in-memory-web-api';
3 import { Hero } from './hero';
4
5 @Injectable({
6   providedIn: 'root',
7 })
8 export class InMemoryDataService implements InMemoryDbService {
9   createDb() {
10     const heroes = [
11       { id: 11, name: 'Dr Nice' },
12       { id: 12, name: 'Narco' },
13       { id: 13, name: 'Bombasto' },
14       { id: 14, name: 'Celeritas' },
15       { id: 15, name: 'Magneta' },
16       { id: 16, name: 'RubberMan' },
17       { id: 17, name: 'Dynama' },
18       { id: 18, name: 'Dr IQ' },
19       { id: 19, name: 'Magma' },
20       { id: 20, name: 'Tornado' }
21     ];
22     return {heroes};
23   }
24
25   // Overrides the genId method to ensure that a hero always has an id.
26   // If the heroes array is empty,
27   // the method below returns the initial number (11).
28   // if the heroes array is not empty, the method below returns the highest
29   // hero id + 1.
30   genId(heroes: Hero[]): number {
31     return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11;
32   }
33 }