Newer
Older
import { Injectable, Inject } from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import { Observable, of } from 'rxjs';
import {catchError, map, switchMap} from 'rxjs/operators';
import {LoginData, BookletStatus, PersonTokenAndTestId, KeyValuePair, SysConfig} from './app.interfaces';
import {ErrorHandler, ServerError} from 'iqb-components';
// ============================================================================
@Injectable()
export class BackendService {

Martin Mechtel
committed
private serverSlimAdminUrl = '';
private serverUrl2 = 'http://localhost/testcenter-iqb-php/'; // TODO (BEFORE-MERGE) REMOVE

Martin Mechtel
committed
@Inject('SERVER_URL') private readonly serverUrl: string,
this.serverSlimUrl = this.serverUrl + 'php_tc/login.php/';

Martin Mechtel
committed
this.serverSlimAdminUrl = this.serverUrl + 'admin/php/login.php/';
this.serverSlimUrl_Close = this.serverUrl + 'php_tc/tc_post.php/';
this.serverUrl = this.serverUrl + 'php_start/';
}
login(name: string, password: string): Observable<LoginData | ServerError> {
.post<LoginData>(this.serverUrl2 + '/login/group', {name, password})

Martin Mechtel
committed
catchError(ErrorHandler.handle),
switchMap(myLoginData => {
if (myLoginData instanceof ServerError) {

Martin Mechtel
committed
return this.http
.post<LoginData>(this.serverUrl2 + '/login/admin', {n: name, p: password})

Martin Mechtel
committed
.pipe(
catchError(ErrorHandler.handle)
);
} else {

Martin Mechtel
committed
}
} else {
return of(myLoginData);
}
})
}
// BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
getLoginData(loginToken: string, personToken: string, bookletDbId: number): Observable<LoginData | ServerError> {
.post<LoginData>(this.serverSlimUrl + 'login', {lt: loginToken, pt: personToken, b: bookletDbId})
getLoginDataAdmin(adminToken: string): Observable<LoginData | ServerError> {
return this.http
.post<LoginData>(this.serverSlimAdminUrl + 'login', {at: adminToken})
.pipe(
catchError(ErrorHandler.handle)
);
}
return this.http
.get<SysConfig>(this.serverUrl2 + `system/config`)
.pipe(catchError(() => of(null)))
.pipe(map((sysConfig: SysConfig): KeyValuePair => {
console.log(sysConfig.version); // check for system version missmatch https://github.com/iqb-berlin/testcenter-iqb-ng/issues/53
return sysConfig.customTexts;
}));
getBookletState(bookletName: string, code = ''): Observable<BookletStatus | ServerError> {
const params = new HttpParams().set('code', code);
return this.http
.get<BookletStatus>(this.serverUrl2 + `booklet/${bookletName}/state`, {params})
.pipe(catchError(ErrorHandler.handle));
startBooklet(code: string, bookletName: string, bookletLabel: string): Observable<PersonTokenAndTestId | ServerError> {
.put<PersonTokenAndTestId>(this.serverUrl2 + `test`, {code, bookletName, bookletLabel})
addBookletLogClose(testId: number): Observable<boolean | ServerError> {
.put<boolean>(this.serverUrl2 + `test/${testId}/log`, {timestamp: Date.now(), entry: 'BOOKLETLOCKEDbyTESTEE'})
.pipe(catchError(ErrorHandler.handle));
lockBooklet(testId: number): Observable<boolean | ServerError> {
.post<boolean>(this.serverUrl2 + `test/${testId}/lock`, {})
.pipe(catchError(ErrorHandler.handle));