Skip to content
Snippets Groups Projects
backend.service.ts 3.82 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 } from 'rxjs';
    
    import { catchError, map } from 'rxjs/operators';
    
    mechtelm's avatar
    mechtelm committed
    import {UnitData, TaggedString, TestData} from './test-controller.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(() => of(false))
          );
    
    mechtelm's avatar
    mechtelm committed
      saveBookletReview(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),
            catchError(() => of(false))
          );
    
    mechtelm's avatar
    mechtelm committed
      getTestData(testId: string): Observable<TestData | number> {
    
        return this.http
    
    mechtelm's avatar
    mechtelm committed
          .get<TestData>(this.serverUrl + 'test/' + testId)
          .pipe(
            catchError(errCode => of(errCode))
          );
    
    mechtelm's avatar
    mechtelm committed
      getUnitData(testId: string, unitid: string): Observable<UnitData | number> {
    
        return this.http
    
          .get<UnitData>(this.serverUrl + 'test/' + testId + '/unit/' + unitid)
    
    mechtelm's avatar
    mechtelm committed
          .pipe(
            catchError(errCode => of(errCode))
          );
    
    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}),
    
    mechtelm's avatar
    mechtelm committed
            catchError(errCode => of(errCode))
    
    mechtelm's avatar
    mechtelm committed
      addUnitLog(testId: string, timestamp: number, unitName: string, entry: string): Observable<boolean> {
    
        return this.http
    
          .put(this.serverUrl + `test/${testId}/unit/${unitName}/log`, {timestamp, entry})
          .pipe(
            map(() => true),
            catchError(() => of(false))
          );
    
    mechtelm's avatar
    mechtelm committed
      addBookletLog(testId: string, timestamp: number, entry: string): Observable<boolean> {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
          .put(this.serverUrl + `test/${testId}/log`, {timestamp, entry})
          .pipe(
            map(() => true),
            catchError(() => of(false))
          );
    
    mechtelm's avatar
    mechtelm committed
      setUnitState(testId: string, unitName: string, stateKey: string, state: string): Observable<boolean> {
    
        return this.http
    
          .patch(this.serverUrl + `test/${testId}/unit/${unitName}/state`, {key: stateKey, value: state})
          .pipe(
            map(() => true),
            catchError(() => of(false))
          );
    
    mechtelm's avatar
    mechtelm committed
      setBookletState(testId: string, stateKey: string, state: string): Observable<boolean> {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
          .patch(this.serverUrl + `test/${testId}/state`, {key: stateKey, value: state})
          .pipe(
            map(() => true),
            catchError(() => of(false))
          );
    
    mechtelm's avatar
    mechtelm committed
      newUnitResponse(testId: string, timestamp: number, unitName: string, response: string, responseType: string)
        : Observable<boolean> {
    
          .put(this.serverUrl + `test/${testId}/unit/${unitName}/response`, {timestamp, response, responseType})
          .pipe(
            map(() => true),
            catchError(() => of(false))
          );
    
    mechtelm's avatar
    mechtelm committed
      newUnitRestorePoint(testId: string, unitName: string, timestamp: number, restorePoint: string): Observable<boolean> {
    
        return this.http
    
          .patch(this.serverUrl + `test/${testId}/unit/${unitName}/restorepoint`, {timestamp, restorePoint})
          .pipe(
            map(() => true),
            catchError(() => of(false))
          );