2018-04-06 23:01:07 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
|
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router';
|
|
|
|
|
import { Observable } from 'rxjs/Observable';
|
|
|
|
|
|
|
|
|
|
import { environment } from '../../../environments/environment';
|
|
|
|
|
import { Team } from '../team';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class TeamService implements Resolve<Array<Team>> {
|
|
|
|
|
|
|
|
|
|
private apiEndPoint = environment.apiUrl + '/api/team';
|
|
|
|
|
private cachedTeams: Array<Team> = [];
|
|
|
|
|
|
|
|
|
|
constructor(private httpClient: HttpClient) { }
|
|
|
|
|
|
|
|
|
|
public resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<Array<Team>> {
|
|
|
|
|
return this.list().toPromise();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public list(): Observable<Array<Team>> {
|
|
|
|
|
return this.httpClient.get<Array<Team>>(this.apiEndPoint);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 18:17:11 +02:00
|
|
|
public persist(team: Team): Observable<Team> {
|
|
|
|
|
return team.id === null
|
|
|
|
|
? this.create(team)
|
|
|
|
|
: this.update(team);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-06 23:01:07 +02:00
|
|
|
public create(team: Team) {
|
|
|
|
|
return this.httpClient.post<Team>(this.apiEndPoint, team);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public update(team: Team) {
|
|
|
|
|
return this.httpClient.put<Team>(`${this.apiEndPoint}/${team.id.toString()}`, team);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public delete(team: Team) {
|
|
|
|
|
return this.httpClient.delete<boolean>(`${this.apiEndPoint}/${team.id.toString()}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get teams(): Array<Team> {
|
|
|
|
|
return this.cachedTeams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set teams(teams: Array<Team>) {
|
|
|
|
|
this.cachedTeams = teams;
|
|
|
|
|
}
|
|
|
|
|
}
|