-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user fetch for a night and a refuge
- Loading branch information
1 parent
97e3f23
commit 43a3466
Showing
7 changed files
with
196 additions
and
12 deletions.
There are no files selected for viewing
24 changes: 12 additions & 12 deletions
24
app/src/app/pages/refuges/refuge-detail/refuge-detail.page.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<ion-header [translucent]="true"> | ||
<ion-toolbar> | ||
<ion-title>refuge-detail</ion-title> | ||
</ion-toolbar> | ||
</ion-header> | ||
|
||
<ion-content [fullscreen]="true"> | ||
<ion-header collapse="condense"> | ||
<ion-toolbar> | ||
<ion-title size="large">refuge-detail</ion-title> | ||
</ion-toolbar> | ||
</ion-header> | ||
<ion-content> | ||
<ion-grid> | ||
<ion-col> | ||
<ion-datetime presentation="date" [(ngModel)]="pickedDate"></ion-datetime> | ||
</ion-col> | ||
<ion-col> | ||
<ion-button (click)="onSearch()" style="margin-left: 5%"> | ||
Search | ||
<ion-icon style="margin-left: 5%" name="folder"></ion-icon> | ||
</ion-button> | ||
</ion-col> | ||
</ion-grid> | ||
</ion-content> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
app/src/app/services/reservations/reservation.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { ReservationService } from './reservation.service'; | ||
|
||
describe('ReservationService', () => { | ||
let service: ReservationService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(ReservationService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { isValidNight, Night } from '../../../schemas/reservations/night'; | ||
import { catchError, map, Observable, ObservableInput, of, retry } from 'rxjs'; | ||
import { | ||
GetReservationsErrors, | ||
GetReservationsResponse, | ||
} from '../../../schemas/reservations/get-reservations-schema'; | ||
import { environment } from '../../../environments/environment'; | ||
import { HttpClient, HttpErrorResponse } from '@angular/common/http'; | ||
import { | ||
Reservation, | ||
ReservationPattern, | ||
} from '../../../schemas/reservations/reservation'; | ||
import { isMatching } from 'ts-pattern'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class ReservationService { | ||
constructor(private http: HttpClient) {} | ||
|
||
getReservationsFrom(night: Night): Observable<GetReservationsResponse> { | ||
if (!isValidNight(night)) | ||
return of({ | ||
status: 'error', | ||
error: GetReservationsErrors.CLIENT_SEND_DATA_ERROR, | ||
}); | ||
return this.getReservationsFromApi(night); | ||
} | ||
|
||
getReservationsFromApi(night: Night): Observable<GetReservationsResponse> { | ||
const endpoint = this.getReservationEndpoint(night); | ||
return this.http.get<Reservation[]>(endpoint).pipe( | ||
map<Reservation[], GetReservationsResponse | Error>( | ||
(reservations: Reservation[]) => { | ||
if (isMatching(ReservationPattern, reservations.values())) | ||
return { status: 'correct', data: reservations }; | ||
return { | ||
status: 'error', | ||
error: GetReservationsErrors.SERVER_INCORRECT_DATA_FORMAT_ERROR, | ||
}; | ||
}, | ||
), | ||
catchError<GetReservationsResponse | Error, ObservableInput<any>>( | ||
(err: HttpErrorResponse) => | ||
of({ | ||
status: 'error', | ||
error: GetReservationsErrors.from(err), | ||
}), | ||
), | ||
retry(3), | ||
); | ||
} | ||
|
||
private getReservationEndpoint(night: Night): string { | ||
return `${environment.API}/reservations/refuge/${night.refugeId}/year/${night.year}/month/${night.month}/day/${night.day}/`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Reservation } from './reservation'; | ||
import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http'; | ||
import { match } from 'ts-pattern'; | ||
|
||
export enum GetReservationsErrors { | ||
UNAUTHORIZED = 'UNAUTHORIZED', | ||
FORBIDDEN = 'FORBIDDEN', | ||
NOT_FOUND = 'NOT_FOUND', | ||
UNKNOWN_ERROR = 'UNKNOWN_ERROR', | ||
SERVER_INCORRECT_DATA_FORMAT_ERROR = 'SERVER_INCORRECT_DATA_FORMAT_ERROR', | ||
CLIENT_SEND_DATA_ERROR = 'CLIENT_SEND_DATA_ERROR', | ||
PROGRAMMER_SEND_DATA_ERROR = 'PROGRAMMER_SEND_DATA_ERROR', | ||
} | ||
|
||
export type GetReservationsResponse = | ||
| { | ||
status: 'correct'; | ||
data: Reservation[]; | ||
} | ||
| { | ||
status: 'error'; | ||
error: GetReservationsErrors; | ||
}; | ||
|
||
export namespace GetReservationsErrors { | ||
export function from(err: HttpErrorResponse): GetReservationsErrors | never { | ||
return match(err.status) | ||
.returnType<GetReservationsErrors>() | ||
.with(0, () => { | ||
throw new Error('You are offline or the server is down.'); | ||
}) | ||
.with( | ||
HttpStatusCode.Unauthorized, | ||
() => GetReservationsErrors.UNAUTHORIZED, | ||
) | ||
.with(HttpStatusCode.Forbidden, () => GetReservationsErrors.FORBIDDEN) | ||
.with(HttpStatusCode.NotFound, () => GetReservationsErrors.NOT_FOUND) | ||
.with( | ||
HttpStatusCode.UnprocessableEntity, | ||
() => GetReservationsErrors.PROGRAMMER_SEND_DATA_ERROR, | ||
) | ||
.otherwise(() => GetReservationsErrors.UNKNOWN_ERROR); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { isValidId } from '../refuge/refuge'; | ||
|
||
export type Night = { | ||
day: number; | ||
month: number; | ||
year: number; | ||
refugeId: string; | ||
}; | ||
|
||
export function isValidNight(night: Night): boolean { | ||
return isValidId(night.refugeId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { P } from 'ts-pattern'; | ||
|
||
export type Reservation = { | ||
userId: string; | ||
refugeId: string; | ||
night: { | ||
day: number; | ||
month: number; | ||
year: number; | ||
}; | ||
id: string; | ||
}; | ||
|
||
export const ReservationPattern: P.Pattern<Reservation> = {}; |