-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e60a337
commit e6d36e3
Showing
20 changed files
with
157 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>[]>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` }); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.