Newer
Older
import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
// import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { catchError } from 'rxjs/operators';
@Injectable()
export class BackendService {
constructor(
@Inject('SERVER_URL') private serverUrl: string,
private http: HttpClient) {
this.serverUrl = this.serverUrl + 'admin/';
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
114
115
116
117
118
119
120
121
122
123
124
125
126
private errorHandler(error: Error | any): Observable<any> {
return Observable.throw(error);
}
// *******************************************************************
login(name: string, password: string): Observable<LoginStatusResponseData | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<LoginStatusResponseData>(this.serverUrl + 'loginAdmin.php', {n: name, p: password}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
// *******************************************************************
logout(token: string): Observable<boolean | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<boolean>(this.serverUrl + 'logoutAdmin.php', {at: token}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
// *******************************************************************
getStatus(token: string): Observable<LoginStatusResponseData | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<LoginStatusResponseData>(this.serverUrl + 'getStatusAdmin.php', {at: token}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
// *******************************************************************
// Fehlerbehandlung beim Aufrufer
getFile(token: string, workspaceId: number, filetype: string, filename: string): Observable<GetFileResponseData[] | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<GetFileResponseData[]>(this.serverUrl + 'getFile.php', {
at: token,
ws: workspaceId,
ft: filetype,
fn: filename
}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
// *******************************************************************
// Fehlerbehandlung beim Aufrufer
getFiles(token: string, workspaceId: number): Observable<GetFileResponseData[] | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<GetFileResponseData[]>(this.serverUrl + 'getFileList.php', {at: token, ws: workspaceId}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
// *******************************************************************
// Fehlerbehandlung beim Aufrufer
deleteFiles(token: string, workspaceId: number, filesToDelete: Array<string>): Observable<string | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<string>(this.serverUrl + 'deleteFiles.php', {at: token, ws: workspaceId, f: filesToDelete}, httpOptions)
.pipe(
catchError(this.handleError)
);
}
// *******************************************************************
checkWorkspace(token: string, workspaceId: number): Observable<CheckWorkspaceResponseData | ServerError> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<CheckWorkspaceResponseData>(this.serverUrl + 'checkWorkspace.php', {at: token, ws: workspaceId}, httpOptions)
.pipe(
catchError(this.handleError)
);
}

andreistroescu
committed
showStats(adminToken: string, workspaceId: number): Observable<GroupResponse[]>{
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http
.post<GroupResponse[]>(this.serverUrl + 'getTestStats.php', {at: adminToken, ws: workspaceId}, httpOptions);
}
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
private handleError(errorObj: HttpErrorResponse): Observable<ServerError> {
const myreturn: ServerError = {
label: 'Fehler bei Datenübertragung',
code: errorObj.status
};
if (errorObj.status === 401) {
myreturn.label = 'Fehler: Zugriff verweigert - bitte (neu) anmelden!';
} else if (errorObj.status === 503) {
myreturn.label = 'Fehler: Server meldet Datenbankproblem.';
} else if (errorObj.error instanceof ErrorEvent) {
myreturn.label = 'Fehler: ' + (<ErrorEvent>errorObj.error).message;
} else {
myreturn.label = 'Fehler: ' + errorObj.message;
}
return Observable.throw(myreturn.label);
}
}
// #############################################################################################
export interface LoginStatusResponseData {
admintoken: string;
name: string;
workspaces: WorkspaceData[];
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}
export interface WorkspaceData {
id: number;
name: string;
}
export interface ServerError {
code: number;
label: string;
}
export interface GetFileResponseData {
filename: string;
filesize: number;
filesizestr: string;
filedatetime: string;
filedatetimestr: string;
type: string;
typelabel: string;
isChecked: boolean;
}
export interface CheckWorkspaceResponseData {
errors: string[];
infos: string[];
warnings: string[];
}

andreistroescu
committed
export interface GroupResponse {
name: string;
testsTotal: number;
testsStarted: number;
responsesGiven: number;