Commit 52beebe2 authored by mdsecurity's avatar mdsecurity

isOpen method finished in the place Class

parent 74f01d8f
# WhatsOpenA2
This project was generated with [angular-cli](https://github.com/angular/angular-cli) version 1.0.0-beta.26.
This project was generated with [angular-cli](https://github.com/angular/angular-cli) version 1.0.0-beta.28.3.
## Development server
Run `ng serve` for a dev server. Navigate to [http://localhost:4200/](https://github.com/angular/angular-cli). The app will automatically reload if you change any of the source files.
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
## Code scaffolding
......
<div class="context-bar-button-container">
<!--<button class="show-hide-button"(click)="show=!show">Click Me</button>-->
<app-show-hide-arrow class="show-hide-button" [isRight]='true' (change)="show = $event"></app-show-hide-arrow>
<button (click)="tempf()">Click</button>
<div [ngClass]="(show)?'light-card context-bar-open' : 'light-card context-bar-closed'">
<div class="context-bar-header">
......
import { Component, OnInit} from '@angular/core';
import {DataProviderService} from '../data-provider.service'
import { Component, OnInit } from '@angular/core';
import { DataProviderService } from '../data-provider.service'
import { Place } from '../place';
@Component({
selector: 'app-context-bar',
templateUrl: './context-bar.component.html',
styleUrls: ['./context-bar.component.scss'],
providers:[DataProviderService]
providers: [DataProviderService]
})
export class ContextBarComponent implements OnInit {
show: boolean = true;
data:Place[];
error:any;
constructor(private dataProvider:DataProviderService) { }
data: Place[];
error: any;
constructor(private dataProvider: DataProviderService) { }
ngOnInit() {
}
this.dataProvider.getFacilities().subscribe(
data => { this.data = data },
error => { this.error = error }
)
}
tempf = function(){
for(let i = 0; i<this.data.length;i++){
console.log(this.data[i].name + ' OPEN: '+ this.data[i].isOpen())
}
}
}
......@@ -5,47 +5,52 @@ import { Observable } from 'rxjs/Observable';
import { Place } from './place';
import { Day } from './day';
import { Time } from './time';
// Operators
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
//import 'rxjs/add/operator/debounceTime';
//mport 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/toPromise';
//import 'rxjs/add/operator/switchMap';
//import 'rxjs/add/operator/toPromise';
@Injectable()
export class DataProviderService {
// facilities: Observable<Place[]>;
error: any;
private Url = 'https://whatsopen.gmu.edu/api/facilities/'; // URL to web API
constructor(private http: Http) { }
getApiData(): Observable<Place[]> {
getFacilities(): Observable<Place[]> {
return this.http.get(this.Url)
.map(this.extractData)
.catch(this.handleError);
}
extractData(res:Response):Place[] {
// TODO
// there is a little bit of a hack here that checks if the place is 24Hours
// will remove later as the api changes
private extractData(res: Response): Place[] {
let places: Place[] = [];
let data = res.json();
for (let i = 0; i < data.length; i++) {
let main_schedule_times: Day[] = [];
for (let e = 0; e < data[i].main_schedule.open_times.length; e++) {
let jsonDay = data[i].main_schedule.open_times[e];
main_schedule_times.push(new Day(
// hack is here
if(jsonDay.end_time === '00:00:00'){ jsonDay.end_time = '23:59:59';};
const day = new Day(
jsonDay.id,
jsonDay.last_modified,
jsonDay.schedule,
jsonDay.start_day,
jsonDay.start_time,
new Time(jsonDay.start_time),
jsonDay.end_day,
jsonDay.end_time));
new Time(jsonDay.end_time));
main_schedule_times.push(Object.freeze(day));
}
places.push(new Place(
main_schedule_times,
[],
......
import { Time } from './time';
export class Day {
id: number;
last_modified: string;
schedule:number;
start_day: number;
start_time: string;
start_time: Time;
end_day: number;
end_time: string;
constructor(id: number, last_modified: string,schedule:number, start_day: number, start_time: string, end_day: number, end_time: string) {
end_time: Time;
constructor(id: number, last_modified: string,schedule:number, start_day: number, start_time: Time, end_day: number, end_time: Time) {
this.id = id;
this.last_modified = last_modified;
this.schedule = schedule;
......
......@@ -16,6 +16,41 @@ export class Place {
this.category = category;
this.location = location;
}
// This method does NOT work with Special schedules
isOpen():boolean{
const currTime = new Date();
const inSeconds = currTime.getHours()*60*60+currTime.getMinutes()*60+currTime.getSeconds();
const today = currTime.getDay() - 1;
const openDays:Day[] = this.onDay(today);
for(let i = 0;i<openDays.length;i++){
let day = openDays[i];
if(day.start_day === today && inSeconds < day.start_time.inSeconds()){
return true;
}
if(day.end_day === today && inSeconds < day.end_time.inSeconds()){
return true;
}
}
return false;
}
// returns index(s) Days that have an end day or start day on a an arg day of the week
private onDay(dayOfWeek:number):Day[]{
let days:Day[] = [];
const currTime = new Date();
const today = currTime.getDay() - 1;
for(let i = 0;i<this.main_schedule_times.length;i++){
let day = this.main_schedule_times[i];
if(day.start_day === today){
days.push(day);
}
if(day.end_day === today){
days.push(day);
}
}
return days;
}
}
export class Time {
hour: number;
minute: number;
second: number;
constructor(time: string) {
const timeArr = time.split(':');
this.hour = Number(timeArr[0]);
this.minute = Number(timeArr[1]);
this.second = Number(timeArr[2]);
}
isGreater(time: Time):boolean {
if (this.hour > time.hour) {
return true;
} else {
if (this.minute > time.minute) {
return true;
} else {
if (this.second > time.second) {
return true;
}
}
}
return false;
}
inSeconds(){
return this.hour*60*60 + this.minute*60+this.second;
}
}
// This file includes polyfills needed by Angular and is loaded before
// the app. You can add your own extra polyfills to this file.
// This file includes polyfills needed by Angular and is loaded before the app.
// You can add your own extra polyfills to this file.
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
......@@ -17,3 +17,22 @@ import 'core-js/es6/reflect';
import 'core-js/es7/reflect';
import 'zone.js/dist/zone';
// If you need to support the browsers/features below, uncomment the import
// and run `npm install import-name-here';
// Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html
// Needed for: IE9
// import 'classlist.js';
// Animations
// Needed for: All but Chrome and Firefox, Not supported in IE9
// import 'web-animations-js';
// Date, currency, decimal and percent pipes
// Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10
// import 'intl';
// NgClass on SVG elements
// Needed for: IE10, IE11
// import 'classlist.js';
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import './polyfills.ts';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment