From 3be83bf72534db9899ec525d3a6da073d4822278 Mon Sep 17 00:00:00 2001 From: Joachim Date: Sat, 1 Sep 2018 12:50:37 +0200 Subject: [PATCH] got championships load and current champ working --- src/app/championship.service.spec.ts | 15 ++++++ src/app/championship.service.ts | 68 ++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/app/championship.service.spec.ts create mode 100644 src/app/championship.service.ts diff --git a/src/app/championship.service.spec.ts b/src/app/championship.service.spec.ts new file mode 100644 index 0000000..a05ad75 --- /dev/null +++ b/src/app/championship.service.spec.ts @@ -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(); + })); +}); diff --git a/src/app/championship.service.ts b/src/app/championship.service.ts new file mode 100644 index 0000000..a07ea69 --- /dev/null +++ b/src/app/championship.service.ts @@ -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(null); + + + constructor(private http: HttpClient, + private route: ActivatedRoute) { + + // load the champs from the DB + this.loadChampionships(); + + } + + private loadChampionships() { + this.http.get(`${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 { + } +}