Skip to content

Instantly share code, notes, and snippets.

@felipebelluco
Last active December 27, 2018 16:48
Show Gist options
  • Save felipebelluco/0c5de260d59d4053234e2911f0426400 to your computer and use it in GitHub Desktop.
Save felipebelluco/0c5de260d59d4053234e2911f0426400 to your computer and use it in GitHub Desktop.
Sample Service
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