viplist.service.ts
1.92 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {forkJoin as observableForkJoin, Observable} from 'rxjs';
import {first, switchMap, map} from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {Vip} from './models/vip.model';
import {User} from '../../shared/models/user.model';
import {UserService} from '../../shared/services/user.service';
import {HttpClient} from '@angular/common/http';
import {MOYA_BASE_URL} from '../../shared/tools/moya-rest.tool';
@Injectable()
export class ViplistService {
constructor(private http: HttpClient, private userService: UserService) {
}
/**
* get vips
* @param searchString: searchString, skip to return all vips
*/
public get(searchString?: string): Observable<Array<Vip>> {
if (!searchString) {
return this.http.get(MOYA_BASE_URL + 'v3/vip/all').pipe(
switchMap(res => observableForkJoin(...(res as Array<any>).map(apiRow => this.hostPopulator(apiRow)))));
}
return this.http.get(MOYA_BASE_URL + 'v3/vip/search/' + searchString).pipe(
switchMap(v => observableForkJoin(...(v as Array<any>).map(x => this.hostPopulator(x)))));
}
/**
* Delete vip.
*
* @param vip
* @return Promise
*/
public async delete(vip: Vip): Promise<boolean> {
if (!vip.id) {
throw new Error('TODO: errori, tyhmä vippi');
}
const res: any = await this.http.delete(MOYA_BASE_URL + 'v3/vip/' + vip.id).pipe(
first())
.toPromise();
return res.ok;
}
public getWithId(id: number): Observable<Vip> {
return this.http.get(MOYA_BASE_URL + 'v3/vip/' + id).pipe(map(x => x as Vip));
}
public create(vip: Vip): Observable<Vip> {
return this.http.post(MOYA_BASE_URL + 'v3/vip/create', vip).pipe(map(x => x as Vip));
}
private hostPopulator(rawVip: any): Observable<Vip> {
return this.userService.get(rawVip.hostId).pipe(
map((u: User) => {rawVip.host = u; return <Vip> rawVip; }),map(x => x as Vip),);
}
}