TripService.ts
2,951 bytes
| 1 | import httpClient from '@/services/httpClient' |
|---|---|
| 2 | import type { IResultObject } from '@/types/IResultObject' |
| 3 | import type { ITrip, ITripCreate, ITripUpdate } from '@/types/ITrip' |
| 4 | import axios from 'axios' |
| 5 | |
| 6 | export default class TripService { |
| 7 | static async getAll(): Promise<IResultObject<ITrip[]>> { |
| 8 | try { |
| 9 | const response = await httpClient.get<ITrip[]>('Trips') |
| 10 | return { data: response.data } |
| 11 | } catch (e) { |
| 12 | return { errors: TripService.handleError(e) } |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | static async getById(id: string): Promise<IResultObject<ITrip>> { |
| 17 | try { |
| 18 | const response = await httpClient.get<ITrip>(`Trips/${id}`) |
| 19 | return { data: response.data } |
| 20 | } catch (e) { |
| 21 | return { errors: TripService.handleError(e) } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | static async create(entity: ITripCreate): Promise<IResultObject<ITrip>> { |
| 26 | try { |
| 27 | const response = await httpClient.post<ITrip>('Trips', entity) |
| 28 | return { data: response.data } |
| 29 | } catch (e) { |
| 30 | return { errors: TripService.handleError(e) } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | static async update(id: string, entity: ITripUpdate): Promise<IResultObject<void>> { |
| 35 | try { |
| 36 | await httpClient.put(`Trips/${id}`, entity) |
| 37 | return { data: undefined } |
| 38 | } catch (e) { |
| 39 | return { errors: TripService.handleError(e) } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static async delete(id: string): Promise<IResultObject<void>> { |
| 44 | try { |
| 45 | await httpClient.delete(`Trips/${id}`) |
| 46 | return { data: undefined } |
| 47 | } catch (e) { |
| 48 | return { errors: TripService.handleError(e) } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static async finalize(tripId: string): Promise<IResultObject<void>> { |
| 53 | try { |
| 54 | await httpClient.post(`Trips/${tripId}/finalize`) |
| 55 | return { data: undefined } |
| 56 | } catch (e) { |
| 57 | return { errors: TripService.handleError(e) } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static async reopen(tripId: string): Promise<IResultObject<void>> { |
| 62 | try { |
| 63 | await httpClient.post(`Trips/${tripId}/reopen`) |
| 64 | return { data: undefined } |
| 65 | } catch (e) { |
| 66 | return { errors: TripService.handleError(e) } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static async getParticipants(tripId: string): Promise<IResultObject<import('@/types/ITrip').ITripParticipant[]>> { |
| 71 | try { |
| 72 | const response = await httpClient.get<import('@/types/ITrip').ITripParticipant[]>(`Trips/${tripId}/participants`) |
| 73 | return { data: response.data } |
| 74 | } catch (e) { |
| 75 | return { errors: TripService.handleError(e) } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private static handleError(e: unknown): string[] { |
| 80 | if (axios.isAxiosError(e)) { |
| 81 | const data = e.response?.data |
| 82 | if (data?.errors && typeof data.errors === 'object') { |
| 83 | const messages: string[] = [] |
| 84 | for (const field of Object.values(data.errors)) { |
| 85 | if (Array.isArray(field)) messages.push(...field) |
| 86 | } |
| 87 | if (messages.length > 0) return messages |
| 88 | } |
| 89 | if (data?.title) return [data.title] |
| 90 | return [e.response?.statusText ?? 'Unknown error'] |
| 91 | } |
| 92 | return ['Network error or server unavailable'] |
| 93 | } |
| 94 | } |
| 95 | |