diff --git a/src/data/pocketbase.ts b/src/data/pocketbase.ts index bc56688..1bae413 100644 --- a/src/data/pocketbase.ts +++ b/src/data/pocketbase.ts @@ -1,4 +1,11 @@ -import Pocketbase from "pocketbase" +import Pocketbase, { type RecordListOptions } from "pocketbase" +import { + type Recipe, + type Ingredient, + type Step, + type Tag, + Collection +} from './schema' const client = new Pocketbase("http://localhost:4321") client.autoCancellation(false) @@ -9,4 +16,27 @@ client.files.getRelativeURL = (record: { [key: string]: any; }, filename: string return res.substring(21) } -export default client; \ No newline at end of file +export default client; + +class APIClient { + client: Pocketbase + + constructor() { + this.client = new Pocketbase("http://localhost:4321") + this.client.autoCancellation(false) + } + + async getRecipes(page: number, perPage: number = 20, options: RecordListOptions) { + return await this.client.collection(Collection.RECIPES).getList(page, perPage, options) + } + + async getAllRecipes() { + return await this.client.collection(Collection.RECIPES).getFullList({ expand: "steps,ingredients,tags" }) + } + + async getRecipe(id: string) { + return await this.client.collection(Collection.RECIPES).getOne(id, { expand: 'ingredients,tags,steps', }) + } +} + +export const client2 = new APIClient() \ No newline at end of file diff --git a/src/data/schema.ts b/src/data/schema.ts index 97e3c6f..88c76e5 100644 --- a/src/data/schema.ts +++ b/src/data/schema.ts @@ -36,5 +36,10 @@ export interface Recipe extends BaseRecord { tags?: Tag[] } - - +export const Collection = { + RECIPES: 'recipes', + STEPS: 'steps', + INGREDIENTS: 'ingredients', + TAGS: 'tags', + IMAGES: 'images' +} diff --git a/src/pages/recipe/[recipeid].astro b/src/pages/recipe/[recipeid].astro index d123a91..892ccf2 100644 --- a/src/pages/recipe/[recipeid].astro +++ b/src/pages/recipe/[recipeid].astro @@ -1,5 +1,6 @@ --- -import client from "@/data/pocketbase"; +import client, { client2 } from "@/data/pocketbase"; + import SiteLayout from "@/layouts/base"; import ImageCarousel from "@/components/Detail/ImageCarousel"; import IngredientTableView from "@/components/Detail/IngredientTableView"; @@ -8,6 +9,9 @@ import InfoView from "@/components/Detail/InfoView"; const { recipeid } = Astro.params; +const re2 = await client2.getRecipe(recipeid as string) +console.log(re2) + const re = await client.collection("recipes").getOne(recipeid ?? "0"); const stepIds = re.steps