Skip to content

Instantly share code, notes, and snippets.

@robphoenix
Last active September 7, 2018 09:05
Show Gist options
  • Save robphoenix/3767cf08772347e02c2434bcf3b84635 to your computer and use it in GitHub Desktop.
Save robphoenix/3767cf08772347e02c2434bcf3b84635 to your computer and use it in GitHub Desktop.
import { InMemoryDbService } from 'angular-in-memory-web-api'
import { Hero } from './hero'
export class InMemoryDataService implements InMemoryDbService {
createDb() {
const heroes = [
{ id: 11, name: 'Mr. Nice' },
{ id: 12, name: 'Narco' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' },
]
return { heroes }
}
// Overrides the genId method to ensure that a hero always has an id.
// If the heroes array is empty,
// the method below returns the initial number (11).
// If the heroes array is not empty, the method below returns the highest
// hero id + 1.
genId(heroes: Hero[]): number {
return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment