Skip to content

Commit

Permalink
Set up @utils/prisma and moved everything to app/api
Browse files Browse the repository at this point in the history
  • Loading branch information
ludavidca committed Dec 27, 2024
1 parent e242737 commit 7346321
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 124 deletions.
2 changes: 1 addition & 1 deletion app/api/getAbsences/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prisma } from '../../../utils/prisma';
import { prisma } from '@utils/prisma';
import { NextResponse } from 'next/server';

export interface AbsenceWithRelations {
Expand Down
72 changes: 72 additions & 0 deletions app/api/users/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// app/api/users/[id]/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@utils/prisma';

// Helper function to validate ID
function validateId(id: string): number | null {
const parsedId = Number(id);
return Number.isNaN(parsedId) ? null : parsedId;
}

export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
const id = validateId(params.id);
if (id === null) {
return NextResponse.json({ error: 'Invalid ID provided' }, { status: 400 });
}

const searchParams = request.nextUrl.searchParams;
const getAbsences = searchParams.get('getAbsences') === 'true';

try {
const user = await prisma.user.findUnique({
where: { id },
include: { absences: getAbsences },
});

if (!user) {
return NextResponse.json({ error: 'User not found' }, { status: 404 });
}

return NextResponse.json(user);
} catch (error) {
console.error('Error fetching user:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}

export async function PATCH(
request: NextRequest,
{ params }: { params: { id: string } }
) {
const id = validateId(params.id);
if (id === null) {
return NextResponse.json({ error: 'Invalid ID provided' }, { status: 400 });
}

try {
const { email, firstName, lastName, role, status } = await request.json();

const updatedUser = await prisma.user.update({
where: { id },
data: { email, firstName, lastName, role, status },
});

if (!updatedUser) {
return NextResponse.json({ error: 'User not found' }, { status: 400 });
}

return NextResponse.json(updatedUser);
} catch (error) {
console.error('Error updating user:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
53 changes: 53 additions & 0 deletions app/api/users/email/[email]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// app/api/user/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { Prisma } from '@prisma/client';
import { prisma } from '@utils/prisma';

async function getRandomUser() {
const user = await prisma.user.findFirstOrThrow({
select: {
email: true,
},
});
return user;
}

export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams;
const shouldIncludeAbsences =
searchParams.get('shouldIncludeAbsences') === 'true';
const realEmail = searchParams.get('email');

try {
const useFake = true;
let email: string;
if (useFake) {
const randomUser = await getRandomUser();
email = randomUser.email;
} else {
email = realEmail!;
}

const user = await prisma.user.findUniqueOrThrow({
where: { email },
include: { absences: shouldIncludeAbsences },
});

return NextResponse.json(user);
} catch (error) {
if (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.code === 'P2025'
) {
return NextResponse.json(
{ error: 'Database is empty or user not found.' },
{ status: 400 }
);
}
console.error('Error fetching user:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
15 changes: 15 additions & 0 deletions app/api/users/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NextResponse } from 'next/server';
import { prisma } from '@utils/prisma';

export async function GET() {
try {
const users = await prisma.user.findMany();
return NextResponse.json(users);
} catch (error) {
console.error('Error fetching users:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
15 changes: 0 additions & 15 deletions src/pages/api/users.ts

This file was deleted.

58 changes: 0 additions & 58 deletions src/pages/api/users/[id].ts

This file was deleted.

49 changes: 0 additions & 49 deletions src/pages/api/users/email/[email].ts

This file was deleted.

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"strictNullChecks": true,
"paths": {
// enables us to write `import X from "auth"` instead of `import X from "../../auth"`
"auth": ["./auth"]
"auth": ["./auth"],
"@utils/prisma": ["./utils/prisma"]
}
},
"include": [
Expand Down

0 comments on commit 7346321

Please sign in to comment.