data-provider.service.ts 4.09 KB
Newer Older
1
2
3
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
mdsecurity's avatar
mdsecurity committed
4
import { Subject } from 'rxjs/Subject';
5
6
7

import { Place } from './place';
import { Day } from './day';
8
import { Time } from './time';
mdsecurity's avatar
mdsecurity committed
9
import { SpecialSchedule } from './special-schedule';
10
11
12
// Operators
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
mdsecurity's avatar
mdsecurity committed
13
14
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/share';
15
import 'rxjs/add/operator/map';
mdsecurity's avatar
mdsecurity committed
16
import 'rxjs/add/operator/DefaultIfEmpty';
17
18
19

@Injectable()
export class DataProviderService {
mdsecurity's avatar
mdsecurity committed
20
21
22
23
24
25
	// facilities: Observable<Place[]>;
	private contextPlace: Place = new Place();
	places: Place[];
	placesObs: Observable<any>;
	private contextSubj: Subject<Place> = new Subject<Place>();
	private Url = 'https://whatsopen.gmu.edu/api/facilities/';  // URL to web API
26

mdsecurity's avatar
mdsecurity committed
27
	constructor(private http: Http) { }
mdsecurity's avatar
mdsecurity committed
28

mdsecurity's avatar
mdsecurity committed
29
	getFacilities(): Observable<Place[]> {
mdsecurity's avatar
mdsecurity committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
		// if (this.places) {
		// 	return Observable.of(this.places);
		// } else if (this.placesObs) {
		// 	return this.placesObs;
		// } else {
		// 	const facilities = localStorage.getItem('facilities');
		// 	if (facilities) {
		// 		this.places = this.extractData(JSON.parse(facilities));
		// 		this.placesObs = Observable.of(this.places);
		// 		return this.placesObs;
		// 	} else {

		this.placesObs = this.http.get(this.Url)
			.map((data) => {
				this.placesObs = null;
				data = data.json();
				this.places = this.extractData(data);
				this.contextPlace = this.places[0];
				localStorage.setItem('facilities', JSON.stringify(data));
				return this.places;
			})
			.catch(this.handleError).share();
		return this.placesObs;
		// 	}
		// }
mdsecurity's avatar
mdsecurity committed
55
56
57
58
59
60
61
62
	}
	getContext(): Observable<Place> {
		return this.contextSubj.asObservable();
	}
	setContext(place: Place): void {
		this.contextPlace = place;
		this.contextSubj.next(place);
	}
mdsecurity's avatar
mdsecurity committed
63
	private extractData(data: any): Place[] {
mdsecurity's avatar
mdsecurity committed
64
65
66
67
68
69
70
71
		const parseTime = function (time: string): Time {
			const timeArr = time.split(':');
			const hour = Number(timeArr[0]);
			const minute = Number(timeArr[1]);
			const second = Number(timeArr[2]);
			return new Time(hour, minute, second);
		}
		const places: Place[] = [];
mdsecurity's avatar
mdsecurity committed
72
		// const data = res.json();
mdsecurity's avatar
mdsecurity committed
73
		for (let i = 0; i < data.length; i++) {
mdsecurity's avatar
mdsecurity committed
74
75
			let main_schedule_times: Day[] = [];
			let special_schedules: SpecialSchedule[] = [];
mdsecurity's avatar
mdsecurity committed
76
77
78
79
80
81
82
83
84
85
86
87
88
			for (let e = 0; e < data[i].main_schedule.open_times.length; e++) {
				let jsonDay = data[i].main_schedule.open_times[e];

				const day = new Day(
					jsonDay.id,
					jsonDay.last_modified,
					jsonDay.schedule,
					jsonDay.start_day,
					parseTime(jsonDay.start_time),
					jsonDay.end_day,
					parseTime(jsonDay.end_time));
				main_schedule_times.push(Object.freeze(day));
			}
mdsecurity's avatar
mdsecurity committed
89
90
			for (let e = 0; e < data[i].special_schedules.length; e++) {
				const jsonSchedule = data[i].special_schedules[e];
91
92
				let open_times: Day[] = [];
				for (let f = 0; f < jsonSchedule.open_times.length; f++) {
mdsecurity's avatar
mdsecurity committed
93
					let jsonDay = jsonSchedule.open_times[f];
94

mdsecurity's avatar
mdsecurity committed
95
					const day = new Day(
96
97
98
99
100
101
102
						jsonDay.id,
						jsonDay.last_modified,
						jsonDay.schedule,
						jsonDay.start_day,
						parseTime(jsonDay.start_time),
						jsonDay.end_day,
						parseTime(jsonDay.end_time));
mdsecurity's avatar
mdsecurity committed
103
104
					open_times.push(Object.freeze(day));
				}
105
				const schedule = new SpecialSchedule(jsonSchedule.valid_start, jsonSchedule.valid_end, open_times);
mdsecurity's avatar
mdsecurity committed
106
107
				special_schedules.push(Object.freeze(schedule));
			}
mdsecurity's avatar
mdsecurity committed
108
			// console.log(special_schedules);
mdsecurity's avatar
mdsecurity committed
109
110
			places.push(new Place(
				main_schedule_times,
111
				special_schedules,
mdsecurity's avatar
mdsecurity committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
				data[i].id,
				data[i].last_modified,
				data[i].name,
				data[i].category,
				data[i].location
			));
		}
		return places;
	}
	private handleError(error: Response | any) {
		// In a real world app, we might use a remote logging infrastructure
		let errMsg: string;
		if (error instanceof Response) {
			const body = error.json() || '';
			const err = body.error || JSON.stringify(body);
			errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
		} else {
			errMsg = error.message ? error.message : error.toString();
		}
		console.error(errMsg);
		return Observable.throw(errMsg);
	}
mdsecurity's avatar
mdsecurity committed
134

135

136
137

}