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 '../claim-utils/service/customOptions.class';
8

    
9
@Injectable()
10
export class ClaimsByTokenService {
11

    
12
    constructor(private http: Http ) {}
13

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

    
17
        let url = apiURL+"project/claims?projectToken="+token;
18

    
19
        let key = url;
20

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

    
25
    }
26

    
27

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

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

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

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

    
57
    console.info("\n\n"+claimsCurationInfo);
58

    
59

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

    
70
  private handleError (error: Response) {
71
    // in a real world app, we may send the error to some remote logging infrastructure
72
    // instead of just logging it to the console
73
    console.log(error);
74
    return Observable.throw(error  || 'Server error');
75
  }
76
}
(4-4/4)