mrv777

tests.service.ts

Mar 17th, 2017
45
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http'
  3. import { Observable } from 'rxjs/Rx'
  4. import { Test } from '../models/test'
  5. import { AuthenticationService } from './authentication.service';
  6.  
  7. // Import RxJs required methods
  8. import 'rxjs/add/operator/map';
  9. import 'rxjs/add/operator/catch';
  10.  
  11. @Injectable()
  12. export class TestsService {
  13.  
  14.   constructor(
  15.     private http: Http,
  16.     private authenticationService: AuthenticationService) { }
  17.  
  18.   // Fetch student's test information
  19.   getStudentTestScores(studentId) : Observable<Test[]> {
  20.     const headers = new Headers({ 'Authorization': 'Bearer ' + this.authenticationService.token });
  21.     const options = new RequestOptions({
  22.         headers: headers,
  23.     });
  24.  
  25.     return this.http.get(`http://SERVER:3000/api/tests/student/${studentId}`, options)
  26.         .map((res:Response) => res.json())
  27.         .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
  28.  
  29.   }
  30.  
  31.   // Fetch all tests
  32.   getTestScores(queryObject) : Observable<Test[]> {
  33.     let params: URLSearchParams = new URLSearchParams();
  34.     for(var queries = 0; queries < queryObject.length; queries++){
  35.       Object.keys(queryObject[queries]).forEach(key => {
  36.         params.set(key, queryObject[queries][key]);
  37.       });
  38.     }
  39.     const headers = new Headers({ 'Authorization': 'Bearer ' + this.authenticationService.token });
  40.     const options = new RequestOptions({
  41.       headers: headers,
  42.       search: params,
  43.     });
  44.  
  45.     return this.http.get(`http://SERVER:3000/api/tests`, options)
  46.       .map((res:Response) => res.json())
  47.       .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
  48.  
  49.   }
  50.  
  51. }
RAW Paste Data