Skip to content

Commit

Permalink
complete migration of api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Admiralfeb committed Sep 27, 2024
1 parent e60a337 commit e6d36e3
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 211 deletions.
3 changes: 3 additions & 0 deletions src/app/_models/fetchFn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { WithStringId } from '@/utils/db';

export type FetchFn<T> = () => Promise<WithStringId<T>[]>;
2 changes: 1 addition & 1 deletion src/app/about/faction/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSystems } from '#/systems.api';
import { getSystems } from '@/app/api/systems/route';
import { buildInaraLink } from '@/functions/buildInaraLink';
import {
Container,
Expand Down
2 changes: 1 addition & 1 deletion src/app/about/fc/page.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/app/admin/fc/page.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/app/admin/systemquery/page.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/app/admin/systems/page.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
21 changes: 21 additions & 0 deletions src/app/api/_common/delete.ts
Original file line number Diff line number Diff line change
@@ -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 });
};
}
8 changes: 8 additions & 0 deletions src/app/api/_common/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FetchFn } from '@/app/_models/fetchFn';

export function generateGet<T>(fetchFn: FetchFn<T>) {
return async () => {
const result = await fetchFn();
return Response.json(result, { status: 200 });
};
}
18 changes: 18 additions & 0 deletions src/app/api/_common/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { insertItem } from '@/utils/db';
import { getIsHC } from '@/utils/get-isHC';
import { WithId } from 'mongodb';

export function generatePost<T extends { _id: string }>(collection: string) {
return async (request: Request) => {
const isHC = await getIsHC();

if (!isHC) {
return new Response(null, { status: 403 });
}
const item: WithId<T> = JSON.parse(await request.json());

await insertItem(collection, item);

return new Response(null, { status: 200 });
};
}
22 changes: 22 additions & 0 deletions src/app/api/_common/put.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { updateItem } from '@/utils/db';
import { getIsHC } from '@/utils/get-isHC';
import { WithId } from 'mongodb';

export function generatePut<T extends { _id: string }>(collection: string) {
return async (request: Request) => {
const isHC = await getIsHC();

if (!isHC) {
return new Response(null, { status: 403 });
}
const item: WithId<T> = 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}` });
}
};
}
61 changes: 9 additions & 52 deletions src/app/api/allies/route.ts
Original file line number Diff line number Diff line change
@@ -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<IAlly>(COLLECTION);
const PUT = generatePut<IAlly>(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 };
10 changes: 4 additions & 6 deletions src/app/api/builds/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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) ?? '';
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions src/app/api/fc/fc-api-utils.ts
Original file line number Diff line number Diff line change
@@ -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<IFleetCarrier>(COLLECTION, 'name', 1);
return items;
};
13 changes: 13 additions & 0 deletions src/app/api/fc/route.ts
Original file line number Diff line number Diff line change
@@ -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<IFleetCarrier>(COLLECTION);
const PUT = generatePut<IFleetCarrier>(COLLECTION);
const DELETE = generateDelete(COLLECTION);

export { DELETE, GET, POST, PUT };
25 changes: 25 additions & 0 deletions src/app/api/joinRequests/route.ts
Original file line number Diff line number Diff line change
@@ -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<IJoinRequest>(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 });
}
13 changes: 13 additions & 0 deletions src/app/api/systems/route.ts
Original file line number Diff line number Diff line change
@@ -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<System>(COLLECTION);
const PUT = generatePut<System>(COLLECTION);
const DELETE = generateDelete(COLLECTION);

export { DELETE, GET, POST, PUT };
8 changes: 8 additions & 0 deletions src/app/api/systems/systems-api-utils.ts
Original file line number Diff line number Diff line change
@@ -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<System>(COLLECTION, 'name', 1);
return items;
};
57 changes: 0 additions & 57 deletions src/pages/api/fc.api.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/pages/api/joinRequests.api.ts

This file was deleted.

Loading

0 comments on commit e6d36e3

Please sign in to comment.