feat: 支持食物库接口
This commit is contained in:
49
services/foodLibraryApi.ts
Normal file
49
services/foodLibraryApi.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import type {
|
||||
FoodItemDto,
|
||||
FoodLibraryResponseDto,
|
||||
GetFoodByIdParams,
|
||||
SearchFoodsParams
|
||||
} from '@/types/food';
|
||||
import { api } from './api';
|
||||
|
||||
/**
|
||||
* 食物库 API 服务
|
||||
*/
|
||||
export class FoodLibraryApi {
|
||||
private static readonly BASE_PATH = '/food-library';
|
||||
|
||||
/**
|
||||
* 获取食物库列表
|
||||
*/
|
||||
static async getFoodLibrary(): Promise<FoodLibraryResponseDto> {
|
||||
return api.get<FoodLibraryResponseDto>(this.BASE_PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索食物
|
||||
*/
|
||||
static async searchFoods(params: SearchFoodsParams): Promise<FoodItemDto[]> {
|
||||
const { keyword } = params;
|
||||
if (!keyword || keyword.trim().length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const encodedKeyword = encodeURIComponent(keyword.trim());
|
||||
return api.get<FoodItemDto[]>(`${this.BASE_PATH}/search?keyword=${encodedKeyword}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取食物详情
|
||||
*/
|
||||
static async getFoodById(params: GetFoodByIdParams): Promise<FoodItemDto> {
|
||||
const { id } = params;
|
||||
return api.get<FoodItemDto>(`${this.BASE_PATH}/${id}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 导出便捷方法
|
||||
export const foodLibraryApi = {
|
||||
getFoodLibrary: () => FoodLibraryApi.getFoodLibrary(),
|
||||
searchFoods: (keyword: string) => FoodLibraryApi.searchFoods({ keyword }),
|
||||
getFoodById: (id: number) => FoodLibraryApi.getFoodById({ id }),
|
||||
};
|
||||
Reference in New Issue
Block a user