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
    if (comment == null || comment === '') {
51
      url = `${this.apiUrl}updateRepositoryInterface?repoId=${repoId}&registeredBy=${registeredBy}`;
52
    } else {
53
      url  = `${this.apiUrl}updateRepositoryInterface?repoId=${repoId}&registeredBy=${registeredBy}&comment=${comment}`;
54
    }
55
    console.log(`knocking on: ${url}`);
56
    console.log(`sending ${JSON.stringify(interfaceInfo)}`);
57
    return this.httpClient.post<RepositoryInterface>(url, interfaceInfo, headerOptions);
58
  }
59

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
157

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

    
164
  getDatasourceClasses(mode: string): Observable<Map<string, string>> {
165
    console.log('mode: ' + mode);
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)