-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up @utils/prisma and moved everything to app/api
- Loading branch information
Showing
8 changed files
with
143 additions
and
124 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
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,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 } | ||
); | ||
} | ||
} |
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,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 } | ||
); | ||
} | ||
} |
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,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 } | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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