82 lines
2.6 KiB
TypeScript
82 lines
2.6 KiB
TypeScript
import api from '../../../services/api';
|
|
import type {
|
|
LocationCreateRequest,
|
|
LocationCreateResponse,
|
|
LocationDeleteResponse,
|
|
LocationListResponse,
|
|
LocationPaginatedResponse,
|
|
LocationUpdateRequest,
|
|
LocationUpdateResponse,
|
|
} from '../types/locations.interfaces';
|
|
|
|
const locationServices = {
|
|
async getLocations(
|
|
paginated?: boolean,
|
|
name?: string,
|
|
city?: string,
|
|
state?: string,
|
|
country?: string
|
|
): Promise<LocationPaginatedResponse | LocationListResponse> {
|
|
try {
|
|
const params: any = {};
|
|
if (paginated === false) params.paginate = false;
|
|
if (name) params.name = name;
|
|
if (city) params.city = city;
|
|
if (state) params.state = state;
|
|
if (country) params.country = country;
|
|
|
|
const response = await api.get('/api/locations', { params });
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Error fetching locations:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
async getLocationById(locationId: number): Promise<LocationCreateResponse> {
|
|
try {
|
|
const response = await api.get(`/api/locations/${locationId}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error(`Error fetching location with ID ${locationId}:`, error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
async createLocation(data: LocationCreateRequest): Promise<LocationCreateResponse> {
|
|
try {
|
|
const response = await api.post('/api/locations', data);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Error creating location:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
async updateLocation(
|
|
locationId: number,
|
|
data: LocationUpdateRequest,
|
|
method: 'patch' | 'put' = 'patch'
|
|
): Promise<LocationUpdateResponse> {
|
|
try {
|
|
const response = await api[method](`/api/locations/${locationId}`, data);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error(`Error updating location with ID ${locationId}:`, error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
async deleteLocation(locationId: number): Promise<LocationDeleteResponse> {
|
|
try {
|
|
const response = await api.delete(`/api/locations/${locationId}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error(`Error deleting location with ID ${locationId}:`, error);
|
|
throw error;
|
|
}
|
|
},
|
|
};
|
|
|
|
export { locationServices };
|