Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add: services for object
  • Loading branch information
htnghia1423 committed Apr 29, 2025
commit b03800a4b8c1d9c091b83787a8e6c20a6eafc196
38 changes: 38 additions & 0 deletions src/features/gifts/giftService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { CreateGiftDTO, GiftIdea } from '@/features/gifts/types';
import supabase from '@/services/supabaseClient';

export const fetchGifts = async (): Promise<GiftIdea[]> => {
const { data, error } = await supabase.from('gifts').select('*');
if (error) throw error;
return data as GiftIdea[];
};

export const addGift = async (gift: CreateGiftDTO): Promise<GiftIdea> => {
const { data, error } = await supabase.from('gifts').insert([gift]).single();
if (error) throw error;
return data as GiftIdea;
};

export const deleteGift = async (id: string): Promise<void> => {
const { error } = await supabase.from('gifts').delete().eq('id', id);
if (error) throw error;
};

export const updateGift = async (
gift: Partial<GiftIdea>,
): Promise<GiftIdea> => {
const { data, error } = await supabase
.from('gifts')
.update(gift)
.eq('id', gift.id)
.select('*')
.single();

if (error) {
console.error('Supabase updateGift error:', error);
throw error;
}

console.log('Supabase updateGift response:', data);
Copy link

Copilot AI May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider removing or replacing this console.log used for debugging with a proper logging mechanism before deploying to production.

Copilot uses AI. Check for mistakes.
return data as GiftIdea;
};
39 changes: 39 additions & 0 deletions src/features/recipients/recipientService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { CreateRecipientDTO, Recipient } from '@/features/recipients/types';
import supabase from '@/services/supabaseClient';

export const fetchRecipients = async (): Promise<Recipient[]> => {
const { data, error } = await supabase.from('recipients').select('*');
if (error) throw error;
return data as Recipient[];
};

export const addRecipient = async (
recipient: CreateRecipientDTO,
): Promise<Recipient> => {
const { data, error } = await supabase
.from('recipients')
.insert([recipient])
.single();
if (error) throw error;
return data as Recipient;
};

export const deleteRecipient = async (id: string): Promise<void> => {
const { error } = await supabase.from('recipients').delete().eq('id', id);
if (error) throw error;
};

export const findRecipientById = async (id: number): Promise<string | null> => {
Copy link

Copilot AI May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter type for 'id' in findRecipientById is declared as number, but recipient IDs are defined as strings in the types. Consider updating the function to accept a string to ensure consistency across the codebase.

Suggested change
export const findRecipientById = async (id: number): Promise<string | null> => {
export const findRecipientById = async (id: string): Promise<string | null> => {

Copilot uses AI. Check for mistakes.
const { data, error } = await supabase
.from('recipients')
.select('name')
.eq('id', id)
.single();

if (error) {
console.error('Error fetching recipient:', error);
return null;
}

return data?.name || null;
};