github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/manage/app/dashboard/dashboard.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 { DashboardInfo, DashboardBestSeller}  from './dashboardInfo';
     7  
     8  import { CrudService }  from '../crud/crud.service';
     9  
    10  declare var Config: any; //  this comes from the autogenerated config.js file
    11  
    12  @Injectable()
    13  export class DashboardService{
    14      private masterFileListUrl: string;
    15      private http:Http;
    16      private baseUrl: string;
    17      defaultHttpHeaders = new Headers({'Content-Type': 'application/json'});
    18      
    19  
    20      constructor (http: Http) {
    21          this.http = http;
    22          this.baseUrl = Config.frontend.url;
    23          this.masterFileListUrl = Config.frontend.url + '/api/v1/repositories/master-files';
    24      }
    25  
    26  
    27      decode(jsonObj: any): DashboardInfo {
    28          return {
    29              publicationCount: jsonObj.publicationCount,
    30              userCount: jsonObj.userCount,
    31              buyCount: jsonObj.buyCount,
    32              loanCount: jsonObj.loanCount
    33          }
    34      }
    35  
    36      get(): Promise<DashboardInfo> {
    37          var self = this
    38          return this.http
    39              .get(
    40                  this.baseUrl + "/dashboardInfos",
    41                  { headers: this.defaultHttpHeaders })
    42              .toPromise()
    43              .then(function (response) {
    44                  let jsonObj = response.json();
    45                  return self.decode(jsonObj);
    46              })
    47              .catch(this.handleError);
    48      }
    49  
    50      getBestSeller(): Promise<DashboardBestSeller[]> {
    51          var self = this
    52          return this.http
    53              .get(
    54                  this.baseUrl + "/dashboardBestSellers",
    55                  { headers: this.defaultHttpHeaders })
    56              .toPromise()
    57              .then(function (response) {
    58                  if (response.ok) {
    59                      let items: DashboardBestSeller[] = [];
    60  
    61                      for (let jsonObj of response.json()) {
    62                          items.push(jsonObj);
    63                      }
    64  
    65                      return items;
    66                  } else {
    67                      throw 'Error creating user ' + response.text;
    68                  }
    69              })
    70              .catch(this.handleError);
    71      }
    72  
    73      protected handleError(error: any): Promise<any> {
    74          console.error('An error occurred', error);
    75          return Promise.reject(error.message || error);
    76      }
    77  }