Skip to content

Commit

Permalink
Done:
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinjcai committed Nov 2, 2023
1 parent 2ef3323 commit 33e4307
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
5 changes: 1 addition & 4 deletions src/api/supabase/queries/tests/user_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export async function runFetchUserByUUID() {
}
}



export async function fullFavItemTest() {
const testUserId = '4a934844-76fa-4a1a-80d7-fa00597398e1';
const testItemId = '10';
Expand All @@ -42,8 +40,7 @@ export async function fullFavItemTest() {
removeFromFavorites(testUserId, testItemId);
result1 = await fetchFavoriteItems(testUserId);
console.log('fetchFavoriteItems Result:', result1);

} catch (error) {
console.error('Error in incrementCartItemByOne:', error);
}
}
}
66 changes: 39 additions & 27 deletions src/api/supabase/queries/user_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,22 @@ export async function fetchUserByUUID(
}
}

export async function fetchFavoriteItems(userId: string): Promise<Record<string, number>> {
export async function fetchFavoriteItems(
userId: string,
): Promise<Record<string, number>> {
// Fetch fav_items for the specified user
const { data, error } = await supabase
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();

if (error) {
throw new Error(`An error occurred when trying to fetch favorite items: ${error.message}`);
throw new Error(
`An error occurred when trying to fetch favorite items: ${error.message}`,
);
} else if (!data) {
throw new Error("No user found with the specified user_id.");
throw new Error('No user found with the specified user_id.');
}

return data.fav_items;
Expand All @@ -75,42 +79,48 @@ export async function fetchFavoriteItems(userId: string): Promise<Record<string,
export async function addToFavorites(userId: string, productId: string) {
// First, fetch the current fav_items for the user
const { data, error } = await supabase
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();

if (error) {
throw new Error(`An error occurred when trying to fetch the user's favorite items: ${error.message}`);
throw new Error(
`An error occurred when trying to fetch the user's favorite items: ${error.message}`,
);
}

const currentFavItems = data?.fav_items || {};

// Add the product to fav_items or update its quantity
currentFavItems[productId] = (currentFavItems[productId] || 0);
currentFavItems[productId] = currentFavItems[productId] || 0;

// Now update the user's fav_items in the database
const updateResponse = await supabase
.from('profiles')
.update({ fav_items: currentFavItems })
.eq('user_id', userId);
.from('profiles')
.update({ fav_items: currentFavItems })
.eq('user_id', userId);

if (updateResponse.error) {
throw new Error(`An error occurred when trying to update the user's favorite items: ${updateResponse.error.message}`);
throw new Error(
`An error occurred when trying to update the user's favorite items: ${updateResponse.error.message}`,
);
}
}

// Function to remove a product from fav_items
export async function removeFromFavorites(userId: string, productId: string) {
// First, fetch the current fav_items for the user
const { data, error } = await supabase
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();

if (error) {
throw new Error(`An error occurred when trying to fetch the user's favorite items: ${error.message}`);
throw new Error(
`An error occurred when trying to fetch the user's favorite items: ${error.message}`,
);
}

const currentFavItems = data?.fav_items || {};
Expand All @@ -120,11 +130,13 @@ export async function removeFromFavorites(userId: string, productId: string) {
console.log(currentFavItems);
// Now update the user's fav_items in the database
const updateResponse = await supabase
.from('profiles')
.update({ fav_items: currentFavItems })
.eq('user_id', userId);
.from('profiles')
.update({ fav_items: currentFavItems })
.eq('user_id', userId);

if (updateResponse.error) {
throw new Error(`An error occurred when trying to update the user's favorite items: ${updateResponse.error.message}`);
throw new Error(
`An error occurred when trying to update the user's favorite items: ${updateResponse.error.message}`,
);
}
}
}
5 changes: 1 addition & 4 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import React, { useEffect } from 'react';

import Link from 'next/link';
import {
fullFavItemTest
} from '../api/supabase/queries/tests/user_test';
import { fullFavItemTest } from '../api/supabase/queries/tests/user_test';
import {
testFetchOrderByUUID,
testFetchOrders,
Expand Down Expand Up @@ -46,7 +44,6 @@ export default function Checkout() {
});

return (

<main>
<Link href="/login">Login</Link>
</main>
Expand Down

0 comments on commit 33e4307

Please sign in to comment.