Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Jsonp, URLSearchParams,ResponseOptions, RequestOptions, Headers} from '@angular/http';
4
import {Observable}     from 'rxjs/Observable';
5
import 'rxjs/add/operator/do';
6

    
7
import { CustomOptions } from '../../services/servicesUtils/customOptions.class';
8

    
9
@Injectable()
10
export class ClaimsByTokenService {
11

    
12
    constructor(private http: Http ) {}
13

    
14
    getClaims(openaireId: string,  apiURL:string):any {
15
        console.info("getClaims in service");
16

    
17
        //let url = apiURL+"project/claims?projectToken="+token;
18
        let url = apiURL+"projects/"+openaireId+"/all_claims";
19

    
20
        let key = url;
21

    
22
        return this.http.get(url, CustomOptions.getAuthOptions())
23
                    //.map(res => <any> res.text())
24
                    .map(request => <any> request.json());
25

    
26
    }
27

    
28

    
29
/*
30
    getClaims(email: string, token: string, user_token: string):any {
31
      let url = OpenaireProperties.getClaimsAPIURL();   // What else?
32
      let body = JSON.stringify( {"email": email, "token": token} );
33
      console.warn('Json body:  : '+body);
34
      let headers = new Headers({ 'Content-Type': 'application/json' });
35
      let options = new RequestOptions({ headers: headers });
36
      return this.http.post(url, body, options)
37
                            .map(res => res.json())
38
                            .do(request => console.info("Insert Response:"+request.status) )
39
                            .catch(this.handleError);
40
    }
41
*/
42

    
43
  updateClaimsCuration( selectedRight: Set<string>, selectedWrong: Set<string>, apiURL:string) {
44
    let url = apiURL + "curate/bulk";
45
    let claimsCurationInfo: any = []; //e.g.:  [{"id":"2","approved":true},{"id":"1","approved":true}]
46

    
47
    selectedRight.forEach(function(selected) {
48
      //console.info(selected);
49
      let claimCurationInfo: {"id": string, "approved": boolean} = {"id": selected, "approved": true};
50
      claimsCurationInfo.push(claimCurationInfo);
51
    });
52

    
53
    selectedWrong.forEach(function(selected) {
54
      let claimCurationInfo: any = {"id": selected, "approved": false};
55
      claimsCurationInfo.push(claimCurationInfo);
56
    });
57

    
58
    console.info(claimsCurationInfo);
59

    
60

    
61
    let body = JSON.stringify( claimsCurationInfo );
62
    console.warn('Json body:  : '+body);
63
    let headers = new Headers({ 'Content-Type': 'application/json' });
64
    let options = new RequestOptions({ headers: headers });
65
    return this.http.post(url, body,  CustomOptions.getAuthOptionsWithBody())
66
                          .map(res => res.json())
67
                          .do(request => console.info("Insert Response:"+request.status) )
68
                          .catch(this.handleError);
69
  }
70

    
71

    
72
    updateClaimCuration( claimCurationInfo: {"id": string, "approved": boolean}, apiURL:string) {
73
      let url = apiURL + "curate/bulk";
74
      let claimsCurationInfo: any = []; //e.g.:  [{"id":"2","approved":true},{"id":"1","approved":true}]
75
      claimsCurationInfo.push(claimCurationInfo);
76

    
77
      console.info(claimsCurationInfo);
78

    
79
      let body = JSON.stringify( claimsCurationInfo );
80
      console.warn('Json body:  : '+body);
81
      let headers = new Headers({ 'Content-Type': 'application/json' });
82
      let options = new RequestOptions({ headers: headers });
83
      return this.http.post(url, body,  CustomOptions.getAuthOptionsWithBody())
84
                            .map(res => res.json())
85
                            .do(request => console.info("Insert Response:"+request.status) )
86
                            .catch(this.handleError);
87
    }
88

    
89
  private handleError (error: Response) {
90
    // in a real world app, we may send the error to some remote logging infrastructure
91
    // instead of just logging it to the console
92
    console.log(error);
93
    return Observable.throw(error  || 'Server error');
94
  }
95
}
(4-4/4)