Skip to content
Snippets Groups Projects
backend.service.ts 5.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Injectable, Inject } from '@angular/core';
    
    mechtelm's avatar
    mechtelm committed
    import { HttpClient, HttpParams } from '@angular/common/http';
    
    import {Observable, of, Subscription} from 'rxjs';
    
    import {catchError, map, switchMap} from 'rxjs/operators';
    
    import {
      UnitData,
      TaggedString,
      TestData,
      TestStateKey,
      StateReportEntry, AppFocusState
    } from './test-controller.interfaces';
    
    paf's avatar
    paf committed
    import {ApiError} from '../app.interfaces';
    
    
    
    @Injectable({
      providedIn: 'root'
    })
    export class BackendService {
    
      constructor(
        @Inject('SERVER_URL') private serverUrl: string,
    
    mechtelm's avatar
    mechtelm committed
      saveUnitReview(testId: string, unitName: string, priority: number, categories: string, entry: string)
        : Observable<boolean> {
    
        return this.http
    
          .put(this.serverUrl + `test/${testId}/unit/${unitName}/review`, {priority, categories, entry})
          .pipe(
            map(() => true),
    
            catchError((err: ApiError) => {
              console.warn(`saveUnitReview Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
      saveTestReview(testId: string, priority: number, categories: string, entry: string): Observable<boolean> {
    
        return this.http
    
          .put(this.serverUrl + `test/${testId}/review`, {priority, categories, entry})
          .pipe(
            map(() => true),
    
              console.warn(`saveTestReview Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
      getTestData(testId: string): Observable<TestData | boolean> {
    
        return this.http
    
    mechtelm's avatar
    mechtelm committed
          .get<TestData>(this.serverUrl + 'test/' + testId)
          .pipe(
    
            catchError((err: ApiError) => {
              console.warn(`getTestData Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
    mechtelm's avatar
    mechtelm committed
          );
    
      getUnitData(testId: string, unitid: string): Observable<UnitData | boolean> {
    
        return this.http
    
          .get<UnitData>(this.serverUrl + 'test/' + testId + '/unit/' + unitid)
    
    mechtelm's avatar
    mechtelm committed
          .pipe(
    
            catchError((err: ApiError) => {
              console.warn(`getUnitData Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
    mechtelm's avatar
    mechtelm committed
          );
    
    mechtelm's avatar
    mechtelm committed
      getResource(testId: string, internalKey: string, resId: string, versionning = false): Observable<TaggedString | number> {
    
        return this.http
          .get(
    
            this.serverUrl + `test/${testId}/resource/${resId}`,
    
            {
              params: new HttpParams().set('v', versionning ? '1' : 'f'),
              responseType: 'text'
            })
    
    mechtelm's avatar
    mechtelm committed
            map(def => <TaggedString>{tag: internalKey, value: def}),
    
            catchError((err: ApiError) => {
              console.warn(`getResource Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(err.code);
    
      updateTestState(testId: string, newState: StateReportEntry[]): Subscription {
    
        return this.http
    
          .patch(this.serverUrl + `test/${testId}/state`, newState)
    
          .subscribe({error: (err: ApiError) => console.error(`updateTestState Api-Error: ${err.code} ${err.info}`)});
    
      addTestLog(testId: string, logEntries: StateReportEntry[]): Subscription {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
          .put(this.serverUrl + `test/${testId}/log`, logEntries)
    
          .subscribe({error: (err: ApiError) => console.error(`addTestLog Api-Error: ${err.code} ${err.info}`)});
    
      updateUnitState(testId: string, unitName: string, newState: StateReportEntry[]): Subscription {
    
          .patch(this.serverUrl + `test/${testId}/unit/${unitName}/state`, newState)
    
          .subscribe({error: (err: ApiError) => console.error(`setUnitState Api-Error: ${err.code} ${err.info}`)});
    
      addUnitLog(testId: string, unitName: string, logEntries: StateReportEntry[]): Subscription {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
          .put(this.serverUrl + `test/${testId}/unit/${unitName}/log`, logEntries)
          .subscribe({error: (err: ApiError) => console.error(`addUnitLog Api-Error: ${err.code} ${err.info}`)});
    
    mechtelm's avatar
    mechtelm committed
      notifyDyingTest(testId: string) {
    
        // TODO add auth or change end point
    
    mechtelm's avatar
    mechtelm committed
        if (navigator.sendBeacon) {
    
          navigator.sendBeacon(this.serverUrl + `test/${testId}/state`, JSON.stringify(<StateReportEntry>{
            key: TestStateKey.FOCUS, timeStamp: Date.now(), content: AppFocusState.DEAD
          }));
    
      updateUnitStateData(testId: string, timeStamp: number, unitName: string, dataPartsAllString: string, unitStateDataType: string)
    
    mechtelm's avatar
    mechtelm committed
        : Observable<boolean> {
    
        // TODO remove after api changed
        const response = dataPartsAllString;
        const restorePoint = dataPartsAllString;
        const responseType = unitStateDataType;
    
          .put(this.serverUrl + `test/${testId}/unit/${unitName}/response`, {timeStamp, response, responseType})
    
            switchMap(() => {
              return this.http
    
                .patch(this.serverUrl + `test/${testId}/unit/${unitName}/restorepoint`, {timeStamp, restorePoint})
    
                .pipe(
                  map(() => true),
                  catchError((err: ApiError) => {
                    console.warn(`newUnitStateData/restorepoint Api-Error: ${err.code} ${err.info} `);
                    return of(false);
                  })
                );
            }),
    
              console.warn(`newUnitStateData/response Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
      lockTest(testId: string, timeStamp: number, content: string): Observable<boolean> {
    
        return this.http
    
          .patch<boolean>(this.serverUrl + `test/${testId}/lock`, {timeStamp, content})
    
          .pipe(
            map(() => true),
            catchError((err: ApiError) => {
              console.warn(`lockBooklet Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);