-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#705 rewrite team and user service to return a subject and update thi…
…s when necessary
- Loading branch information
1 parent
6ea9160
commit 746c5bf
Showing
7 changed files
with
97 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,46 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Team } from '../shared/types/model/Team'; | ||
import { Observable, of, take, tap } from 'rxjs'; | ||
import { BehaviorSubject, Observable, tap } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class TeamService { | ||
constructor(private http: HttpClient) {} | ||
|
||
private teams: Team[] | undefined; | ||
private teams: BehaviorSubject<Team[]> = new BehaviorSubject<Team[]>([]); | ||
private teamsLoaded = false; | ||
|
||
getAllTeams(): Observable<Team[]> { | ||
if (this.teams) { | ||
return of(this.teams).pipe(take(1)); | ||
if (!this.teamsLoaded) { | ||
this.reloadTeams().subscribe(); | ||
this.teamsLoaded = true; | ||
} | ||
return this.http.get<Team[]>('/api/v2/teams').pipe(tap((teams) => (this.teams = teams))); | ||
return this.teams.asObservable(); | ||
} | ||
|
||
reloadTeams(): Observable<Team[]> { | ||
this.teams = undefined; | ||
return this.getAllTeams(); | ||
return this.http.get<Team[]>('/api/v2/teams').pipe( | ||
tap((teams) => { | ||
if (!this.teams) { | ||
this.teams = new BehaviorSubject<Team[]>(teams); | ||
return; | ||
} | ||
this.teams.next(teams); | ||
}), | ||
); | ||
} | ||
|
||
createTeam(team: Team): Observable<Team> { | ||
return this.http.post<Team>('/api/v2/teams', team); | ||
return this.http.post<Team>('/api/v2/teams', team).pipe(tap(() => this.reloadTeams().subscribe())); | ||
} | ||
|
||
updateTeam(team: Team): Observable<Team> { | ||
return this.http.put<Team>(`/api/v2/teams/${team.id}`, team); | ||
return this.http.put<Team>(`/api/v2/teams/${team.id}`, team).pipe(tap(() => this.reloadTeams().subscribe())); | ||
} | ||
|
||
deleteTeam(id: number): Observable<any> { | ||
return this.http.delete(`/api/v2/teams/${id}`); | ||
return this.http.delete(`/api/v2/teams/${id}`).pipe(tap(() => this.reloadTeams().subscribe())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters