Project

General

Profile

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

    
5
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
6
import { Injectable } from '@angular/core';
7
import { environment } from '../../environments/environment';
8
import {
9
  AggregationDetails,
10
  Country, MetricsInfo,
11
  Repository,
12
  RepositoryInterface,
13
  RepositorySnippet, RepositorySummaryInfo, TermsOfUse,
14
  Timezone,
15
  Typology, User
16
} from '../domain/typeScriptClasses';
17
import { Observable, of } from 'rxjs';
18
import { timezones } from '../domain/timezones';
19
import { typologies } from '../domain/typologies';
20
import {URLParameter} from '../domain/url-parameter';
21

    
22
const headerOptions = {
23
  headers : new HttpHeaders().set('Content-Type', 'application/json')
24
                             .set('Accept', 'application/json'),
25
  withCredentials: true
26
};
27

    
28

    
29
@Injectable ()
30
export class RepositoryService {
31
  private apiUrl = environment.API_ENDPOINT + '/repositories/';
32
  private dashboardAPIUrl = environment.API_ENDPOINT + '/dashboard/';
33

    
34
  constructor(private httpClient: HttpClient) { }
35

    
36
  addInterface(datatype: string, repoId: string, registeredBy: string, comment: string, newInterface: RepositoryInterface): Observable<RepositoryInterface> {
37
    let url;
38
    if (comment == null || comment === '') {
39
      url = `${this.apiUrl}addInterface?datatype=${datatype}&repoId=${repoId}&registeredBy=${registeredBy}`;
40
    } else {
41
      url = `${this.apiUrl}addInterface?datatype=${datatype}&repoId=${repoId}&registeredBy=${registeredBy}&comment=${comment}`;
42
    }
43
    console.log(`knocking on: ${url}`);
44
    console.log(`sending ${JSON.stringify(newInterface)}`);
45
    return this.httpClient.post<RepositoryInterface>(url, newInterface, headerOptions);
46
  }
47

    
48
  updateInterface(repoId: string, registeredBy: string, comment: string, interfaceInfo: RepositoryInterface): Observable<RepositoryInterface> {
49
    let url;
50
    console.log(comment);
51
    if (comment == null || comment === '') {
52
      url = `${this.apiUrl}updateRepositoryInterface?repoId=${repoId}&registeredBy=${registeredBy}`;
53
    } else {
54
      url  = `${this.apiUrl}updateRepositoryInterface?repoId=${repoId}&registeredBy=${registeredBy}&comment=${comment}`;
55
    }
56
    console.log(`knocking on: ${url}`);
57
    console.log(`sending ${JSON.stringify(interfaceInfo)}`);
58
    return this.httpClient.post<RepositoryInterface>(url, interfaceInfo, headerOptions);
59
  }
60

    
61
  deleteInterface(id: string, registeredBy: string) {
62
    const url = `${this.apiUrl}deleteInterface/?id=${id}&registeredBy=${registeredBy}`;
63
    console.log(`knocking on: ${url}`);
64

    
65
    return this.httpClient.delete(url, {withCredentials: true, responseType: 'text'});
66
  }
67

    
68
  addRepository(datatype: string, newRepository: Repository): Observable<Repository> {
69
    const url = `${this.apiUrl}addRepository?datatype=${datatype}`;
70
    console.log(`knocking on: ${url}`);
71
    console.log(`sending ${JSON.stringify(newRepository)}`);
72
    return this.httpClient.post<Repository>(url, newRepository, headerOptions);
73
  }
74

    
75
  // updateRepository(repoInfo: Repository): Observable<Repository> {
76
  //   const url = `${this.apiUrl}updateRepository`;
77
  //   console.log(`knocking on: ${url}`);
78
  //   console.log(`sending ${JSON.stringify(repoInfo)}`);
79
  //   return this.httpClient.post<Repository>(url, repoInfo, headerOptions);
80
  // }
81

    
82
  updateRepository(repoInfo: Repository): Observable<Repository> {
83
    const url = `${this.apiUrl}updateRepository`;
84
    console.log(`knocking on: ${url}`);
85
    console.log(`sending ${JSON.stringify(repoInfo)}`);
86
    return this.httpClient.post<Repository>(url, repoInfo, headerOptions);
87
  }
88

    
89
  updateRepositoriesTerms(termsList: any): Observable<TermsOfUse> {
90
    const url = `${this.apiUrl}terms`;
91
    console.log(`knocking on: ${url}`);
92
    console.log(`sending ${JSON.stringify(termsList)}`);
93
    return this.httpClient.post<TermsOfUse>(url, termsList, headerOptions);
94
  }
95

    
96
  getRepositoriesOfCountry(country: string, mode: string): Observable<RepositorySnippet[]> {
97
    const url = `${this.apiUrl}getRepositoriesByCountry/${country}/${mode}`;
98
    console.log(`knocking on: ${url}`);
99
    return this.httpClient.get<RepositorySnippet[]>(url, headerOptions);
100
  }
101

    
102
  getRepositoriesSnippetsOfUser(): Observable<RepositorySnippet[]> {
103
    const url = `${this.apiUrl}snippets/user`;
104
    console.log(`knocking on: ${url}`);
105
    return this.httpClient.get<RepositorySnippet[]>(url, headerOptions);
106
  }
107

    
108
  getRepositoryById(id: string): Observable<Repository> {
109
    const   url = `${this.apiUrl}getRepositoryById/${id}`;
110
    console.log(`knocking on: ${url}`);
111
    return this.httpClient.get<Repository>(url, headerOptions);
112
  }
113

    
114
  getRepositoryInterface(id: string): Observable<RepositoryInterface[]> {
115
    const url = `${this.apiUrl}getRepositoryInterface/${id}`;
116
    console.log(`knocking on: ${url}`);
117
    return this.httpClient.get<RepositoryInterface[]>(url, headerOptions);
118
  }
119

    
120
  getUrlsOfUserRepos(): Observable<string[]> {
121
    const url = `${this.apiUrl}getUrlsOfUserRepos/0/100/`;
122
    console.log(`knocking on: ${url}`);
123
    return this.httpClient.get<string[]>(url, headerOptions);
124
  }
125

    
126
  getRepositoryAggregations(id: string): Observable<AggregationDetails[]> {
127
    const url = `${this.apiUrl}getRepositoryAggregations/${id}`;
128
    console.log(`knocking on: ${url}`);
129
    return this.httpClient.get<AggregationDetails[]>(url, headerOptions);
130
  }
131

    
132
  getRepositoryAggregationsByYear(id: string): Observable<Map<string, AggregationDetails[]>> {
133
    const url = `${this.apiUrl}getRepositoryAggregationsByYear/${id}`;
134
    console.log(`knocking on: ${url}`);
135
    return this.httpClient.get<Map<string, AggregationDetails[]>>(url, headerOptions);
136
  }
137

    
138
  getTimezones(): Observable<Timezone[]> {
139
/*    const url = `${this.apiUrl}getTimezones`;
140
    console.log(`knocking on: ${url}`);
141
    return this.httpClient.get<Timezone[]>(url, headerOptions);*/
142
    return of(<Timezone[]>timezones);
143
  }
144

    
145
  getTypologies(): Observable<Typology[]> {
146
/*    const url = `${this.apiUrl}getTypologies`;
147
    console.log(`knocking on: ${url}`);
148
    return this.httpClient.get<Typology[]>(url, headerOptions);*/
149
    return of(<Typology[]>typologies);
150
  }
151

    
152
  getCountries(): Observable<Country[]> {
153
    const url = `${this.apiUrl}countries`;
154
    console.log(`knocking on: ${url}`);
155
    return this.httpClient.get<Country[]>(url, headerOptions);
156
  }
157

    
158

    
159
  getCompatibilityClasses (mode: string): Observable<Map<string, string>> {
160
    const url = `${this.apiUrl}getCompatibilityClasses/${mode}`;
161
    console.log(`knocking on: ${url}`);
162
    return this.httpClient.get<Map<string, string>>(url, headerOptions);
163
  }
164

    
165
  getDatasourceClasses(mode: string): Observable<Object> {
166
    const url = `${this.apiUrl}getDatasourceClasses/${mode}`;
167
    console.log(`knocking on: ${url}`);
168
    return this.httpClient.get<Map<string, string>>(url, headerOptions);
169
  }
170

    
171

    
172
  getMetricsInfoForRepository (repoId: string): Observable<MetricsInfo> {
173
    const url = `${this.apiUrl}getMetricsInfoForRepository/${repoId}`;
174
    console.log(`knocking on: ${url}`);
175
    return this.httpClient.get<MetricsInfo>(url, headerOptions);
176
  }
177

    
178
  getListLatestUpdate(mode: string): Observable<any> {
179
    const url = `${this.apiUrl}getListLatestUpdate/${mode}`;
180
    console.log(`knocking on: ${url}`);
181
    return this.httpClient.get<any>(url, headerOptions);
182
  }
183

    
184
  searchRegisteredRepositories(page, size, urlParams: URLParameter[]) {
185
    const url = `${this.apiUrl}searchRegisteredRepositories/${page}/${size}`;
186
    console.log(`knocking on: ${url}`);
187
    let params = new HttpParams();
188
    for (const urlParameter of urlParams) {
189
      for (const value of urlParameter.value) {
190
        params = params.append(urlParameter.key, value);
191
      }
192
    }
193

    
194
    return this.httpClient.get<RepositorySnippet[]>(url, {params, withCredentials: true});
195
  }
196

    
197
  getRepositoriesSummaryInfo(): Observable<RepositorySummaryInfo[]> {
198
    const url = `${this.dashboardAPIUrl}getRepositoriesSummary/0/100`;
199
    console.log(`knocking on: ${url}`);
200
    return this.httpClient.get<RepositorySummaryInfo[]>(url, headerOptions);
201
  }
202

    
203
  getRepositoryAdmins(repoId: string): Observable<User[]> {
204
    const url = `${this.apiUrl}${repoId}/admins`;
205
    console.log(`knocking on: ${url}`);
206
    return this.httpClient.get<User[]>(url, headerOptions);
207
  }
208

    
209
  deleteRepositoryAdmin(repoId: string, repoAdminEmail: string) {
210
    const url = `${this.apiUrl}${repoId}/admins/${repoAdminEmail}`;
211
    console.log(`knocking on: ${url}`);
212

    
213
    return this.httpClient.delete(url, headerOptions);
214
  }
215

    
216
  addRepositoryAdmin(repoId: string, repoAdminEmail: string) {
217
    const url = `${this.apiUrl}${repoId}/admins`;
218
    return this.httpClient.post<string>(url, repoAdminEmail, headerOptions);
219
  }
220
}
(9-9/13)