import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { Match } from '../models/match/match.model'; import { SPlayer } from '../models/s-player/splayer.model'; import { NgbToast, NgbToastService, NgbToastType } from 'ngb-toast'; import { Auth } from '../models/auth/auth.model'; @Injectable({ providedIn: 'root' }) export class SavePlayerService { //endpoint: string = 'https://ryon.ee-cognizantacademy.com'; //endpoint: string = 'http://ryonbackend-env.eba-pbjsc7zw.us-east-2.elasticbeanstalk.com'; //endpoint: string = 'http://localhost:8000'; //endpoint: string = 'http://localhost:3306'; endpoint: string = 'https://dota2-protractor-backend.de'; postHeader = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; constructor(private http: HttpClient, private toastService: NgbToastService) { } addAuth(auth: Auth): boolean{ this.http.post(this.endpoint+"/auth", auth, this.postHeader).subscribe(res => { }, (err) => { console.log(err); this.showFailure(); }); this.showPlayerAddSuccess(); return true; } addPlayer(player: SPlayer): boolean{ this.http.post(this.endpoint+"/player", player, this.postHeader).subscribe(res => { }, (err) => { console.log(err); this.showFailure(); }); this.showPlayerAddSuccess(); return true; } updatePlayer(player: SPlayer): boolean{ this.http.put(this.endpoint+"/player", player, this.postHeader).subscribe(res => { }, (err) => { console.log(err); this.showFailure(); }); return true; } deletePlayer(player: SPlayer): boolean{ this.http.delete(this.endpoint+"/player/", player.id) .subscribe(res => { }, (err) => { console.log(err); this.showFailure(); }); return true; } addMatch(match: Match, player: SPlayer): boolean{ this.http.post(this.endpoint+"/match", match, this.postHeader).subscribe(res => {this.addPlayer(player) }, (err) => { console.log(err); this.showFailure(); }); this.showMatchAddSuccess(); return true; } updateMatch(match: Match, player: SPlayer): boolean{ this.http.put(this.endpoint+"/match", match, this.postHeader).subscribe(res => {this.updatePlayer(player) }, (err) => { console.log(err); this.showFailure(); }); return true; } deleteMatch(match: Match): boolean{ this.http.delete(this.endpoint+"/match/", match.id) .subscribe(res => { }, (err) => { console.log(err); this.showFailure(); }); return true; } getAuth(id: any): Observable{ return this.http.get(`${this.endpoint}/auth/id/${id}`); } getAllSavedPlayers(): Observable{ return this.http.get(`${this.endpoint}/players`); } getSavedPlayerById(id: any): Observable{ return this.http.get(`${this.endpoint}/player/id/${id}`); } getSavedPlayerByName(name: any): Observable{ return this.http.get(`${this.endpoint}/player/name/${name}`); } getSavedMatch(id:any): Observable{ return this.http.get(`${this.endpoint}/match/id/${id}`); } getAllSavedMatches(): Observable{ return this.http.get(`${this.endpoint}/matches`); } showMatchAddSuccess(): void { const toast: NgbToast = { toastType: NgbToastType.Success, text: "Match added successfullly", dismissible: true, timeInSeconds: 5, onDismiss: () => { console.log("Toast dismissed!!"); } } this.toastService.show(toast); } showPlayerAddSuccess(): void { const toast: NgbToast = { toastType: NgbToastType.Success, text: "Player added successfullly", dismissible: true, timeInSeconds: 5, onDismiss: () => { console.log("Toast dismissed!!"); } } this.toastService.show(toast); } showFailure(): void { const toast: NgbToast = { toastType: NgbToastType.Danger, text: "Failure", dismissible: true, timeInSeconds: 5, onDismiss: () => { console.log("Toast dismissed!!"); } } this.toastService.show(toast); } removeToast(toast: NgbToast): void { this.toastService.remove(toast); } }