From 31b96e8d6ae39fa96cdec77aa395478ab5690879 Mon Sep 17 00:00:00 2001 From: gibbyb Date: Mon, 7 Oct 2024 14:21:50 -0500 Subject: [PATCH] Added legacy APIs to deploy this website --- package.json | 3 ++- src/app/api/getCountdown/route.ts | 21 ++++++++++++++++++++ src/app/api/getMessage/route.ts | 22 +++++++++++++++++++++ src/app/api/getUsers/route.ts | 21 ++++++++++++++++++++ src/app/api/setCountdown/route.ts | 23 ++++++++++++++++++++++ src/app/api/setMessage/route.ts | 24 +++++++++++++++++++++++ src/app/api/updatePushToken/route.ts | 29 ++++++++++++++++++++++++++++ 7 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 src/app/api/getCountdown/route.ts create mode 100644 src/app/api/getMessage/route.ts create mode 100644 src/app/api/getUsers/route.ts create mode 100644 src/app/api/setCountdown/route.ts create mode 100644 src/app/api/setMessage/route.ts create mode 100644 src/app/api/updatePushToken/route.ts diff --git a/package.json b/package.json index 4a1d0cd..2abc924 100755 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "dev": "next dev", "lint": "next lint", "start": "next start", - "go": "git pull docker master && next build && next start" + "go": "git pull docker master && next dev", + "goprod": "git pull docker master && next build && next start" }, "dependencies": { "@t3-oss/env-nextjs": "^0.10.1", diff --git a/src/app/api/getCountdown/route.ts b/src/app/api/getCountdown/route.ts new file mode 100644 index 0000000..1f217dc --- /dev/null +++ b/src/app/api/getCountdown/route.ts @@ -0,0 +1,21 @@ +'use server'; +import { NextResponse } from 'next/server'; +import { getCountdown } from '~/server/functions'; + +export const GET = async (request: Request) => { + try { + const url = new URL(request.url); + const apiKey = url.searchParams.get('apiKey'); + if (apiKey !== process.env.API_KEY) { + console.log('Invalid API Key'); + return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); + } else { + const countdown = await getCountdown(); + return NextResponse.json(countdown); + } + } catch (error) { + console.error(error); + return NextResponse.json({ message: "Error" }, { status: 500 }); + } +}; +// localhost:3000/api/getCountdown?apiKey=I_Love_Madeline diff --git a/src/app/api/getMessage/route.ts b/src/app/api/getMessage/route.ts new file mode 100644 index 0000000..88efddf --- /dev/null +++ b/src/app/api/getMessage/route.ts @@ -0,0 +1,22 @@ +'use server'; +import { NextResponse } from 'next/server'; +import { getMessage } from '~/server/functions'; + +export const GET = async (request: Request) => { + try { + const url = new URL(request.url); + const apiKey = url.searchParams.get('apiKey'); + if (apiKey !== process.env.API_KEY) { + console.log('Invalid API Key'); + return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); + } else { + const userId = url.searchParams.get('userId') ?? '2'; + const message = await getMessage(parseInt(userId)); + return NextResponse.json(message); + } + } catch (error) { + console.error(error); + return NextResponse.json({ message: "Error" }, { status: 500 }); + } +}; +// localhost:3000/api/getMessage?apiKey=I_Love_Madeline&userId=2 diff --git a/src/app/api/getUsers/route.ts b/src/app/api/getUsers/route.ts new file mode 100644 index 0000000..9d08c23 --- /dev/null +++ b/src/app/api/getUsers/route.ts @@ -0,0 +1,21 @@ +'use server'; +import { NextResponse } from 'next/server'; +import { getUsers } from '~/server/functions'; + +export const GET = async (request: Request) => { + try { + const url = new URL(request.url); + const apiKey = url.searchParams.get('apiKey'); + if (apiKey !== process.env.API_KEY) { + console.log('Invalid API Key'); + return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); + } else { + const users = await getUsers(); + return NextResponse.json(users); + } + } catch (error) { + console.error(error); + return NextResponse.json({ message: "Error" }, { status: 500 }); + } +}; +// localhost:3000/api/getUsers?apiKey=I_Love_Madeline diff --git a/src/app/api/setCountdown/route.ts b/src/app/api/setCountdown/route.ts new file mode 100644 index 0000000..d82bcd0 --- /dev/null +++ b/src/app/api/setCountdown/route.ts @@ -0,0 +1,23 @@ +"use server"; +import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; +import { setCountdown } from "~/server/functions"; + +export const POST = async (request: NextRequest) => { + try { + const url = new URL(request.url); + const apiKey = url.searchParams.get("apiKey"); + if (apiKey !== process.env.API_KEY) { + console.log("Invalid API Key"); + return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); + } else { + const countdown = url.searchParams.get("countdown") ?? "2023-01-01T00:00:00.000Z"; + await setCountdown(new Date(countdown)); + return NextResponse.json({ message: "Countdown set successfully" }); + } + } catch (error) { + console.error(error); + return NextResponse.json({ message: "Error" }, { status: 500 }); + } +}; +// localhost:3000/api/setCountdown?apiKey=I_Love_Madeline&countdown=2024-09-20T12:00:00.000Z diff --git a/src/app/api/setMessage/route.ts b/src/app/api/setMessage/route.ts new file mode 100644 index 0000000..231bc2d --- /dev/null +++ b/src/app/api/setMessage/route.ts @@ -0,0 +1,24 @@ +"use server"; +import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; +import { setMessage } from "~/server/functions"; + +export const POST = async (request: NextRequest) => { + try { + const url = new URL(request.url); + const apiKey = url.searchParams.get("apiKey"); + if (apiKey !== process.env.API_KEY) { + console.log("Invalid API Key"); + return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); + } else { + const userId = url.searchParams.get("userId") ?? "2"; + const message = url.searchParams.get("message") ?? "Test"; + await setMessage(parseInt(userId), message); + return NextResponse.json({ message: "Message set successfully" }); + } + } catch (error) { + console.error(error); + return NextResponse.json({ message: "Error" }, { status: 500 }); + } +}; +// localhost:3000/api/setMessage?apiKey=I_Love_Madeline&userId=2&message=HelloWorld diff --git a/src/app/api/updatePushToken/route.ts b/src/app/api/updatePushToken/route.ts new file mode 100644 index 0000000..76f171f --- /dev/null +++ b/src/app/api/updatePushToken/route.ts @@ -0,0 +1,29 @@ +import { NextResponse } from 'next/server'; +import { updateUserPushToken } from '~/server/functions'; + +type Data = { + apiKey: string; + userId: string; + pushToken: string; +}; + +export const POST = async (request: Request) => { + try { + const { apiKey, userId, pushToken } = await request.json() as Data; + console.log('Received request:', { apiKey, userId, pushToken }); + + if (apiKey !== process.env.API_KEY) { + console.log('Invalid API Key'); + return NextResponse.json({ message: "Invalid API Key" }, { status: 401 }); + } + + console.log('Updating push token for user:', userId); + await updateUserPushToken(parseInt(userId), pushToken); + + console.log('Push token updated successfully'); + return NextResponse.json({ message: "Push token updated successfully" }); + } catch (error) { + console.error('Error in updatePushToken:', error); + return NextResponse.json({ message: "Error updating push token" }, { status: 500 }); + } +};