Project

General

Profile

1
/*
2
* Created by myrto on 12/05/2017
3
*/
4

    
5
/*
6
*  !!! USING TEMPORARY API ADDRESS AND USER
7
*/
8

    
9
import { Injectable } from '@angular/core';
10
import { Observable } from 'rxjs/Observable';
11
import 'rxjs/add/operator/map';
12
import { Http, Response, Headers, RequestOptions } from '@angular/http';
13

    
14
import { Country, MetricsInfo, Repository, RepositoryInterface, Timezone } from '../domain/typeScriptClasses';
15
import { apiUrl } from '../domain/tempAPI';
16

    
17
let headers = new Headers({ 'Content-Type': 'application/json' });
18
let httpOptions = new RequestOptions({ headers: headers });
19

    
20
@Injectable ()
21
export class RepositoryService {
22
  private apiUrl = apiUrl;
23

    
24
  constructor(private http: Http) { }
25

    
26
  getRepositoriesOfCountry(country: string, mode: string): Observable<Repository[]> {
27
    let url = `${this.apiUrl}/repository/getRepositoriesByCountry/${country}/${mode}`;
28
    console.log(`knocking on: ${url}`);
29
    return this.http.get(url)
30
      .map( res => <Repository[]>res.json())
31
      .catch(this.handleError);
32
  }
33

    
34
  getRepositoriesOfUser(userEmail: string): Observable<Repository[]> {
35
    let url = `${this.apiUrl}/repository/getRepositoriesOfUser/${userEmail}/0/100`;
36
    console.log(`knocking on: ${url}`);
37
    return this.http.get(url)
38
      .map( res => <Repository[]>res.json())
39
      .do(res => console.log(`counted ${res.length} repositories`))
40
      .catch(this.handleError);
41
  }
42

    
43

    
44
  getRepositoryById(id: string): Observable<Repository> {
45
    let url = `${this.apiUrl}/repository/getRepositoryById/${id}`;
46
    console.log(`knocking on: ${url}`);
47
    return this.http.get(url)
48
      .map( res => <Repository>res.json())
49
      .do(res => console.log(`got repository with name: ${res.officialName}`))
50
      .catch(this.handleError);
51
  }
52

    
53
  getRepositoryInterface(id: string): Observable<RepositoryInterface[]>{
54
    let url = `${this.apiUrl}/repository/getRepositoryInterface/${id}`;
55
    console.log(`knocking on: ${url}`);
56
    return this.http.get(url)
57
      .map( res => <RepositoryInterface[]>res.json())
58
      .catch(this.handleError);
59
  }
60

    
61
  getUrlsOfUserRepos(userEmail: string): Observable<string[]>{
62
    let url = `${this.apiUrl}/repository/getUrlsOfUserRepos/${userEmail}/0/100/`;
63
    console.log(`knocking on: ${url}`);
64
    return this.http.get(url)
65
      .map( res => <string[]>res.json())
66
      .catch(this.handleError);
67
  }
68

    
69
  getTimezones(): Observable<Timezone[]>{
70
    let url = `${this.apiUrl}/repository/getTimezones`;
71
    console.log(`knocking on: ${url}`);
72
    return this.http.get(url)
73
      .map( res => <Timezone[]>res.json())
74
      .catch(this.handleError);
75
  }
76

    
77
  getCountries(): Observable<Country[]> {
78
    let url = `${this.apiUrl}/repository/getCountries`;
79
    console.log(`knocking on: ${url}`);
80
    return this.http.get(url)
81
      .map( res => <Country[]>res.json())
82
      .catch(this.handleError);
83
  }
84

    
85

    
86
  getCompatibilityClasses (mode: string): Observable<Map<string,string>> {
87
    let url = `${this.apiUrl}/repository/getCompatibilityClasses/${mode}`;
88
    console.log(`knocking on: ${url}`);
89
    return this.http.get(url)
90
      .map( res => <Map<string,string>>res.json())
91
      .catch(this.handleError);
92
  }
93

    
94
  getDatasourceClasses(mode: string): Observable<Map<string,string>>{
95
    let url = `${this.apiUrl}/repository/getDatasourceClasses/${mode}`;
96
    console.log(`knocking on: ${url}`);
97
    return this.http.get(url)
98
      .map( res => <Map<string,string>>res.json())
99
      .catch(this.handleError);
100
  }
101

    
102

    
103
  getMetricsInfoForRepository (repoId: string): Observable<MetricsInfo> {
104
    let url = `${this.apiUrl}/repository/getMetricsInfoForRepository/${repoId}`;
105
    console.log(`knocking on: ${url}`);
106
    return this.http.get(url)
107
      .map( res => <MetricsInfo>res.json())
108
      .catch(this.handleError);
109
  }
110

    
111
  updateEnglishName(id: string, englishname: string): Observable<string>{
112
    let url = `${this.apiUrl}/repository/updateEnglishName?id=${id}&officialName=DSpace&englishname=${englishname}`;
113
    console.log(`knocking on: ${url}`);
114
    httpOptions.withCredentials = true;
115
    return this.http.post(url,httpOptions)
116
      .map( res => {
117
        console.log(`responded ${res.statusText}`);
118
        return res.status.toString();
119
      })
120
      .catch(this.handleError).share();
121
  }
122

    
123
  updateLongtitude(id: string, longtitude: string): Observable<string>{
124
    let url = `${this.apiUrl}/repository/updateLongtitude`;
125
    console.log(`knocking on: ${url}`);
126
    let body = JSON.stringify({
127
      id : id,
128
      logntitude: longtitude
129
    });
130
    console.log(`sending ${body}`);
131
    httpOptions.withCredentials = true;
132

    
133
    return this.http.post(url,body,httpOptions)
134
      .map( res => <string>res.json())
135
      .catch(this.handleError).share();
136
  }
137

    
138
  updateLatitude(id: string, latitude: string): Observable<string>{
139
    let url = `${this.apiUrl}/repository/updateLatitude`;
140
    console.log(`knocking on: ${url}`);
141
    let body = JSON.stringify({
142
      id : id,
143
      latitude: latitude
144
    });
145
    console.log(`sending ${body}`);
146
    httpOptions.withCredentials = true;
147

    
148
    return this.http.post(url,body,httpOptions)
149
      .map( res => <string>res.json())
150
      .catch(this.handleError).share();
151
  }
152

    
153
  updateLogoUrl(id: string, logoUrl: string): Observable<string>{
154
    let url = `${this.apiUrl}/repository/updateLogoUrl`;
155
    console.log(`knocking on: ${url}`);
156
    let body = JSON.stringify({
157
      id : id,
158
      logoUrl: logoUrl
159
    });
160
    console.log(`sending ${body}`);
161
    httpOptions.withCredentials = true;
162

    
163
    return this.http.post(url, body, httpOptions)
164
      .map( res => <string>res.json())
165
      .catch(this.handleError).share();
166
  }
167

    
168
  updateTimezone(id: string, timezone: string): Observable<string>{
169
    let url = `${this.apiUrl}/repository/updateTimezone`;
170
    console.log(`knocking on: ${url}`);
171
    let body = JSON.stringify({
172
      id : id,
173
      timezone: timezone
174
    });
175
    console.log(`sending ${body}`);
176
    httpOptions.withCredentials = true;
177

    
178
    return this.http.post(url, body, httpOptions)
179
      .map( res => <string>res.json())
180
      .catch(this.handleError).share();
181
  }
182

    
183

    
184
  private handleError(error: Response | any) {
185
    // In a real world app, we might use a remote logging infrastructure
186
    // We'd also dig deeper into the error to get a better message
187
    let errMsg = "";
188
    console.log('E R R O R !!');
189
    console.log(error);
190
    if (error instanceof Response) {
191
      const body = error.text() || '';
192
      //const err = body.error || JSON.stringify(body);
193
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
194
      console.log(errMsg);
195
    } else {
196
      errMsg = (error.message) ? error.message :
197
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
198
      console.error(errMsg); // log to console instead
199
    }
200
    return Observable.throw(errMsg);
201
  }
202

    
203
}
(7-7/8)