mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 02:53:50 +00:00
feat(#6): Add a basic product page using the db rather than stripe
This commit is contained in:
@@ -6,12 +6,11 @@
|
||||
let enablePagination = false;
|
||||
|
||||
onMount(async () => {
|
||||
const response = await fetch('/products');
|
||||
const response = await fetch('/products?limit=20&offset=0');
|
||||
const responseJson = await response.json();
|
||||
products = responseJson.data;
|
||||
if (responseJson.has_more) {
|
||||
enablePagination = true;
|
||||
}
|
||||
products = responseJson.products;
|
||||
var count = responseJson.count;
|
||||
// enablePagination needs to be set to true if there are more than 20 products
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { stripe as stripeClient } from '$lib/utils/stripe';
|
||||
|
||||
export const GET = async () => {
|
||||
const products = await stripeClient.products.list({
|
||||
limit: 100
|
||||
});
|
||||
import { getActiveProductsWithPrices } from '$lib/utils/supabase/admin';
|
||||
|
||||
export const GET = async ({ url }) => {
|
||||
try {
|
||||
// Get the price for each product
|
||||
for (const product of products.data) {
|
||||
const price = await stripeClient.prices.retrieve(product.default_price as string);
|
||||
if (price) {
|
||||
product.actualPrice = price.unit_amount / 100;
|
||||
}
|
||||
}
|
||||
const limitParam = url.searchParams.get('limit');
|
||||
const offsetParam = url.searchParams.get('offset');
|
||||
|
||||
const limit = limitParam ? parseInt(limitParam, 10) : 10;
|
||||
const offset = offsetParam ? parseInt(offsetParam, 10) : 0;
|
||||
|
||||
const { products, count } = await getActiveProductsWithPrices(limit, offset);
|
||||
|
||||
return json({ products, count });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return json({ error: error.message }, { status: 500 });
|
||||
}
|
||||
return json(products);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user