Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http'
- import { Observable } from 'rxjs/Rx'
- import { Test } from '../models/test'
- import { AuthenticationService } from './authentication.service';
- // Import RxJs required methods
- import 'rxjs/add/operator/map';
- import 'rxjs/add/operator/catch';
- @Injectable()
- export class TestsService {
- constructor(
- private http: Http,
- private authenticationService: AuthenticationService) { }
- // Fetch student's test information
- getStudentTestScores(studentId) : Observable<Test[]> {
- const headers = new Headers({ 'Authorization': 'Bearer ' + this.authenticationService.token });
- const options = new RequestOptions({
- headers: headers,
- });
- return this.http.get(`http://SERVER:3000/api/tests/student/${studentId}`, options)
- .map((res:Response) => res.json())
- .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
- }
- // Fetch all tests
- getTestScores(queryObject) : Observable<Test[]> {
- let params: URLSearchParams = new URLSearchParams();
- for(var queries = 0; queries < queryObject.length; queries++){
- Object.keys(queryObject[queries]).forEach(key => {
- params.set(key, queryObject[queries][key]);
- });
- }
- const headers = new Headers({ 'Authorization': 'Bearer ' + this.authenticationService.token });
- const options = new RequestOptions({
- headers: headers,
- search: params,
- });
- return this.http.get(`http://SERVER:3000/api/tests`, options)
- .map((res:Response) => res.json())
- .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement