66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
import { inject, Injectable } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { Observable } from 'rxjs';
|
|
import { Bet, CreateBetRequest, UpdateBetRequest } from '../../models/bet.model';
|
|
import { SuccessResponse } from '../../models/api-responses.model';
|
|
import { map } from 'rxjs/operators';
|
|
import { toDate } from '../../utils/type-guards';
|
|
|
|
function mapBet(bet: any): Bet {
|
|
return {
|
|
...bet,
|
|
startDate: toDate(bet.startDate)!,
|
|
endDate: toDate(bet.endDate)
|
|
};
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class BetApiService {
|
|
private http = inject(HttpClient);
|
|
private readonly apiUrl = 'http://localhost:3000/api/v1/bets';
|
|
|
|
getUserBets(): Observable<Bet[]> {
|
|
return this.http.get<SuccessResponse<Bet[]>>(`${this.apiUrl}/user`).pipe(
|
|
map(res => res.data.map(mapBet))
|
|
);
|
|
}
|
|
|
|
getPublicBets(): Observable<Bet[]> {
|
|
return this.http.get<SuccessResponse<Bet[]>>(`${this.apiUrl}/public`).pipe(
|
|
map(res => res.data.map(mapBet))
|
|
);
|
|
}
|
|
|
|
getBets(): Observable<Bet[]> {
|
|
return this.http.get<SuccessResponse<Bet[]>>(this.apiUrl).pipe(
|
|
map(res => res.data.map(mapBet))
|
|
);
|
|
}
|
|
|
|
getBet(id: string): Observable<Bet> {
|
|
return this.http.get<SuccessResponse<Bet>>(`${this.apiUrl}/${id}`).pipe(
|
|
map(res => mapBet(res.data))
|
|
);
|
|
}
|
|
|
|
createBet(bet: CreateBetRequest): Observable<Bet> {
|
|
return this.http.post<SuccessResponse<Bet>>(this.apiUrl, bet).pipe(
|
|
map(res => mapBet(res.data))
|
|
);
|
|
}
|
|
|
|
updateBet(id: string, bet: UpdateBetRequest): Observable<Bet> {
|
|
return this.http.put<SuccessResponse<Bet>>(`${this.apiUrl}/${id}`, bet).pipe(
|
|
map(res => mapBet(res.data))
|
|
);
|
|
}
|
|
|
|
deleteBet(id: string): Observable<void> {
|
|
return this.http.delete<SuccessResponse<null>>(`${this.apiUrl}/${id}`).pipe(
|
|
map(() => undefined)
|
|
);
|
|
}
|
|
}
|