rename grocery to product
This commit is contained in:
@@ -1,23 +1,34 @@
|
||||
import axios from 'axios';
|
||||
import { Grocery, GroceryCreate, Shop, ShopCreate, ShoppingEvent, ShoppingEventCreate } from '../types';
|
||||
import { Product, ProductCreate, Shop, ShopCreate, ShoppingEvent, ShoppingEventCreate } from '../types';
|
||||
|
||||
const BASE_URL = 'http://localhost:8000';
|
||||
const API_BASE_URL = 'http://localhost:8000';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
const api = {
|
||||
get: <T>(url: string): Promise<{ data: T }> =>
|
||||
fetch(`${API_BASE_URL}${url}`).then(res => res.json()).then(data => ({ data })),
|
||||
post: <T>(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: <T>(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<void> =>
|
||||
fetch(`${API_BASE_URL}${url}`, { method: 'DELETE' }).then(() => {}),
|
||||
};
|
||||
|
||||
// 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}`),
|
||||
// Product API functions
|
||||
export const productApi = {
|
||||
getAll: () => api.get<Product[]>('/products/'),
|
||||
getById: (id: number) => api.get<Product>(`/products/${id}`),
|
||||
create: (product: ProductCreate) => api.post<Product>('/products/', product),
|
||||
update: (id: number, product: Partial<ProductCreate>) =>
|
||||
api.put<Product>(`/products/${id}`, product),
|
||||
delete: (id: number) => api.delete(`/products/${id}`),
|
||||
};
|
||||
|
||||
// Shop API functions
|
||||
@@ -25,7 +36,7 @@ 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>) =>
|
||||
update: (id: number, shop: Partial<ShopCreate>) =>
|
||||
api.put<Shop>(`/shops/${id}`, shop),
|
||||
delete: (id: number) => api.delete(`/shops/${id}`),
|
||||
};
|
||||
@@ -34,9 +45,8 @@ export const shopApi = {
|
||||
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: ShoppingEventCreate) =>
|
||||
create: (event: ShoppingEventCreate) => api.post<ShoppingEvent>('/shopping-events/', event),
|
||||
update: (id: number, event: ShoppingEventCreate) =>
|
||||
api.put<ShoppingEvent>(`/shopping-events/${id}`, event),
|
||||
delete: (id: number) => api.delete(`/shopping-events/${id}`),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user