You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.4 KiB
81 lines
2.4 KiB
import { Injectable } from '@angular/core';
|
|
import { HttpClient } from "@angular/common/http";
|
|
import { Observable, from } from "rxjs";
|
|
|
|
import { Season } from '../models/season.model';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
|
|
export class SeasonsService {
|
|
seasons: Season[] = [];
|
|
useTestData: boolean = false;
|
|
|
|
constructor(private httpClient: HttpClient) {
|
|
}
|
|
|
|
private convertToClientSeason(data: any): Season {
|
|
let _season = new Season();
|
|
_season.id = data.id.toString();
|
|
_season.title = data.title;
|
|
_season.subTitle = data.subTitle;
|
|
_season.startingDate = new Date(data.startingDate);
|
|
_season.racers = [];
|
|
_season.races = data.races;
|
|
_season.standings = data.standings;
|
|
console.log(_season);
|
|
return _season;
|
|
}
|
|
|
|
getAllSeasons(): Observable<Array<Season>> {
|
|
if(this.useTestData) {
|
|
return this.httpClient.get<Array<Season>>("assets/Seasons.json");
|
|
}
|
|
|
|
let promise = new Promise<Array<Season>>((resolve, reject) =>{
|
|
this.httpClient.get<Array<Season>>("http://localhost:3000/seasons/").subscribe(data => {
|
|
let seasons: Season[] = [];
|
|
for(let server_season of data) {
|
|
let _season = this.convertToClientSeason(server_season);
|
|
seasons.push(_season)
|
|
console.log(_season)
|
|
console.log(server_season)
|
|
}
|
|
resolve(seasons);
|
|
//reject(undefined);
|
|
});
|
|
});
|
|
return from(promise);
|
|
}
|
|
|
|
getSeason(id: string): Observable<Season | undefined> {
|
|
if(this.useTestData) {
|
|
let promise = new Promise<Season|undefined>((resolve, reject) =>{
|
|
this.httpClient.get<Array<Season>>("assets/Seasons.json").subscribe(data => {
|
|
for(let season of data) {
|
|
if(season.id.toString() == id) {
|
|
resolve(season);
|
|
}
|
|
}
|
|
reject(undefined);
|
|
});
|
|
});
|
|
return from(promise);
|
|
}
|
|
|
|
let promise = new Promise<Season|undefined>((resolve, reject) =>{
|
|
this.httpClient.get<Array<Season>>("http://localhost:3000/seasons/"+id).subscribe(data => {
|
|
let _season = this.convertToClientSeason(data);
|
|
console.log(data);
|
|
console.log(_season);
|
|
resolve(_season);
|
|
});
|
|
});
|
|
return from(promise);
|
|
}
|
|
|
|
create(title: string, subTitle: string, startingDate: Date) {
|
|
return this.httpClient.post("http://localhost:3000/seasons/", {title, subTitle, startingDate});
|
|
}
|
|
}
|
|
|