Commit bd97e70d authored by Ján Dovjak's avatar Ján Dovjak
Browse files

feat: playerService connected to API

parent c971044b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ export class RecruitVeteranPlayerFormComponent implements OnInit {

  confirmSubmission() : void {
    this.confirmed = true;
    this.playerService.recruitVeteranPlayer(this.model.player!, this.model.team);
    this.playerService.recruitVeteranPlayer(this.model.player!, this.team);
    this.formSubmitEvent.emit(null);
  }
}
+4 −0
Original line number Diff line number Diff line
export interface RecruitNewPlayer {
    name: string;
    teamId: number;
}
 No newline at end of file
+4 −0
Original line number Diff line number Diff line
export interface RecruitVeteran {
    playerId: number;
    teamId: number;
}
 No newline at end of file
+19 −26
Original line number Diff line number Diff line
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Observable, of } from 'rxjs';
import { CreatePlayer } from '../models/create-player';
import { Player } from '../models/player';
import { Team } from '../models/team';
import { RecruitNewPlayer } from '../models/recruit-new-player';
import { RecruitVeteran } from '../models/recruit-veteran';

@Injectable({
  providedIn: 'root'
@@ -29,48 +33,37 @@ export class PlayerService {
    {id: 2, name: "Alexander Ovechkin"},
  ];

  constructor() { }
  getAllFreeUrl : string = 'http://localhost:8080/pa165/rest/player/free';
  getForTeamUrl : string = 'http://localhost:8080/pa165/rest/player/team/'
  createNewUrl : string = 'http://localhost:8080/pa165/rest/player/recruitNew';
  recruitUrl : string = 'http://localhost:8080/pa165/rest/player/transferPlayer';
  fireUrl : string = 'http://localhost:8080/pa165/rest/player/fire/';

  constructor(private http: HttpClient) { }

  getPlayers() : Observable<Player[]> {
    return of(this.players);
    return this.http.get<Player[]>(this.getAllFreeUrl);
  }

  getFreePlayers() : Observable<Player[]> {
    return of(this.freePlayers);
    return this.http.get<Player[]>(this.getAllFreeUrl);
  }

  getPlayersForTeam(id: number) : Observable<Player[]> {
    if (id === 1) {
      return of(this.teamOnePlayers);
    } else {
      return of([]);
    }
    return this.http.get<Player[]>(this.getForTeamUrl + id);
  }

  recruitPlayer(player: CreatePlayer, team: Team) : void {
    if(team.id == 1) {
      this.teamOnePlayers.push({
        name: player.name,
        id: Math.random()
      });
    }
    let newPlayer : RecruitNewPlayer = { name: player.name, teamId: team.id};
    this.http.post<RecruitNewPlayer>(this.createNewUrl, newPlayer).subscribe();
  }

  recruitVeteranPlayer(player: Player, team: Team) : void {
    if(team.id == 1) {
      this.teamOnePlayers.push(player);
      this.freePlayers = this.freePlayers.filter((val, i, array) => val.id != player.id);
    }
    let newPlayer : RecruitVeteran = { playerId: player.id, teamId: team.id};
    this.http.post<RecruitVeteran>(this.recruitUrl, newPlayer).subscribe();
  }

  firePlayer(id: number): void {
    let player: Player|undefined = this.teamOnePlayers.find((val) => val.id == id);
    if ( player != undefined ) {
      this.freePlayers.push(
        player
      )
    }
    this.teamOnePlayers = this.teamOnePlayers
      .filter((val, i, array) => val.id != id);
    this.http.delete(this.fireUrl + id).subscribe();
  }
}
+7 −5
Original line number Diff line number Diff line
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';

import { Observable, of } from 'rxjs';
import { AccessEnum } from '../models/access-enum';
@@ -18,15 +18,17 @@ export class TeamService {
    {id: 2, name: 'Boston Bruins'}
  ];

  constructor(
    private http: HttpClient) { }
  getAllUrl : string = 'http://localhost:8080/pa165/rest/team/all';
  createUrl : string = 'http://localhost:8080/pa165/rest/team/create';

  constructor(private http: HttpClient) { }

  getTeams(): Observable<Team[]> {
    return this.http.get<Team[]>('http://localhost:8080/pa165/rest/team/all');
    return this.http.get<Team[]>(this.getAllUrl);
  }

  createTeam(team: CreateTeam) : void {
    this.http.post<Team>('http://localhost:8080/pa165/rest/team/create', team)
    this.http.post<Team>(this.createUrl, team)
    .subscribe();
  }