got championships load and current champ working

This commit is contained in:
Joachim 2018-09-01 12:50:37 +02:00
parent 310e1ccd2d
commit 3be83bf725
2 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { ChampionshipService } from './championship.service';
describe('ChampionshipService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ChampionshipService]
});
});
it('should be created', inject([ChampionshipService], (service: ChampionshipService) => {
expect(service).toBeTruthy();
}));
});

View File

@ -0,0 +1,68 @@
import {Injectable, OnInit} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {ActivatedRoute} from '@angular/router';
import {BehaviorSubject} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ChampionshipService implements OnInit {
protected httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
}),
withCredentials: true
};
protected baseUrl = 'http://localhost:8080/';
private championships: any[] = [];
/**
* What's the current championship? Could be null if not loaded yet.
*/
private idChampionship = new BehaviorSubject<number>(null);
constructor(private http: HttpClient,
private route: ActivatedRoute) {
// load the champs from the DB
this.loadChampionships();
}
private loadChampionships() {
this.http.get<any[]>(`${this.baseUrl}championship`, this.httpOptions).subscribe(val => {
console.log('loaded championships');
this.championships = val;
console.log(val);
});
}
/**
* Set the currently visible championship.
* @param idChampionship
*/
setChampionship(idChampionship: number) {
this.idChampionship.next(idChampionship);
}
getChampionships() {
return this.championships;
}
getCurrentChampionship() {
console.log(this.championships);
const theOne = this.championships.filter(val => val.idchampionship === this.idChampionship.value);
console.log(theOne);
if (theOne && theOne.length > 0) {
return theOne[0];
} else {
return null;
}
}
ngOnInit(): void {
}
}