From e6d36e35c5adb47f415ff70690b1f9700486e710 Mon Sep 17 00:00:00 2001 From: Austyn Deseo Date: Fri, 27 Sep 2024 01:26:06 -0500 Subject: [PATCH] complete migration of api routes --- src/app/_models/fetchFn.ts | 3 ++ src/app/about/faction/page.tsx | 2 +- src/app/about/fc/page.tsx | 2 +- src/app/admin/fc/page.tsx | 2 +- src/app/admin/systemquery/page.tsx | 2 +- src/app/admin/systems/page.tsx | 2 +- src/app/api/_common/delete.ts | 21 ++++++++ src/app/api/_common/get.ts | 8 ++++ src/app/api/_common/post.ts | 18 +++++++ src/app/api/_common/put.ts | 22 +++++++++ src/app/api/allies/route.ts | 61 ++++-------------------- src/app/api/builds/route.ts | 10 ++-- src/app/api/fc/fc-api-utils.ts | 8 ++++ src/app/api/fc/route.ts | 13 +++++ src/app/api/joinRequests/route.ts | 25 ++++++++++ src/app/api/systems/route.ts | 13 +++++ src/app/api/systems/systems-api-utils.ts | 8 ++++ src/pages/api/fc.api.ts | 57 ---------------------- src/pages/api/joinRequests.api.ts | 30 ------------ src/pages/api/systems.api.ts | 61 ------------------------ 20 files changed, 157 insertions(+), 211 deletions(-) create mode 100644 src/app/_models/fetchFn.ts create mode 100644 src/app/api/_common/delete.ts create mode 100644 src/app/api/_common/get.ts create mode 100644 src/app/api/_common/post.ts create mode 100644 src/app/api/_common/put.ts create mode 100644 src/app/api/fc/fc-api-utils.ts create mode 100644 src/app/api/fc/route.ts create mode 100644 src/app/api/joinRequests/route.ts create mode 100644 src/app/api/systems/route.ts create mode 100644 src/app/api/systems/systems-api-utils.ts delete mode 100644 src/pages/api/fc.api.ts delete mode 100644 src/pages/api/joinRequests.api.ts delete mode 100644 src/pages/api/systems.api.ts diff --git a/src/app/_models/fetchFn.ts b/src/app/_models/fetchFn.ts new file mode 100644 index 0000000..a6671fe --- /dev/null +++ b/src/app/_models/fetchFn.ts @@ -0,0 +1,3 @@ +import { WithStringId } from '@/utils/db'; + +export type FetchFn = () => Promise[]>; diff --git a/src/app/about/faction/page.tsx b/src/app/about/faction/page.tsx index fae4946..76f336b 100644 --- a/src/app/about/faction/page.tsx +++ b/src/app/about/faction/page.tsx @@ -1,4 +1,4 @@ -import { getSystems } from '#/systems.api'; +import { getSystems } from '@/app/api/systems/route'; import { buildInaraLink } from '@/functions/buildInaraLink'; import { Container, diff --git a/src/app/about/fc/page.tsx b/src/app/about/fc/page.tsx index 8a35172..b5434b7 100644 --- a/src/app/about/fc/page.tsx +++ b/src/app/about/fc/page.tsx @@ -1,4 +1,4 @@ -import { getFCs } from '#/fc.api'; +import { getFCs } from '@/app/api/fc/fc-api-utils'; import { genericSortArray } from '@/functions/sort'; import { Container, Typography } from '@mui/material'; import { Metadata } from 'next'; diff --git a/src/app/admin/fc/page.tsx b/src/app/admin/fc/page.tsx index 9e68c61..1a0b43c 100644 --- a/src/app/admin/fc/page.tsx +++ b/src/app/admin/fc/page.tsx @@ -1,4 +1,4 @@ -import { getFCs } from '#/fc.api'; +import { getFCs } from '@/app/api/fc/fc-api-utils'; import { CarriersDashboard } from '@/app/admin/fc/_components/carriersDashboard'; import { runAdminAuthCheck } from '@/utils/runAuthCheck'; import { Metadata } from 'next'; diff --git a/src/app/admin/systemquery/page.tsx b/src/app/admin/systemquery/page.tsx index 2a1446c..adcedbb 100644 --- a/src/app/admin/systemquery/page.tsx +++ b/src/app/admin/systemquery/page.tsx @@ -1,4 +1,4 @@ -import { getSystems } from '#/systems.api'; +import { getSystems } from '@/app/api/systems/route'; import { runAdminAuthCheck } from '@/utils/runAuthCheck'; import { SystemDashboard } from '@/app/admin/systems/_components/systemDashboard'; import { Metadata } from 'next'; diff --git a/src/app/admin/systems/page.tsx b/src/app/admin/systems/page.tsx index bac7078..86683eb 100644 --- a/src/app/admin/systems/page.tsx +++ b/src/app/admin/systems/page.tsx @@ -1,4 +1,4 @@ -import { getSystems } from '#/systems.api'; +import { getSystems } from '@/app/api/systems/route'; import { runAdminAuthCheck } from '@/utils/runAuthCheck'; import { SystemDashboard } from '@/app/admin/systems/_components/systemDashboard'; import { Metadata } from 'next'; diff --git a/src/app/api/_common/delete.ts b/src/app/api/_common/delete.ts new file mode 100644 index 0000000..1208d8f --- /dev/null +++ b/src/app/api/_common/delete.ts @@ -0,0 +1,21 @@ +import { deleteItem } from '@/utils/db'; +import { getIsHC } from '@/utils/get-isHC'; +import { NextRequest } from 'next/server'; + +export function generateDelete(collection: string) { + return async (request: NextRequest) => { + const isHC = await getIsHC(); + + if (!isHC) { + return new Response(null, { status: 403 }); + } + const item = request.nextUrl.searchParams.get('id'); + if (!item) { + return new Response(null, { status: 400, statusText: `Id not provided` }); + } + + await deleteItem(collection, item); + + return new Response(null, { status: 200 }); + }; +} diff --git a/src/app/api/_common/get.ts b/src/app/api/_common/get.ts new file mode 100644 index 0000000..e3f44b5 --- /dev/null +++ b/src/app/api/_common/get.ts @@ -0,0 +1,8 @@ +import { FetchFn } from '@/app/_models/fetchFn'; + +export function generateGet(fetchFn: FetchFn) { + return async () => { + const result = await fetchFn(); + return Response.json(result, { status: 200 }); + }; +} diff --git a/src/app/api/_common/post.ts b/src/app/api/_common/post.ts new file mode 100644 index 0000000..b70ed19 --- /dev/null +++ b/src/app/api/_common/post.ts @@ -0,0 +1,18 @@ +import { insertItem } from '@/utils/db'; +import { getIsHC } from '@/utils/get-isHC'; +import { WithId } from 'mongodb'; + +export function generatePost(collection: string) { + return async (request: Request) => { + const isHC = await getIsHC(); + + if (!isHC) { + return new Response(null, { status: 403 }); + } + const item: WithId = JSON.parse(await request.json()); + + await insertItem(collection, item); + + return new Response(null, { status: 200 }); + }; +} diff --git a/src/app/api/_common/put.ts b/src/app/api/_common/put.ts new file mode 100644 index 0000000..555c06b --- /dev/null +++ b/src/app/api/_common/put.ts @@ -0,0 +1,22 @@ +import { updateItem } from '@/utils/db'; +import { getIsHC } from '@/utils/get-isHC'; +import { WithId } from 'mongodb'; + +export function generatePut(collection: string) { + return async (request: Request) => { + const isHC = await getIsHC(); + + if (!isHC) { + return new Response(null, { status: 403 }); + } + const item: WithId = JSON.parse(await request.json()); + + const updateResult = await updateItem(collection, item); + + if (updateResult) { + return new Response(null, { status: 200 }); + } else { + return new Response(null, { status: 500, statusText: `Failed to update id: ${item._id}` }); + } + }; +} diff --git a/src/app/api/allies/route.ts b/src/app/api/allies/route.ts index bac9dcc..b1aa1fe 100644 --- a/src/app/api/allies/route.ts +++ b/src/app/api/allies/route.ts @@ -1,56 +1,13 @@ import { IAlly } from '@/app/about/_models/ally'; -import { deleteItem, insertItem, updateItem } from '@/utils/db'; -import { getIsHC } from '@/utils/get-isHC'; -import { NextRequest } from 'next/server'; +import { generateDelete } from '../_common/delete'; +import { generateGet } from '../_common/get'; +import { generatePost } from '../_common/post'; +import { generatePut } from '../_common/put'; import { COLLECTION, getAllies } from './allies-api-utils'; -export async function GET() { - const result = await getAllies(); +const GET = generateGet(getAllies); +const POST = generatePost(COLLECTION); +const PUT = generatePut(COLLECTION); +const DELETE = generateDelete(COLLECTION); - return Response.json(result, { status: 200 }); -} - -export async function POST(request: Request) { - const isHC = await getIsHC(); - - if (!isHC) { - return new Response(null, { status: 403 }); - } - const ally: IAlly = JSON.parse(await request.json()); - - await insertItem(COLLECTION, ally); - - return new Response(null, { status: 200 }); -} -export async function PUT(request: Request) { - const isHC = await getIsHC(); - - if (!isHC) { - return new Response(null, { status: 403 }); - } - const ally: IAlly = JSON.parse(await request.json()); - - const updateResult = await updateItem(COLLECTION, ally); - - if (updateResult) { - return new Response(null, { status: 200 }); - } else { - return new Response(null, { status: 500, statusText: `Failed to update id: ${ally._id}` }); - } -} - -export async function DELETE(request: NextRequest) { - const isHC = await getIsHC(); - - if (!isHC) { - return new Response(null, { status: 403 }); - } - const ally = request.nextUrl.searchParams.get('id'); - if (!ally) { - return new Response(null, { status: 400, statusText: `Id not provided` }); - } - - await deleteItem(COLLECTION, ally); - - return new Response(null, { status: 200 }); -} +export { DELETE, GET, POST, PUT }; diff --git a/src/app/api/builds/route.ts b/src/app/api/builds/route.ts index 83b9dc5..4834e7a 100644 --- a/src/app/api/builds/route.ts +++ b/src/app/api/builds/route.ts @@ -13,13 +13,11 @@ import { getUserId } from '@/utils/get-userId'; import { Filter } from 'mongodb'; import { NextRequest } from 'next/server'; import { getBuilds } from './getBuilds'; +import { generateGet } from '../_common/get'; const COLLECTION = 'shipBuildsv2'; -export async function GET() { - const result = await getBuilds(); - return Response.json(result); -} +export const GET = generateGet(getBuilds); export async function POST(request: Request) { const build: IBuildInfov2 = JSON.parse(await request.json()); @@ -36,7 +34,7 @@ export async function POST(request: Request) { export async function PUT(request: Request) { const updateBuild: IBuildInfov2 = JSON.parse(await request.json()); const userId = await getUserId(request); - const isHC = await getIsHC(request); + const isHC = await getIsHC(); if (updateBuild.title) { const authorId = (updateBuild.authorId as string) ?? ''; @@ -66,7 +64,7 @@ export async function PUT(request: Request) { export async function DELETE(request: NextRequest) { const userId = await getUserId(request); - const isHC = await getIsHC(request); + const isHC = await getIsHC(); const authorId = request.nextUrl.searchParams.get('authorId') ?? ''; if (authorId !== userId && !isHC) { diff --git a/src/app/api/fc/fc-api-utils.ts b/src/app/api/fc/fc-api-utils.ts new file mode 100644 index 0000000..9cd4519 --- /dev/null +++ b/src/app/api/fc/fc-api-utils.ts @@ -0,0 +1,8 @@ +import { IFleetCarrier } from '@/app/about/_models/fleetCarrier'; +import { getItems } from '@/utils/db'; + +export const COLLECTION = 'fleetCarriers'; +export const getFCs = async () => { + const items = await getItems(COLLECTION, 'name', 1); + return items; +}; diff --git a/src/app/api/fc/route.ts b/src/app/api/fc/route.ts new file mode 100644 index 0000000..4b01be4 --- /dev/null +++ b/src/app/api/fc/route.ts @@ -0,0 +1,13 @@ +import { IFleetCarrier } from '@/app/about/_models/fleetCarrier'; +import { generateDelete } from '../_common/delete'; +import { generateGet } from '../_common/get'; +import { generatePost } from '../_common/post'; +import { generatePut } from '../_common/put'; +import { COLLECTION, getFCs } from './fc-api-utils'; + +const GET = generateGet(getFCs); +const POST = generatePost(COLLECTION); +const PUT = generatePut(COLLECTION); +const DELETE = generateDelete(COLLECTION); + +export { DELETE, GET, POST, PUT }; diff --git a/src/app/api/joinRequests/route.ts b/src/app/api/joinRequests/route.ts new file mode 100644 index 0000000..a3253be --- /dev/null +++ b/src/app/api/joinRequests/route.ts @@ -0,0 +1,25 @@ +import { IJoinRequest } from '@/app/join/_models/joinRequest'; +import { getItems, insertItem } from '@/utils/db'; +import { getIsHC } from '@/utils/get-isHC'; + +const COLLECTION = 'joinRequests'; + +export async function GET() { + const isHC = await getIsHC(); + + if (!isHC) { + return new Response(null, { status: 403 }); + } + + const result = await getItems(COLLECTION, 'timeStamp', -1); + + return Response.json(result); +} + +export async function POST(request: Request) { + const joinInfo: IJoinRequest = JSON.parse(await request.json()); + joinInfo.timeStamp = new Date(joinInfo.timeStamp ?? new Date()); + await insertItem(COLLECTION, joinInfo); + + return new Response(null, { status: 201 }); +} diff --git a/src/app/api/systems/route.ts b/src/app/api/systems/route.ts new file mode 100644 index 0000000..ad54dc6 --- /dev/null +++ b/src/app/api/systems/route.ts @@ -0,0 +1,13 @@ +import { System } from '@/app/about/_models/system'; +import { generateDelete } from '../_common/delete'; +import { generateGet } from '../_common/get'; +import { generatePost } from '../_common/post'; +import { generatePut } from '../_common/put'; +import { COLLECTION, getSystems } from './systems-api-utils'; + +const GET = generateGet(getSystems); +const POST = generatePost(COLLECTION); +const PUT = generatePut(COLLECTION); +const DELETE = generateDelete(COLLECTION); + +export { DELETE, GET, POST, PUT }; diff --git a/src/app/api/systems/systems-api-utils.ts b/src/app/api/systems/systems-api-utils.ts new file mode 100644 index 0000000..4f41669 --- /dev/null +++ b/src/app/api/systems/systems-api-utils.ts @@ -0,0 +1,8 @@ +import { System } from '@/app/about/_models/system'; +import { getItems } from '@/utils/db'; + +export const COLLECTION = 'systems'; +export const getSystems = async () => { + const items = await getItems(COLLECTION, 'name', 1); + return items; +}; diff --git a/src/pages/api/fc.api.ts b/src/pages/api/fc.api.ts deleted file mode 100644 index caf2ed8..0000000 --- a/src/pages/api/fc.api.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { deleteItem, getItems, insertItem, updateItem } from '@/utils/db'; -import { getIsHC } from '@/utils/get-isHC'; -import { IFleetCarrier } from '@/app/about/_models/fleetCarrier'; -import { NextApiRequest, NextApiResponse } from 'next'; - -const COLLECTION = 'fleetCarriers'; -export default async (req: NextApiRequest, res: NextApiResponse) => { - try { - const isHC = await getIsHC(req); - - const carrier: IFleetCarrier = req.body; - - switch (req.method) { - case 'POST': - if (!isHC) { - res.status(401).send('unauthorized'); - return; - } - - await insertItem(COLLECTION, carrier); - - res.status(200).end(); - break; - case 'PUT': - if (!isHC) { - res.status(401).send('unauthorized'); - return; - } - - const updateResult = await updateItem(COLLECTION, carrier); - - if (updateResult) res.status(200).end(); - else res.status(500).send(`Failed to update id: ${req.body._id}`); - break; - case 'DELETE': - if (!isHC) { - res.status(401).send('unauthorized'); - return; - } - await deleteItem(COLLECTION, req.query['id'] as string); - res.status(200).end(); - break; - case 'GET': - default: - const results = await getFCs(); - - res.status(200).send(results); - } - } catch (e) { - res.status(500).send(e.message); - } -}; - -export const getFCs = async () => { - const items = await getItems(COLLECTION, 'name', 1); - return items; -}; diff --git a/src/pages/api/joinRequests.api.ts b/src/pages/api/joinRequests.api.ts deleted file mode 100644 index 49843ee..0000000 --- a/src/pages/api/joinRequests.api.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { insertItem, getItems } from '@/utils/db'; -import { getIsHC } from '@/utils/get-isHC'; -import { IJoinRequest } from '@/app/join/_models/joinRequest'; -import { NextApiRequest, NextApiResponse } from 'next'; - -const COLLECTION = 'joinRequests'; -export default async (req: NextApiRequest, res: NextApiResponse) => { - try { - const isHC = await getIsHC(req); - - const joinInfo: IJoinRequest = req.body; - - if (req.method === 'POST') { - joinInfo.timeStamp = new Date(joinInfo.timeStamp); - await insertItem(COLLECTION, joinInfo); - - res.status(200).end(); - } else { - if (!isHC) { - res.status(401).send('unauthorized'); - return; - } - - const result = await getItems(COLLECTION, 'timeStamp', -1); - res.status(200).send(result); - } - } catch (e) { - res.status(500).send(e.message); - } -}; diff --git a/src/pages/api/systems.api.ts b/src/pages/api/systems.api.ts deleted file mode 100644 index 9428a49..0000000 --- a/src/pages/api/systems.api.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { deleteItem, getItems, insertItem, updateItem } from '@/utils/db'; -import { getIsHC } from '@/utils/get-isHC'; -import { System } from '@/app/about/_models/system'; -import { NextApiRequest, NextApiResponse } from 'next'; - -const COLLECTION = 'systems'; - -export default async (req: NextApiRequest, res: NextApiResponse) => { - try { - const isHC = await getIsHC(req); - - const system: System = req.body; - - switch (req.method) { - case 'POST': - if (!isHC) { - res.status(401).send('unauthorized'); - return; - } - await insertItem(COLLECTION, system); - - res.status(200).end(); - break; - case 'PUT': - if (!isHC) { - res.status(401).send('unauthorized'); - return; - } - - const updateResult = await updateItem(COLLECTION, system); - if (updateResult) res.status(200).end(); - else res.status(500).send(`Failed to update id: ${req.body._id}`); - - break; - case 'DELETE': - if (!isHC) { - res.status(401).send('unauthorized'); - return; - } - - await deleteItem(COLLECTION, req.query['id'] as string); - - res.status(200).end(); - break; - case 'GET': - default: - const result = await getSystems(); - - res.status(200).send(result); - break; - } - } catch (e) { - console.error(e); - res.status(500).send(e.message); - } -}; - -export const getSystems = async () => { - const items = await getItems(COLLECTION, 'name', 1); - return items; -};