Files
project_it207_client/services/product.ts
2025-11-14 14:44:46 +07:00

113 lines
2.5 KiB
TypeScript

import api from './api';
export interface ProductResponse {
productId: number;
productName: string;
description: string;
price: number;
stockQuantity: number;
imageUrl: string;
categoryId: number;
categoryName: string;
createdAt: string;
updatedAt: string;
}
export interface ProductListResponse {
products: ProductResponse[];
currentPage: number;
totalItems: number;
totalPages: number;
hasNext: boolean;
hasPrevious: boolean;
}
class ProductService {
/**
* Lấy danh sách tất cả sản phẩm
*/
async getAllProducts(
page: number = 0,
size: number = 10,
sortBy: string = 'productId',
sortDir: 'asc' | 'desc' = 'asc'
): Promise<ProductListResponse> {
const params = new URLSearchParams({
page: page.toString(),
size: size.toString(),
sortBy,
sortDir,
});
return await api.get<ProductListResponse>(`/products?${params}`);
}
/**
* Lấy chi tiết sản phẩm theo ID
*/
async getProductById(id: number): Promise<ProductResponse> {
return await api.get<ProductResponse>(`/products/${id}`);
}
/**
* Lấy sản phẩm theo category
*/
async getProductsByCategory(
categoryId: number,
page: number = 0,
size: number = 10,
sortBy: string = 'productId',
sortDir: 'asc' | 'desc' = 'asc'
): Promise<ProductListResponse> {
const params = new URLSearchParams({
page: page.toString(),
size: size.toString(),
sortBy,
sortDir,
});
return await api.get<ProductListResponse>(`/products/category/${categoryId}?${params}`);
}
/**
* Tìm kiếm sản phẩm
*/
async searchProducts(
keyword: string,
page: number = 0,
size: number = 10,
sortBy: string = 'productId',
sortDir: 'asc' | 'desc' = 'asc'
): Promise<ProductListResponse> {
const params = new URLSearchParams({
keyword,
page: page.toString(),
size: size.toString(),
sortBy,
sortDir,
});
return await api.get<ProductListResponse>(`/products/search?${params}`);
}
/**
* Lấy sản phẩm còn hàng
*/
async getAvailableProducts(
page: number = 0,
size: number = 10,
sortBy: string = 'productId',
sortDir: 'asc' | 'desc' = 'asc'
): Promise<ProductListResponse> {
const params = new URLSearchParams({
page: page.toString(),
size: size.toString(),
sortBy,
sortDir,
});
return await api.get<ProductListResponse>(`/products/available?${params}`);
}
}
export default new ProductService();