Add api to get users for initial app start

This commit is contained in:
Gabriel Brown 2024-09-09 23:03:48 -05:00
parent 28f44c35c6
commit c8cb5d16d1
2 changed files with 35 additions and 0 deletions

View File

@ -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

View File

@ -3,6 +3,20 @@ import { db } from '~/server/db';
import * as schema from '~/server/db/schema';
import { eq } from 'drizzle-orm';
export const getUsers = async () => {
try {
const result = await db.select({
id: schema.users.id,
name: schema.users.name,
message: schema.users.message,
}).from(schema.users);
return result;
} catch (error) {
console.error("Error fetching users", error);
throw new Error("Failed to fetch users");
}
};
export const getMessage = async (userId: number) => {
try {
let message = 1;