Initial commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
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';
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user