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 { HttpHeaders } from '@angular/common/http';
11

    
12
import { Observable } from 'rxjs/Observable';
13

    
14
import { Country, Repository, RepositoryInterface, Timezone, Topic } from '../domain/typeScriptClasses';
15
import 'rxjs/add/operator/map';
16
import { Http, Response, Headers, RequestOptions } from '@angular/http';
17

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

    
21
@Injectable ()
22
export class RepositoryService {
23
/*  private apiUrl = 'http://195.134.66.230:8380/uoa-repository-manager-service'; */
24
  private apiUrl = 'http://194.177.192.121:8380/uoa-repository-manager-service';
25

    
26
  constructor(private http: Http) { }
27

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

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

    
45

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

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

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

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

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

    
87

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

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

    
104

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

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

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

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

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

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

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

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

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

    
182

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

    
202
}
(7-7/8)