From 041650ebd6ea5f96111b5f38386760e0b0caed3e Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Fri, 6 Mar 2026 17:49:13 +0000 Subject: [PATCH] Edit app-9xzmfic2e4g1/src/db/api.ts via Editor --- app-9xzmfic2e4g1/src/db/api.ts | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/app-9xzmfic2e4g1/src/db/api.ts b/app-9xzmfic2e4g1/src/db/api.ts index d2b2519..627cd6a 100644 --- a/app-9xzmfic2e4g1/src/db/api.ts +++ b/app-9xzmfic2e4g1/src/db/api.ts @@ -40,6 +40,7 @@ export interface Trip { } const api = { + // Trips async getTrips() { const { data, error } = await supabase .from('trips') @@ -88,6 +89,7 @@ const api = { if (error) throw error; }, + // Edge Functions async generateItinerary(params: { startDate: string; endDate: string; @@ -127,6 +129,9 @@ const api = { return `${baseUrl}/functions/v1/get-place-photo?photo_reference=${photoReference}`; }, + // ── Public Guides ────────────────────────────────────────────────────────── + + /** Rehber olarak yayınla / yayından kaldır */ async publishGuide(tripId: string, opts: { guide_intro?: string; guide_tips?: string[]; @@ -154,10 +159,11 @@ const api = { if (error) throw error; }, + /** Tüm public rehberleri getir (popülerlik sırasına göre) */ async getPublicGuides(limit = 20) { const { data, error } = await supabase .from('trips') - .select('id, title, destination, start_date, end_date, itinerary, guide_intro, guide_tips, views_count, likes_count, published_at, user_id, profiles(id, full_name)') + .select('id, title, destination, start_date, end_date, itinerary, guide_intro, guide_tips, views_count, likes_count, published_at, user_id, profiles(id, username)') .eq('is_public', true) .order('likes_count', { ascending: false }) .order('views_count', { ascending: false }) @@ -166,18 +172,21 @@ const api = { return data || []; }, + /** Tek bir public rehberi getir + view sayacını artır */ async getPublicGuide(tripId: string) { const { data, error } = await supabase .from('trips') - .select('*, profiles(id, full_name)') + .select('*, profiles(id, username)') .eq('id', tripId) .eq('is_public', true) .single(); if (error) throw error; + // view sayacını artır (fire & forget) supabase.rpc('increment_guide_views', { trip_id: tripId }).then(() => {}); return data; }, + /** Rehberi kopyalayarak kendi tripini oluştur */ async cloneGuide(tripId: string): Promise { const guide = await this.getPublicGuide(tripId); if (!guide) throw new Error('Rehber bulunamadı'); @@ -192,24 +201,28 @@ const api = { return newTrip; }, + /** Beğen / beğeniyi geri al */ async toggleLike(tripId: string): Promise { const { data: { user } } = await supabase.auth.getUser(); if (!user) throw new Error('Giriş gerekli'); + const { data: existing } = await supabase .from('guide_likes') .select('user_id') .eq('user_id', user.id) .eq('trip_id', tripId) .maybeSingle(); + if (existing) { await supabase.from('guide_likes').delete().eq('user_id', user.id).eq('trip_id', tripId); - return false; + return false; // beğeni kaldırıldı } else { await supabase.from('guide_likes').insert({ user_id: user.id, trip_id: tripId }); - return true; + return true; // beğenildi } }, + /** Kullanıcının beğendiği guide id'leri */ async getMyLikes(): Promise { const { data: { user } } = await supabase.auth.getUser(); if (!user) return []; @@ -217,8 +230,6 @@ const api = { .from('guide_likes') .select('trip_id') .eq('user_id', user.id); - return (data || []).map((r: any) => r.trip_id); + return (data || []).map(r => r.trip_id); }, -}; - -export default api; \ No newline at end of file +}; \ No newline at end of file