Initial Version
This commit is contained in:
50
frontend/src/services/api.ts
Normal file
50
frontend/src/services/api.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import axios from 'axios';
|
||||
import { Grocery, GroceryCreate, Shop, ShopCreate, ShoppingEvent, ShoppingEventCreate } from '../types';
|
||||
|
||||
const BASE_URL = 'http://localhost:8000';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// Grocery API functions
|
||||
export const groceryApi = {
|
||||
getAll: () => api.get<Grocery[]>('/groceries/'),
|
||||
getById: (id: number) => api.get<Grocery>(`/groceries/${id}`),
|
||||
create: (grocery: GroceryCreate) => api.post<Grocery>('/groceries/', grocery),
|
||||
update: (id: number, grocery: Partial<GroceryCreate>) =>
|
||||
api.put<Grocery>(`/groceries/${id}`, grocery),
|
||||
delete: (id: number) => api.delete(`/groceries/${id}`),
|
||||
};
|
||||
|
||||
// Shop API functions
|
||||
export const shopApi = {
|
||||
getAll: () => api.get<Shop[]>('/shops/'),
|
||||
getById: (id: number) => api.get<Shop>(`/shops/${id}`),
|
||||
create: (shop: ShopCreate) => api.post<Shop>('/shops/', shop),
|
||||
update: (id: number, shop: Partial<ShopCreate>) =>
|
||||
api.put<Shop>(`/shops/${id}`, shop),
|
||||
delete: (id: number) => api.delete(`/shops/${id}`),
|
||||
};
|
||||
|
||||
// Shopping Event API functions
|
||||
export const shoppingEventApi = {
|
||||
getAll: () => api.get<ShoppingEvent[]>('/shopping-events/'),
|
||||
getById: (id: number) => api.get<ShoppingEvent>(`/shopping-events/${id}`),
|
||||
create: (event: ShoppingEventCreate) =>
|
||||
api.post<ShoppingEvent>('/shopping-events/', event),
|
||||
update: (id: number, event: Partial<ShoppingEventCreate>) =>
|
||||
api.put<ShoppingEvent>(`/shopping-events/${id}`, event),
|
||||
delete: (id: number) => api.delete(`/shopping-events/${id}`),
|
||||
};
|
||||
|
||||
// Statistics API functions
|
||||
export const statsApi = {
|
||||
getCategories: () => api.get('/stats/categories'),
|
||||
getShops: () => api.get('/stats/shops'),
|
||||
};
|
||||
|
||||
export default api;
|
||||
Reference in New Issue
Block a user