diff --git a/src/routes/api/all-watchlists/+server.ts b/src/routes/api/all-watchlists/+server.ts new file mode 100644 index 00000000..189df084 --- /dev/null +++ b/src/routes/api/all-watchlists/+server.ts @@ -0,0 +1,15 @@ +import type { RequestHandler } from "./$types"; + +export const GET = (async ({ locals }) => { + const { user, pb } = locals; + let output; + + try { + output = await pb.collection("watchlist").getFullList({ + filter: `user="${user?.id}"`, + }); + } catch (e) { + output = []; + } + return new Response(JSON.stringify(output)); +}) satisfies RequestHandler; diff --git a/src/routes/crypto/[tickerID]/+layout.server.ts b/src/routes/crypto/[tickerID]/+layout.server.ts index 79b4f5d2..3576cdfd 100644 --- a/src/routes/crypto/[tickerID]/+layout.server.ts +++ b/src/routes/crypto/[tickerID]/+layout.server.ts @@ -23,17 +23,16 @@ const fetchData = async (apiURL, apiKey, endpoint, ticker) => { return output; }; -const fetchWatchlist = async (fastifyURL, userId) => { - const postData = { userId: userId }; - const response = await fetch(fastifyURL + "/all-watchlists", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(postData), - }); - - const output = (await response.json())?.items; +const fetchWatchlist = async (pb, userId) => { + let output; + try { + output = await pb.collection("watchlist").getFullList({ + filter: `user="${userId}"`, + }); + } catch (e) { + //console.log(e) + output = []; + } return output; }; @@ -57,15 +56,13 @@ async function fetchPortfolio(fastifyURL, userId) */ export const load = async ({ params, locals, setHeaders }) => { - let apiURL = locals?.apiURL; - let fastifyURL = locals?.fastifyURL; - let apiKey = locals?.apiKey; + const { apiURL, apiKey, pb, user } = locals; const promises = [ fetchData(apiURL, apiKey, "/crypto-profile", params.tickerID), fetchData(apiURL, apiKey, "/stock-quote", params.tickerID), fetchData(apiURL, apiKey, "/one-day-price", params.tickerID), - fetchWatchlist(fastifyURL, locals?.user?.id), + fetchWatchlist(pb, user?.id), //fetchPortfolio(fastifyURL, locals?.user?.id) ]; diff --git a/src/routes/etf/[tickerID]/+layout.server.ts b/src/routes/etf/[tickerID]/+layout.server.ts index 0eb3c88b..2a29045c 100644 --- a/src/routes/etf/[tickerID]/+layout.server.ts +++ b/src/routes/etf/[tickerID]/+layout.server.ts @@ -30,18 +30,21 @@ const fetchData = async (apiURL, apiKey, endpoint, ticker) => { return response.json(); }; -const fetchFromFastify = async (fastifyURL, endpoint, userId) => { - const response = await fetch(`${fastifyURL}${endpoint}`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ userId }), - }); - const { items } = await response.json(); - return items; +const fetchWatchlist = async (pb, userId) => { + let output; + try { + output = await pb.collection("watchlist").getFullList({ + filter: `user="${userId}"`, + }); + } catch (e) { + //console.log(e) + output = []; + } + return output; }; export const load = async ({ params, locals, setHeaders }) => { - const { apiURL, fastifyURL, apiKey, user } = locals; + const { apiURL, apiKey, pb, user } = locals; const { tickerID } = params; const endpoints = [ @@ -59,7 +62,7 @@ export const load = async ({ params, locals, setHeaders }) => { ...endpoints.map((endpoint) => fetchData(apiURL, apiKey, endpoint, tickerID) ), - fetchFromFastify(fastifyURL, "/all-watchlists", user?.id), + fetchWatchlist(pb, user?.id), //fetchFromFastify(fastifyURL, '/get-portfolio-data', user?.id) ]; diff --git a/src/routes/stocks/[tickerID]/+layout.server.ts b/src/routes/stocks/[tickerID]/+layout.server.ts index 2cb97f2c..08e0b151 100644 --- a/src/routes/stocks/[tickerID]/+layout.server.ts +++ b/src/routes/stocks/[tickerID]/+layout.server.ts @@ -30,14 +30,17 @@ const fetchData = async (apiURL, apiKey, endpoint, ticker) => { return response.json(); }; -const fetchFromFastify = async (fastifyURL, endpoint, userId) => { - const response = await fetch(`${fastifyURL}${endpoint}`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ userId }), - }); - const { items } = await response.json(); - return items; +const fetchWatchlist = async (pb, userId) => { + let output; + try { + output = await pb.collection("watchlist").getFullList({ + filter: `user="${userId}"`, + }); + } catch (e) { + //console.log(e) + output = []; + } + return output; }; const fetchCommunitySentiment = async (pb, ticker, cookies) => { @@ -58,7 +61,7 @@ const fetchCommunitySentiment = async (pb, ticker, cookies) => { }; export const load = async ({ params, locals, cookies, setHeaders }) => { - const { apiURL, fastifyURL, apiKey, pb, user } = locals; + const { apiURL, apiKey, pb, user } = locals; const { tickerID } = params; const endpoints = [ @@ -78,7 +81,7 @@ export const load = async ({ params, locals, cookies, setHeaders }) => { ...endpoints.map((endpoint) => fetchData(apiURL, apiKey, endpoint, tickerID) ), - fetchFromFastify(fastifyURL, "/all-watchlists", user?.id), + fetchWatchlist(pb, user?.id), //fetchFromFastify(fastifyURL, '/get-portfolio-data', user?.id), fetchCommunitySentiment(pb, tickerID, cookies), ]; diff --git a/src/routes/watchlist/+page.svelte b/src/routes/watchlist/+page.svelte index 5e6c735f..a26e0961 100644 --- a/src/routes/watchlist/+page.svelte +++ b/src/routes/watchlist/+page.svelte @@ -311,17 +311,14 @@ async function deleteWatchList(event) { async function getAllListData() { - const postData = {'userId': data?.user?.id} - const response = await fetch(data?.fastifyURL+'/all-watchlists', { - method: 'POST', + const response = await fetch('/api/all-watchlists', { + method: 'GET', headers: { "Content-Type": "application/json" }, - body: JSON.stringify(postData) }); - allList = (await response.json())?.items; - console.log(allList) + allList = await response.json(); if(allList?.length !== 0) {