Last active
December 27, 2018 16:48
-
-
Save felipebelluco/0c5de260d59d4053234e2911f0426400 to your computer and use it in GitHub Desktop.
Sample Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from '@angular/core'; | |
import {HttpClient} from "@angular/common/http"; | |
import {forkJoin, Observable} from "rxjs"; | |
import {map} from "rxjs/operators"; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class SampleService { | |
constructor(private http: HttpClient) { | |
} | |
save(sample: Sample): Observable<Sample> { | |
return this.http.post("http://localhost:3000/samples", sample).pipe(map(this.parser)); | |
} | |
saveAll(samples: Sample[]): Observable<Sample[]> { | |
return forkJoin(samples.map(this.save)) | |
} | |
private parser = (value): Sample => new Sample(value.name); | |
} | |
export class Sample { | |
name: string; | |
constructor(name: string) { | |
this.name = name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment