68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
import api from './api';
|
||
|
||
// Типы данных для корзины
|
||
export interface CartItemCreate {
|
||
variant_id: number;
|
||
quantity: number;
|
||
}
|
||
|
||
export interface CartItemUpdate {
|
||
quantity: number;
|
||
}
|
||
|
||
export interface CartItem {
|
||
id: number;
|
||
user_id: number;
|
||
variant_id: number;
|
||
quantity: number;
|
||
created_at: string;
|
||
updated_at?: string;
|
||
product_id: number;
|
||
product_name: string;
|
||
product_price: number;
|
||
product_image?: string;
|
||
variant_name: string;
|
||
slug: string;
|
||
total_price: number;
|
||
}
|
||
|
||
export interface Cart {
|
||
items: CartItem[];
|
||
total_amount: number;
|
||
items_count: number;
|
||
}
|
||
|
||
// Сервис для работы с корзиной
|
||
const cartService = {
|
||
// Получить корзину пользователя
|
||
getCart: async (): Promise<Cart> => {
|
||
const response = await api.get('/cart');
|
||
return response.data;
|
||
},
|
||
|
||
// Добавить товар в корзину
|
||
addToCart: async (item: CartItemCreate): Promise<CartItem> => {
|
||
const response = await api.post('/cart/items', item);
|
||
return response.data.cart_item;
|
||
},
|
||
|
||
// Обновить количество товара в корзине
|
||
updateCartItem: async (id: number, item: CartItemUpdate): Promise<CartItem> => {
|
||
const response = await api.put(`/cart/items/${id}`, item);
|
||
return response.data.cart_item;
|
||
},
|
||
|
||
// Удалить товар из корзины
|
||
removeFromCart: async (id: number): Promise<boolean> => {
|
||
const response = await api.delete(`/cart/items/${id}`);
|
||
return response.data.success;
|
||
},
|
||
|
||
// Очистить корзину
|
||
clearCart: async (): Promise<boolean> => {
|
||
const response = await api.delete('/cart/clear');
|
||
return response.data.success;
|
||
}
|
||
};
|
||
|
||
export default cartService;
|