Skip to content
Snippets Groups Projects
backend.service.ts 5.77 KiB
Newer Older
  • Learn to ignore specific revisions
  • rhenck's avatar
    rhenck committed
    import { Injectable, Inject } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { Observable, of } from 'rxjs';
    import { catchError, map } from 'rxjs/operators';
    import {
    
      GetFileResponseData, SysCheckStatistics,
    
    rhenck's avatar
    rhenck committed
      ReviewData, LogData, UnitResponse, ResultData
    } from './workspace.interfaces';
    import { WorkspaceDataService } from './workspacedata.service';
    import { ApiError, WorkspaceData } from '../app.interfaces';
    
    Martin Mechtel's avatar
    Martin Mechtel committed
    
    
    @Injectable({
      providedIn: 'root'
    })
    
    Martin Mechtel's avatar
    Martin Mechtel committed
    export class BackendService {
      constructor(
    
        @Inject('SERVER_URL') private readonly serverUrl: string,
    
        private wds: WorkspaceDataService,
    
      getWorkspaceData(workspaceId: string): Observable<WorkspaceData | number> {
        return this.http
    
    paf's avatar
    paf committed
          .get<WorkspaceData>(`${this.serverUrl}workspace/${workspaceId}`)
    
            catchError((err: ApiError) => {
              console.warn(`getWorkspaceData Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(err.code);
    
      getFiles(): Observable<GetFileResponseData> {
    
          .get<GetFileResponseData>(`${this.serverUrl}workspace/${this.wds.wsId}/files`)
    
          .pipe(
            catchError((err: ApiError) => {
              console.warn(`getFiles Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return [];
    
    Martin Mechtel's avatar
    Martin Mechtel committed
      }
    
    
      deleteFiles(filesToDelete: Array<string>): Observable<FileDeletionReport> {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
    paf's avatar
    paf committed
          .request<FileDeletionReport>('delete', `${this.serverUrl}workspace/${this.wds.wsId}/files`, { body: { f: filesToDelete } })
    
          .pipe(
            catchError((err: ApiError) => {
              console.warn(`deleteFiles Api-Error: ${err.code} ${err.info} `);
              return of(<FileDeletionReport> {
                deleted: [],
                not_allowed: [`deleteFiles Api-Error: ${err.code} ${err.info} `],
                did_not_exist: []
    
    paf's avatar
    paf committed
              });
    
    Martin Mechtel's avatar
    Martin Mechtel committed
      }
    
    
      getResultData(): Observable<ResultData[]> {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
    paf's avatar
    paf committed
          .get<ResultData[]>(`${this.serverUrl}workspace/${this.wds.wsId}/results`, {})
    
          .pipe(
            catchError((err: ApiError) => {
              console.warn(`getResultData Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return [];
    
    Martin Mechtel's avatar
    Martin Mechtel committed
      }
    
    
      getResponses(groups: string[]): Observable<UnitResponse[]> {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
    paf's avatar
    paf committed
          .get<UnitResponse[]>(`${this.serverUrl}workspace/${this.wds.wsId}/responses`, { params: { groups: groups.join(',') } })
    
          .pipe(
            catchError((err: ApiError) => {
              console.warn(`getResponses Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return [];
    
    Martin Mechtel's avatar
    Martin Mechtel committed
      }
    
    
      getLogs(groups: string[]): Observable<LogData[]> {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
    paf's avatar
    paf committed
          .get<LogData[]>(`${this.serverUrl}workspace/${this.wds.wsId}/logs`, { params: { groups: groups.join(',') } })
    
          .pipe(
            catchError((err: ApiError) => {
              console.warn(`getLogs Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return [];
    
    Martin Mechtel's avatar
    Martin Mechtel committed
      }
    
    
      getReviews(groups: string[]): Observable<ReviewData[]> {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
    paf's avatar
    paf committed
          .get<ReviewData[]>(`${this.serverUrl}workspace/${this.wds.wsId}/reviews`, { params: { groups: groups.join(',') } })
    
          .pipe(
            catchError((err: ApiError) => {
              console.warn(`getReviews Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return [];
    
      deleteData(groups: string[]): Observable<boolean> {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        return this.http
    
    paf's avatar
    paf committed
          .request('delete', `${this.serverUrl}workspace/${this.wds.wsId}/responses`, { body: { groups } })
    
          .pipe(
            map(() => true),
            catchError((err: ApiError) => {
              console.warn(`deleteData Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
    Martin Mechtel's avatar
    Martin Mechtel committed
      }
    
      getSysCheckReportList(): Observable<SysCheckStatistics[]> {
    
    paf's avatar
    paf committed
          .get<ReviewData[]>(`${this.serverUrl}workspace/${this.wds.wsId}/sys-check/reports/overview`)
    
          .pipe(
            catchError((err: ApiError) => {
              console.warn(`getSysCheckReportList Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return [];
    
    paf's avatar
    paf committed
      getSysCheckReport(reports: string[], enclosure: string, delimiter: string, lineEnding: string)
    
    paf's avatar
    paf committed
          .get(`${this.serverUrl}workspace/${this.wds.wsId}/sys-check/reports`,
    
            {
              params: {
                checkIds: reports.join(','),
    
    paf's avatar
    paf committed
                delimiter,
                enclosure,
                lineEnding
    
    paf's avatar
    paf committed
                Accept: 'text/csv'
    
          .pipe(
            catchError((err: ApiError) => {
              console.warn(`getSysCheckReport Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
      deleteSysCheckReports(checkIds: string[]): Observable <FileDeletionReport> {
    
    paf's avatar
    paf committed
          .request<FileDeletionReport>('delete', `${this.serverUrl}workspace/${this.wds.wsId}/sys-check/reports`, { body: { checkIds } })
    
          .pipe(
            catchError((err: ApiError) => {
              console.warn(`deleteSysCheckReports Api-Error: ${err.code} ${err.info} `);
              return of(<FileDeletionReport> {
                deleted: [],
                not_allowed: [`deleteSysCheckReports Api-Error: ${err.code} ${err.info} `],
                did_not_exist: []
    
    paf's avatar
    paf committed
              });
    
      downloadFile(fileType: string, fileName: string): Observable<Blob | boolean> {
    
    paf's avatar
    paf committed
          .get(`${this.serverUrl}workspace/${this.wds.wsId}/file/${fileType}/${fileName}`, { responseType: 'blob' })
    
          .pipe(
            catchError((err: ApiError) => {
              console.warn(`downloadFile Api-Error: ${err.code} ${err.info} `);
    
    paf's avatar
    paf committed
              return of(false);
    
      }
    }
    
    export interface FileDeletionReport {
      deleted: string[];
      not_allowed: string[];
      did_not_exist: string[];
    
    Martin Mechtel's avatar
    Martin Mechtel committed
    }