Edit app-9xzmfic2e4g1/src/db/api.ts via Editor

This commit is contained in:
Flatlogic Bot 2026-03-06 17:39:29 +00:00
parent 2a52cdf8a5
commit 27a33044af

View File

@ -40,7 +40,6 @@ export interface Trip {
} }
const api = { const api = {
// Trips
async getTrips() { async getTrips() {
const { data, error } = await supabase const { data, error } = await supabase
.from('trips') .from('trips')
@ -89,7 +88,6 @@ const api = {
if (error) throw error; if (error) throw error;
}, },
// Edge Functions
async generateItinerary(params: { async generateItinerary(params: {
startDate: string; startDate: string;
endDate: string; endDate: string;
@ -131,7 +129,6 @@ const api = {
// ── Public Guides ────────────────────────────────────────────────────────── // ── Public Guides ──────────────────────────────────────────────────────────
/** Rehber olarak yayınla / yayından kaldır */
async publishGuide(tripId: string, opts: { async publishGuide(tripId: string, opts: {
guide_intro?: string; guide_intro?: string;
guide_tips?: string[]; guide_tips?: string[];
@ -159,11 +156,10 @@ const api = {
if (error) throw error; if (error) throw error;
}, },
/** Tüm public rehberleri getir (popülerlik sırasına göre) */
async getPublicGuides(limit = 20) { async getPublicGuides(limit = 20) {
const { data, error } = await supabase const { data, error } = await supabase
.from('trips') .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, username)') .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)')
.eq('is_public', true) .eq('is_public', true)
.order('likes_count', { ascending: false }) .order('likes_count', { ascending: false })
.order('views_count', { ascending: false }) .order('views_count', { ascending: false })
@ -172,21 +168,18 @@ const api = {
return data || []; return data || [];
}, },
/** Tek bir public rehberi getir + view sayacını artır */
async getPublicGuide(tripId: string) { async getPublicGuide(tripId: string) {
const { data, error } = await supabase const { data, error } = await supabase
.from('trips') .from('trips')
.select('*, profiles(id, username)') .select('*, profiles(id, full_name)')
.eq('id', tripId) .eq('id', tripId)
.eq('is_public', true) .eq('is_public', true)
.single(); .single();
if (error) throw error; if (error) throw error;
// view sayacını artır (fire & forget)
supabase.rpc('increment_guide_views', { trip_id: tripId }).then(() => {}); supabase.rpc('increment_guide_views', { trip_id: tripId }).then(() => {});
return data; return data;
}, },
/** Rehberi kopyalayarak kendi tripini oluştur */
async cloneGuide(tripId: string): Promise<Trip> { async cloneGuide(tripId: string): Promise<Trip> {
const guide = await this.getPublicGuide(tripId); const guide = await this.getPublicGuide(tripId);
if (!guide) throw new Error('Rehber bulunamadı'); if (!guide) throw new Error('Rehber bulunamadı');
@ -201,7 +194,6 @@ const api = {
return newTrip; return newTrip;
}, },
/** Beğen / beğeniyi geri al */
async toggleLike(tripId: string): Promise<boolean> { async toggleLike(tripId: string): Promise<boolean> {
const { data: { user } } = await supabase.auth.getUser(); const { data: { user } } = await supabase.auth.getUser();
if (!user) throw new Error('Giriş gerekli'); if (!user) throw new Error('Giriş gerekli');
@ -215,14 +207,13 @@ const api = {
if (existing) { if (existing) {
await supabase.from('guide_likes').delete().eq('user_id', user.id).eq('trip_id', tripId); await supabase.from('guide_likes').delete().eq('user_id', user.id).eq('trip_id', tripId);
return false; // beğeni kaldırıldı return false;
} else { } else {
await supabase.from('guide_likes').insert({ user_id: user.id, trip_id: tripId }); await supabase.from('guide_likes').insert({ user_id: user.id, trip_id: tripId });
return true; // beğenildi return true;
} }
}, },
/** Kullanıcının beğendiği guide id'leri */
async getMyLikes(): Promise<string[]> { async getMyLikes(): Promise<string[]> {
const { data: { user } } = await supabase.auth.getUser(); const { data: { user } } = await supabase.auth.getUser();
if (!user) return []; if (!user) return [];
@ -232,4 +223,6 @@ const api = {
.eq('user_id', user.id); .eq('user_id', user.id);
return (data || []).map(r => r.trip_id); return (data || []).map(r => r.trip_id);
}, },
}; };
export default api;