-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from calblueprint/kevin_cai/Add_schema_to_supa…
…base Kevin cai/add schema to supabase
- Loading branch information
Showing
14 changed files
with
759 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ yarn-error.log* | |
|
||
# local env files | ||
.env*.local | ||
.env | ||
|
||
# vercel | ||
.vercel | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.