64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { Injectable, inject } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import {map, Observable} from 'rxjs';
|
|
import { environment } from '../../environments/environment';
|
|
import { Bet, CreateBetPayload, UpdateBetPayload } from '../models/data.models';
|
|
import {SuccessResponse} from "../models/api-responses.model";
|
|
import {toDate} from "../core/utils/type-guards";
|
|
|
|
function mapBet(bet: any): Bet {
|
|
return {
|
|
...bet,
|
|
startDate: toDate(bet.startDate)!,
|
|
endDate: toDate(bet.endDate)
|
|
};
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class BetService {
|
|
private http = inject(HttpClient);
|
|
private readonly base = `${environment.apiUrl}/api/v1/bets`;
|
|
|
|
getUserBets(): Observable<Bet[]> {
|
|
return this.http.get<SuccessResponse<Bet[]>>(`${this.base}/user`).pipe(
|
|
map(res => res.data.map(mapBet))
|
|
);
|
|
}
|
|
|
|
getPublicBets(): Observable<Bet[]> {
|
|
return this.http.get<SuccessResponse<Bet[]>>(`${this.base}/public`).pipe(
|
|
map(res => res.data.map(mapBet))
|
|
);
|
|
}
|
|
|
|
getAllBets(): Observable<Bet[]> {
|
|
return this.http.get<SuccessResponse<Bet[]>>(this.base).pipe(
|
|
map(res => res.data.map(mapBet))
|
|
);
|
|
}
|
|
|
|
getBetById(id: string): Observable<Bet> {
|
|
return this.http.get<SuccessResponse<Bet>>(`${this.base}/${id}`).pipe(
|
|
map(res => mapBet(res.data))
|
|
);
|
|
}
|
|
|
|
createBet(payload: CreateBetPayload): Observable<Bet> {
|
|
return this.http.post<SuccessResponse<Bet[]>>(this.base, payload).pipe(
|
|
map(res => mapBet(res.data))
|
|
);
|
|
}
|
|
|
|
updateBet(id: string, payload: UpdateBetPayload): Observable<Bet> {
|
|
return this.http.put<SuccessResponse<Bet[]>>(`${this.base}/${id}`, payload).pipe(
|
|
map(res => mapBet(res.data))
|
|
);
|
|
}
|
|
|
|
deleteBet(id: string): Observable<void> {
|
|
return this.http.delete<void>(`${this.base}/${id}`);
|
|
}
|
|
}
|