-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.service.ts
37 lines (31 loc) · 1.01 KB
/
post.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
import { SHUFFLE } from './constants';
import { Post } from './types';
@Injectable({
providedIn: 'root'
})
export class PostService {
api: string = `${environment.BASE_URL_JSON_PLACEHOLDER_API}/posts`;
constructor(private http: HttpClient) {
}
getPosts(): Promise<Post[]> {
return this.http.get<Post[]>(this.api).toPromise()
.catch((reason: any) => {
console.error(`PostService::getPosts reason=`, reason);
// Note: Letting the calling component decide how to handle error
throw reason;
})
.then(this.shuffle);
}
private shuffle(posts: Post[]): Post[] {
if(SHUFFLE) {
/* Note: This is not super efficient. If dealing with larger arrays, this will need replaced
* by a more performant solution */
return posts.sort(() => Math.random() - 0.5);
} else {
return posts;
}
}
}