From 428714e97831747900e4065d3eccaebf0830c023 Mon Sep 17 00:00:00 2001 From: Sosokker Date: Sat, 10 May 2025 04:48:25 +0700 Subject: [PATCH] api: add get endpoints --- services/data/cooking.ts | 15 +++++++++++++++ services/data/foods.ts | 27 +-------------------------- services/data/forum.ts | 21 +++++++++++++++++++++ types.ts | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 26 deletions(-) create mode 100644 services/data/cooking.ts create mode 100644 services/data/forum.ts create mode 100644 types.ts diff --git a/services/data/cooking.ts b/services/data/cooking.ts new file mode 100644 index 0000000..211b63e --- /dev/null +++ b/services/data/cooking.ts @@ -0,0 +1,15 @@ +import { supabase } from "@/services/supabase"; +import { CookingStep } from "@/types"; +import { PostgrestError } from "@supabase/supabase-js"; + +export const getCookingSteps = async (food_id: string): Promise<{ data: CookingStep[] | null; error: PostgrestError | null }> => { + const { data, error } = await supabase.from("cooking_steps").select(` + id, + created_at, + food_id, + step_order, + title, + description + `).eq("food_id", food_id).order("step_order", { ascending: true }); + return { data, error }; +}; \ No newline at end of file diff --git a/services/data/foods.ts b/services/data/foods.ts index 0c3c15a..2237314 100644 --- a/services/data/foods.ts +++ b/services/data/foods.ts @@ -1,20 +1,7 @@ import { supabase } from "@/services/supabase"; +import { Foods, LikedFood, SavedFood } from "@/types"; import { PostgrestError } from "@supabase/supabase-js"; -interface Foods { - id: string; - name: string; - description?: string; - time_to_cook_minutes: number; - skill_level: "Easy" | "Medium" | "Hard"; - ingredient_count?: number; - calories?: number; - image_url?: string; - is_shared: boolean; - created_by: string; - created_at: string; -} - /** * Retrieves a list of foods based on the provided filters. * @@ -58,18 +45,6 @@ export const getFoods = async ( return { data, error }; }; -interface SavedFood { - user_id: string; - food_id: string; - created_at: string; -} - -interface LikedFood { - user_id: string; - food_id: string; - created_at: string; -} - /** * Retrieves a list of saved foods for a specific user. * diff --git a/services/data/forum.ts b/services/data/forum.ts new file mode 100644 index 0000000..20ddb3e --- /dev/null +++ b/services/data/forum.ts @@ -0,0 +1,21 @@ +import { supabase } from "@/services/supabase"; + +export const createLike = async (food_id: string, user_id: string) => { + const { data, error } = await supabase.from("food_likes").insert({ food_id, user_id }); + return { data, error }; +} + +export const createSave = async (food_id: string, user_id: string) => { + const { data, error } = await supabase.from("food_saves").insert({ food_id, user_id }); + return { data, error }; +} + +export const deleteLike = async (food_id: string, user_id: string) => { + const { data, error } = await supabase.from("food_likes").delete().eq("food_id", food_id).eq("user_id", user_id); + return { data, error }; +} + +export const deleteSave = async (food_id: string, user_id: string) => { + const { data, error } = await supabase.from("food_saves").delete().eq("food_id", food_id).eq("user_id", user_id); + return { data, error }; +} diff --git a/types.ts b/types.ts new file mode 100644 index 0000000..3d03231 --- /dev/null +++ b/types.ts @@ -0,0 +1,36 @@ +interface SavedFood { + user_id: string; + food_id: string; + created_at: string; + } + + interface LikedFood { + user_id: string; + food_id: string; + created_at: string; + } + + interface Foods { + id: string; + name: string; + description?: string; + time_to_cook_minutes: number; + skill_level: "Easy" | "Medium" | "Hard"; + ingredient_count?: number; + calories?: number; + image_url?: string; + is_shared: boolean; + created_by: string; + created_at: string; +} + +interface CookingStep { + id: string, + created_at: string, + food_id: string, + step_order: number, + title: string, + description: string +} + +export { CookingStep, Foods, LikedFood, SavedFood };