github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/manage/app/crud/crud.service.ts (about)

     1  import { Injectable }    from '@angular/core';
     2  import { Http, Headers }    from '@angular/http';
     3  
     4  import 'rxjs/add/operator/toPromise';
     5  
     6  import { CrudItem }         from './crud-item'
     7  
     8  export abstract class CrudService<T extends CrudItem> {
     9      http: Http;
    10      defaultHttpHeaders = new Headers(
    11          {'Content-Type': 'application/json'});
    12      baseUrl: string;
    13  
    14      // Decode Json from API and build crud object
    15      abstract decode(jsonObj: any): T;
    16  
    17      // Encode crud object to API json
    18      abstract encode(obj: T): any;
    19  
    20      list(): Promise<T[]> {
    21          var self = this
    22          return this.http.get(
    23                  this.baseUrl,
    24                  { headers: this.defaultHttpHeaders })
    25              .toPromise()
    26              .then(function (response) {
    27                  let items: T[] = [];
    28  
    29                  for (let jsonObj of response.json()) {
    30                      items.push(self.decode(jsonObj));
    31                  }
    32  
    33                  return items;
    34              })
    35              .catch(this.handleError);
    36      }
    37  
    38      get(id: string): Promise<T> {
    39          var self = this
    40          return this.http
    41              .get(
    42                  this.baseUrl + "/" + id,
    43                  { headers: this.defaultHttpHeaders })
    44              .toPromise()
    45              .then(function (response) {
    46                  let jsonObj = response.json();
    47                  return self.decode(jsonObj);
    48              })
    49              .catch(this.handleError);
    50      }
    51  
    52      delete(id: string): Promise<boolean> {
    53          var self = this
    54          return this.http.delete(this.baseUrl + "/" + id)
    55              .toPromise()
    56              .then(function (response) {
    57                  if (response.ok) {
    58                      return true;
    59                  } else {
    60                      throw 'Error creating user ' + response.text;
    61                  }
    62              })
    63              .catch(this.handleError);
    64      }
    65  
    66      add(obj: T): Promise<T> {
    67          return this.http
    68              .post(
    69                  this.baseUrl,
    70                  this.encode(obj),
    71                  { headers: this.defaultHttpHeaders })
    72              .toPromise()
    73              .then(function (response) {
    74                  if (response.ok) {
    75                      return obj;
    76                  } else {
    77                      throw 'Error creating user ' + response.text;
    78                  }
    79              })
    80              .catch(this.handleError);
    81      }
    82  
    83      update(obj: T): Promise<T> {
    84          return this.http
    85              .put(
    86                  this.baseUrl + "/" + obj.id,
    87                  this.encode(obj),
    88                  { headers: this.defaultHttpHeaders })
    89              .toPromise()
    90              .then(function (response) {
    91                  if (response.ok) {
    92                      return obj;
    93                  } else {
    94                      throw 'Error creating user ' + response.text;
    95                  }
    96              })
    97              .catch(this.handleError);
    98      }
    99  
   100      protected handleError(error: any): Promise<any> {
   101          console.error('An error occurred', error);
   102          return Promise.reject(error.message || error);
   103      }
   104  }