95eff42dfe
Co-authored-by: Lala, Shahd <Shahd.Lala@sybit.de> Co-authored-by: shahdlala66 <shahd.lala66@gmail.com> Reviewed-on: #8
53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { Injectable, inject } from '@angular/core';
|
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
|
import { Observable } from 'rxjs';
|
|
import { Tournament, TournamentList, RoundPairings } from '../models/tournament.models';
|
|
|
|
export interface CreateTournamentForm {
|
|
name: string;
|
|
nbRounds: number;
|
|
clockLimitMinutes: number;
|
|
clockIncrement: number;
|
|
rated: boolean;
|
|
}
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class TournamentService {
|
|
private readonly http = inject(HttpClient);
|
|
private readonly base = '/api/tournament';
|
|
|
|
list(): Observable<TournamentList> {
|
|
return this.http.get<TournamentList>(this.base);
|
|
}
|
|
|
|
get(id: string): Observable<Tournament> {
|
|
return this.http.get<Tournament>(`${this.base}/${id}`);
|
|
}
|
|
|
|
create(form: CreateTournamentForm): Observable<Tournament> {
|
|
const body = new URLSearchParams();
|
|
body.set('name', form.name);
|
|
body.set('nbRounds', String(form.nbRounds));
|
|
body.set('clockLimit', String(form.clockLimitMinutes * 60));
|
|
body.set('clockIncrement', String(form.clockIncrement));
|
|
body.set('rated', String(form.rated));
|
|
return this.http.post<Tournament>(this.base, body.toString(), {
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' })
|
|
});
|
|
}
|
|
|
|
start(id: string): Observable<Tournament> {
|
|
return this.http.post<Tournament>(`${this.base}/${id}/start`, null);
|
|
}
|
|
|
|
joinWithBotToken(id: string, botToken: string): Observable<void> {
|
|
return this.http.post<void>(`${this.base}/${id}/join`, null, {
|
|
headers: new HttpHeaders({ Authorization: `Bearer ${botToken}` })
|
|
});
|
|
}
|
|
|
|
roundPairings(id: string, round: number): Observable<RoundPairings> {
|
|
return this.http.get<RoundPairings>(`${this.base}/${id}/round/${round}`);
|
|
}
|
|
}
|