Add api to get users for initial app start
This commit is contained in:
parent
28f44c35c6
commit
c8cb5d16d1
21
src/app/api/getUsers/route.ts
Normal file
21
src/app/api/getUsers/route.ts
Normal 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
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user