import { Product, ProductCreate, Shop, ShopCreate, ShoppingEvent, ShoppingEventCreate, Brand, BrandCreate, Grocery, GroceryCreate, GroceryCategory, GroceryCategoryCreate } from '../types'; const API_BASE_URL = '/api'; const api = { get: (url: string): Promise<{ data: T }> => fetch(`${API_BASE_URL}${url}`).then(res => res.json()).then(data => ({ data })), post: (url: string, body: any): Promise<{ data: T }> => fetch(`${API_BASE_URL}${url}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }).then(res => res.json()).then(data => ({ data })), put: (url: string, body: any): Promise<{ data: T }> => fetch(`${API_BASE_URL}${url}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }).then(res => res.json()).then(data => ({ data })), delete: (url: string): Promise => fetch(`${API_BASE_URL}${url}`, { method: 'DELETE' }).then(() => {}), }; // Product API functions export const productApi = { getAll: () => api.get('/products/'), getById: (id: number) => api.get(`/products/${id}`), create: (product: ProductCreate) => api.post('/products/', product), update: (id: number, product: Partial) => api.put(`/products/${id}`, product), delete: (id: number) => api.delete(`/products/${id}`), }; // Shop API functions export const shopApi = { getAll: () => api.get('/shops/'), getById: (id: number) => api.get(`/shops/${id}`), create: (shop: ShopCreate) => api.post('/shops/', shop), update: (id: number, shop: Partial) => api.put(`/shops/${id}`, shop), delete: (id: number) => api.delete(`/shops/${id}`), }; // Brand API functions export const brandApi = { getAll: () => api.get('/brands/'), getById: (id: number) => api.get(`/brands/${id}`), create: (brand: BrandCreate) => api.post('/brands/', brand), update: (id: number, brand: Partial) => api.put(`/brands/${id}`, brand), delete: (id: number) => api.delete(`/brands/${id}`), }; // Grocery Category API functions export const groceryCategoryApi = { getAll: () => api.get('/grocery-categories/'), getById: (id: number) => api.get(`/grocery-categories/${id}`), create: (category: GroceryCategoryCreate) => api.post('/grocery-categories/', category), update: (id: number, category: Partial) => api.put(`/grocery-categories/${id}`, category), delete: (id: number) => api.delete(`/grocery-categories/${id}`), }; // Shopping Event API functions export const shoppingEventApi = { getAll: () => api.get('/shopping-events/'), getById: (id: number) => api.get(`/shopping-events/${id}`), create: (event: ShoppingEventCreate) => api.post('/shopping-events/', event), update: (id: number, event: ShoppingEventCreate) => api.put(`/shopping-events/${id}`, event), delete: (id: number) => api.delete(`/shopping-events/${id}`), }; // Grocery API functions export const groceryApi = { getAll: () => api.get('/groceries/'), getById: (id: number) => api.get(`/groceries/${id}`), create: (grocery: GroceryCreate) => api.post('/groceries/', grocery), update: (id: number, grocery: Partial) => api.put(`/groceries/${id}`, grocery), delete: (id: number) => api.delete(`/groceries/${id}`), }; // Statistics API functions export const statsApi = { getCategories: () => api.get('/stats/categories'), getShops: () => api.get('/stats/shops'), }; export default api;