From 85ddb3fdb861a5f82d3f988598bb41238e5258c9 Mon Sep 17 00:00:00 2001 From: pheralb Date: Sun, 17 Dec 2023 00:19:56 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Update=20api=20routes=20+=20add?= =?UTF-8?q?=20categories=20endpoint.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/api/categories/+server.ts | 21 ++++++++++++ src/routes/api/svgs/+server.ts | 49 ++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/routes/api/categories/+server.ts diff --git a/src/routes/api/categories/+server.ts b/src/routes/api/categories/+server.ts new file mode 100644 index 0000000..84ef4ae --- /dev/null +++ b/src/routes/api/categories/+server.ts @@ -0,0 +1,21 @@ +import { json } from '@sveltejs/kit'; + +// Data: +import { svgs } from '@/data/svgs'; + +export const GET = () => { + const categories = svgs + .map((svg) => svg.category) + .filter((category, index, array) => array.indexOf(category) === index); + + // Status 200 | If limit is a number: + return json( + categories.map((category) => { + return { + category, + total: svgs.filter((svg) => svg.category === category).length + }; + }), + { status: 200 } + ); +}; diff --git a/src/routes/api/svgs/+server.ts b/src/routes/api/svgs/+server.ts index 39420c1..5bc345f 100644 --- a/src/routes/api/svgs/+server.ts +++ b/src/routes/api/svgs/+server.ts @@ -7,9 +7,13 @@ import { error, json } from '@sveltejs/kit'; import { svgsData } from '@/data'; export const GET = ({ url }: RequestEvent) => { - const getParams = url.searchParams.get('limit'); const fullUrl = url.origin ?? 'svgl.vercel.app'; + // Params: + const getLimitParams = url.searchParams.get('limit'); + const getCategoryParams = url.searchParams.get('category'); + const getSearchParams = url.searchParams.get('search'); + // Add full route to svgs: const fullRouteSvgsData: iSVG[] = svgsData.map((svg) => { if (typeof svg.route === 'object' && svg.route !== null) { @@ -30,11 +34,50 @@ export const GET = ({ url }: RequestEvent) => { }); // Status 200 | If no limit is provided, return all svgs: - if (!getParams) { + if (!getLimitParams && !getCategoryParams && !getSearchParams) { return json(fullRouteSvgsData, { status: 200 }); } - const limit = Number(getParams); + const limit = Number(getLimitParams); + const category = getCategoryParams; + + if (category) { + const categorySvgs = fullRouteSvgsData.filter((svg) => { + return svg.category === category.charAt(0).toUpperCase() + category.slice(1); + }); + + // Error 400 | If category does not exist: + if (categorySvgs.length === 0) { + error(400, { + message: 'Category does not exist.' + }); + } + + if (!getLimitParams) { + return json(categorySvgs, { status: 200 }); + } + + return json(categorySvgs.slice(0, limit), { status: 200 }); + } + + if (getSearchParams) { + const searchSvgs = fullRouteSvgsData.filter((svg) => { + return svg.title.toLowerCase().includes(getSearchParams.toLowerCase()); + }); + + // Error 400 | If search does not exist: + if (searchSvgs.length === 0) { + error(400, { + message: 'Search does not exist.' + }); + } + + if (!getLimitParams) { + return json(searchSvgs, { status: 200 }); + } + + return json(searchSvgs.slice(0, limit), { status: 200 }); + } // Error 400 | if limit is not a number: if (isNaN(limit)) {