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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Guid } from 'guid-typescript';
import { Observable } from 'rxjs';
import { Language } from 'src/models/language.model';
import { AppConstants } from '../app-constants.module';
import { TokenService } from './token.service';
@Injectable({
providedIn: 'root'
})
export class LanguageService {
constructor(private _http: HttpClient, private _tokenService: TokenService)
{ }
/* Requests from session storage */
createLanguageWithSessionStorageRequest(name: string): Observable<object> {
const token = this._tokenService.getTokenFromSessionStorage();
return this.createLanguageRequest(name, token);
}
getAllLanguagesWithSessionStorageRequest(): Observable<object> {
const token = this._tokenService.getTokenFromSessionStorage();
return this.getAllLanguagesRequest(token);
}
putLanguageWithSessionStorageRequest(langId: Guid, newName: string): Observable<object> {
const token = this._tokenService.getTokenFromSessionStorage();
return this.putLanguageRequest(token, langId, newName);
}
deleteLanguageWithSessionStorageRequest(langId: Guid): Observable<object> {
const token = this._tokenService.getTokenFromSessionStorage();
return this.deleteLanguageRequest(token, langId);
}
/* Language requests */
createLanguageRequest(name: string, authToken: string): Observable<object> {
const body = {
name: name
};
const options = {
headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
};
return this._http.post(AppConstants.API_LANGUAGE_URL, body, options);
}
getLanguageRequest(langId: Guid): Observable<object> {
const options = {
params: new HttpParams().set('Id', langId.toString()),
};
return this._http.get(AppConstants.API_LANGUAGE_URL, options);
}
getAllLanguagesRequest(authToken: string): Observable<object> {
const options = {
headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
};
return this._http.get(AppConstants.API_LANGUAGE_URL + '/GetLanguages', options);
}
getFullLanguagesFromIncomplete(givenLanguages: Language[]): Promise<Language[]> {
if (givenLanguages.length === 0) {
return new Promise(resolve => resolve(givenLanguages));
}
// This accepts language array with incomplete languages, meaning
// languages that only have an id, but no name
return new Promise(resolve => {
const lastGuid = givenLanguages[givenLanguages.length - 1].id;
// For each language, request his name and assign it
for (const lang of givenLanguages) {
this.getLanguageRequest(lang.id).subscribe(
(result: object) => {
// this only assigns the "name" property to the language,
// because only the name is returned from the request
Object.assign(lang, result);
if (lastGuid === lang.id) {
resolve(givenLanguages);
}
}
);
}
});
}
putLanguageRequest(authToken: string, langId: Guid, newName: string): Observable<object> {
const body = {
name: newName
};
const options = {
params: new HttpParams().set('Id', langId.toString()),
headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
};
return this._http.put(AppConstants.API_LANGUAGE_URL, body, options);
}
deleteLanguageRequest(authToken: string, langId: Guid): Observable<object> {
const options = {
params: new HttpParams().set('LanguageId', langId.toString()),
headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
};
return this._http.delete(AppConstants.API_LANGUAGE_URL, options);
}
}
|