Skip to content

Commit

Permalink
Merge pull request #10 from calblueprint/kevin_cai/Add_schema_to_supa…
Browse files Browse the repository at this point in the history
…base

Kevin cai/add schema to supabase
  • Loading branch information
EthanAuyeung authored Oct 15, 2023
2 parents da236ce + 9652e93 commit bcf9b36
Show file tree
Hide file tree
Showing 14 changed files with 759 additions and 163 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ yarn-error.log*

# local env files
.env*.local
.env

# vercel
.vercel
Expand Down
296 changes: 135 additions & 161 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@types/react-dom": "18.2.7",
"dotenv": "^16.3.1",
"eslint-config-next": "13.5.2",
"next": "13.5.2",
"next": "^13.5.4",
"react": "^18.2.0",
"react-dom": "18.2.0",
"styled-components": "^6.0.8"
Expand Down
34 changes: 34 additions & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
import Link from 'next/link';
import {
testFetchUserData,
testFetchUserByUUID,
testAddUserAddress,
} from '../supabase/tests/user_test';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import {
testFetchOrderByUUID,
testFetchOrders,
testGetOrderById,
testToggleOrderProgress,
testUpdateAllOrdersProgressToTrue,
} from '../supabase/tests/order_test';
import {
testFetchProducts,
testFetchProductByName,
} from '../supabase/tests/product_test';
import {
testFetchPickupData,
testFetchPickupTimesByUUID,
} from '../supabase/tests/pickup_test';

export default function Checkout() {
testFetchUserData();
testFetchUserByUUID();
testAddUserAddress();
testFetchOrderByUUID();
testFetchOrders();
testGetOrderById();
testToggleOrderProgress();
testFetchProducts();
testFetchProductByName();
testFetchPickupData();
testFetchPickupTimesByUUID();
// testUpdateAllOrdersProgressToTrue();

return (
<main>
<Link href="/login">Login</Link>
Expand Down
38 changes: 38 additions & 0 deletions src/schema/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export type User = {
email: string;
password: string;
first_name: string;
last_name: string;
pet_information: string;
user_id: string; // UUID
order_option: boolean;
address: number; // index of the address the street, city, and zipcode to build the address
street: string[];
city: string[];
zipcode: string[];
};

export type Order = {
id: number; // bigint generated by default as identity
user_id: string; // UUID not null
cart: number; // bigint[] null
status: string; // bigint null
pickup_time: number; // bigint null
};

export type Schedule = {
id: number; // bigint generated by default as identity
date: string; // text not null
start_time: string; // text null
end_time: string; // text null
};

export type Product = {
product_id: number; // bigint generated by default as identity
name: string; // text not null;
description: string; // text null;
category: string; // numeric not null;
quantity: number; // numeric not null;
photo: string; // text null;
updated_at: string; // timestamp with time zone not null default now();
};
163 changes: 163 additions & 0 deletions src/supabase/order_queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* eslint-disable no-console */
//

import {
PostgrestSingleResponse,
PostgrestError,
createClient,
} from '@supabase/supabase-js';
import { Order } from '../schema/schema';

// Replace these with your Supabase project URL and API key
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseApiKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;

// Initialize the Supabase client
const supabase = createClient(supabaseUrl ?? '', supabaseApiKey ?? '');

export async function fetchOrders(): Promise<
PostgrestSingleResponse<Order[]> | { data: never[]; error: PostgrestError }
> {
try {
const { data: orders, error } = await supabase
.from('Order') // Update to the "Order" table
.select('*')
.single();

if (error) {
console.error('Error fetching data:', error);
return { data: [], error };
}

return orders;
} catch (error) {
console.error('Error:', error);
throw error;
}
}

export async function fetchOrderByUUID(
uuid: string,
): Promise<PostgrestSingleResponse<Order>> {
try {
const { data: order, error } = await supabase
.from('Order') // Update to the "Order" table
.select('*')
.eq('id', uuid)
.single();

if (error) {
console.error('Error fetching order data:', error);
}

return order;
} catch (error) {
console.error('Error:', error);
throw error;
}
}

export async function getOrdersByUserId(
userId: string,
): Promise<
PostgrestSingleResponse<Order[]> | { data: never[]; error: PostgrestError }
> {
try {
const { data: orders, error } = await supabase
.from('Order')
.select('*')
.eq('user_id', userId)
.single();

if (error) {
console.error('Error fetching orders:', error);
return { data: [], error };
}

return orders;
} catch (error) {
console.error('Error:', error);
throw error;
}
}

// Function to get an order by its ID
export async function getOrderById(
orderId: string,
): Promise<PostgrestSingleResponse<Order>> {
try {
const { data: order, error } = await supabase
.from('Order')
.select('*')
.eq('id', orderId)
.single();

if (error) {
console.error('Error fetching order:', error);
}

return order;
} catch (error) {
console.error('Error:', error);
throw error;
}
}

export async function toggleOrderProgress(
orderId: string,
): Promise<PostgrestSingleResponse<Order>> {
try {
// Fetch the order by ID to get its current "approved" value
const { data: currentOrder, error: fetchError } = await supabase
.from('Order')
.select('approved')
.eq('id', orderId)
.single();

if (fetchError) {
console.error('Error fetching order:', fetchError);
throw fetchError;
}

// Toggle the "approved" value
const updatedApprovedValue = !currentOrder?.approved;

// Update the order with the new "approved" value
const { data: updatedOrder, error: updateError } = await supabase
.from('Order')
.update({ approved: updatedApprovedValue })
.eq('id', orderId)
.single();

if (updateError) {
console.error('Error updating order:', updateError);
throw updateError;
}

return updatedOrder;
} catch (error) {
console.error('Error:', error);
throw error;
}
}

export async function updateAllOrdersProgressToTrue(): Promise<
boolean | string
> {
try {
// Update all orders to set "approved" to true
const { error: updateError } = await supabase
.from('Order')
.update({ approved: true });

if (updateError) {
console.error('Error updating orders:', updateError);
return 'Update failed'; // Return an error message if the update fails
}

return true; // Return true if the update succeeds
} catch (error) {
console.error('Error:', error);
return 'Update failed'; // Return an error message if an exception occurs
}
}
59 changes: 59 additions & 0 deletions src/supabase/pickup_queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-console */
//

import {
PostgrestSingleResponse,
PostgrestError,
createClient,
} from '@supabase/supabase-js';
import { Schedule } from '../schema/schema';

// Replace these with your Supabase project URL and API key
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseApiKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;

// Initialize the Supabase client
const supabase = createClient(supabaseUrl ?? '', supabaseApiKey ?? '');

export async function fetchPickupData(): Promise<
PostgrestSingleResponse<Schedule[]> | { data: never[]; error: PostgrestError }
> {
try {
const { data: pickupTimes, error } = await supabase
.from('Pickup_Times')
.select('*')
.single();

if (error) {
console.error('Error fetching data:', error);
return { data: [], error };
}

return pickupTimes;
} catch (error) {
console.error('Error:', error);
throw error;
}
}

export async function fetchPickupTimesByUUID(
uuid: string,
): Promise<PostgrestSingleResponse<unknown>> {
try {
const { data: pickupTimes, error } = await supabase
.from('Pickup_Times')
.select('*')
.eq('id', uuid)
.single();

if (error) {
console.error('Error fetching user data:', error);
}

return pickupTimes;
} catch (error) {
console.error('Error:', error);
throw error;
}
}
58 changes: 58 additions & 0 deletions src/supabase/product_queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-console */
//
import {
PostgrestSingleResponse,
PostgrestError,
createClient,
} from '@supabase/supabase-js';
import { Product } from '../schema/schema';

// Replace these with your Supabase project URL and API key
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseApiKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;

// Initialize the Supabase client
const supabase = createClient(supabaseUrl ?? '', supabaseApiKey ?? '');

export async function fetchProducts(): Promise<
PostgrestSingleResponse<Product[]> | { data: never[]; error: PostgrestError }
> {
try {
const { data: products, error } = await supabase
.from('Product')
.select('*')
.single();

if (error) {
console.error('Error fetching data:', error);
return { data: [], error };
}

return products;
} catch (error) {
console.error('Error:', error);
throw error;
}
}

export async function fetchProductByID(
productId: string,
): Promise<PostgrestSingleResponse<Product>> {
try {
const { data: product, error } = await supabase
.from('Product')
.select('*')
.eq('product_id', productId)
.single();

if (error) {
console.error('Error fetching product data:', error);
}

return product;
} catch (error) {
console.error('Error:', error);
throw error;
}
}
Loading

0 comments on commit bcf9b36

Please sign in to comment.