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
    comment = newInterface.comments; // temporary fix. TODO: test with backend
39
    if (comment == null || comment === '') {
40
      url = `${this.apiUrl}addInterface?datatype=${datatype}&repoId=${repoId}&registeredBy=${registeredBy}`;
41
    } else {
42
      url = `${this.apiUrl}addInterface?datatype=${datatype}&repoId=${repoId}&registeredBy=${registeredBy}&comment=${comment}`;
43
    }
44
    console.log(`knocking on: ${url}`);
45
    console.log(`sending ${JSON.stringify(newInterface)}`);
46
    return this.httpClient.post<RepositoryInterface>(url, newInterface, headerOptions);
47
  }
48

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
159

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

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

    
172

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

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

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

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

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

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

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

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

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