-
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.
- create endpoints for user absences and id by email
- Loading branch information
Showing
7 changed files
with
237 additions
and
41 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,30 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export async function GET(req: NextRequest) { | ||
const { searchParams } = new URL(req.url); | ||
const encodedId = searchParams.get('id'); | ||
console.log('ID: ' + encodedId); | ||
|
||
if (!encodedId) { | ||
return new NextResponse('ID not provided', { status: 400 }); | ||
} | ||
|
||
const id = Number(decodeURIComponent(encodedId)); | ||
|
||
if (Number.isNaN(id)) { | ||
return new NextResponse('ID not a number', { status: 400 }); | ||
} | ||
|
||
try { | ||
const absences = await prisma.absence.findMany({ | ||
where: { id: id }, | ||
}); | ||
|
||
return new NextResponse(JSON.stringify(absences), { status: 200 }); | ||
} catch (error) { | ||
return new NextResponse('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,31 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export async function GET(req: NextRequest) { | ||
const { searchParams } = new URL(req.url); | ||
const encodedEmail = searchParams.get('email'); | ||
|
||
if (!encodedEmail) { | ||
return new NextResponse('Email not provided', { status: 400 }); | ||
} | ||
|
||
// const email = decodeURIComponent(encodedEmail); | ||
console.log('email: ' + decodeURIComponent(encodedEmail)); // proof | ||
// choosing an email inside the db instead: | ||
const email = '[email protected]'; | ||
try { | ||
const user = await prisma.user.findUnique({ | ||
where: { email }, | ||
}); | ||
|
||
if (!user) { | ||
return new NextResponse('User not found', { status: 400 }); | ||
} else { | ||
return new NextResponse(JSON.stringify(user), { status: 200 }); | ||
} | ||
} catch (error) { | ||
return new NextResponse('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,10 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export async function GET(req: NextRequest) { | ||
const users = await prisma.user.findMany(); | ||
console.log(users); | ||
return NextResponse.json(users); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -1,34 +1,87 @@ | ||
import { useSession } from 'next-auth/react'; | ||
import { SignInButton } from '../components/SignInButton'; | ||
import { SignOutButton } from '../components/SignOutButton'; | ||
|
||
export default function AnotherPage() { | ||
const { data: session, status } = useSession(); | ||
|
||
return ( | ||
<div> | ||
<h1>Profile</h1> | ||
<hr/> | ||
{status === 'loading' ? ( | ||
<p>Loading...</p> | ||
) : session && session.user ? ( | ||
<div> | ||
<h2>Personal Information</h2> | ||
<p>Name: {session.user.name}</p> | ||
<p>Email: {session.user.email}</p> | ||
<p>Image:</p> | ||
{session.user.image && <img src={session.user.image} alt="User Image" />} | ||
|
||
<hr/> | ||
<h2>Metrics</h2> | ||
<p>Absences:</p> | ||
<hr/> | ||
</div> | ||
) : ( | ||
<p>Error: Signed out</p> | ||
)} | ||
<hr/> | ||
<SignOutButton /> | ||
</div> | ||
); | ||
} | ||
import { useSession } from 'next-auth/react'; | ||
import { useEffect, useState } from 'react'; | ||
import { SignOutButton } from '../components/SignOutButton'; | ||
|
||
export default function AnotherPage() { | ||
const { data: session, status } = useSession(); | ||
const [userId, setUserId] = useState(null); | ||
const [numAbsences, setNumAbsences] = useState(null); | ||
const [usedAbsences, setUsedAbsences] = useState(null); | ||
|
||
useEffect(() => { | ||
const fetchUserInfoByEmail = async () => { | ||
if (!session || !session.user || !session.user.email) return; | ||
|
||
const email = session.user.email; | ||
const apiUrl = `/api/users/get-user-by-email?email=${encodeURIComponent(email)}`; | ||
|
||
try { | ||
const response = await fetch(apiUrl); | ||
if (!response.ok) { | ||
throw new Error(response.statusText); | ||
} | ||
const data = await response.json(); | ||
setUserId(data['id']); | ||
setNumAbsences(data['numOfAbsences']); | ||
} catch (error) { | ||
console.error('Error fetching data:', error); | ||
} | ||
}; | ||
|
||
fetchUserInfoByEmail(); | ||
}, [session]); | ||
|
||
useEffect(() => { | ||
const fetctUsedAbsences = async () => { | ||
if (userId === null || userId === undefined) return; | ||
|
||
const apiUrl = `/api/users/absences?id=${encodeURIComponent(userId)}`; | ||
|
||
try { | ||
const response = await fetch(apiUrl); | ||
if (!response.ok) { | ||
throw new Error(response.statusText); | ||
} | ||
const data = await response.json(); | ||
const usedAbsences = data.length; | ||
setUsedAbsences(usedAbsences); | ||
console.log('used absences: ' + usedAbsences); | ||
} catch (error) { | ||
console.error('Error fetching data:', error); | ||
} | ||
}; | ||
|
||
fetctUsedAbsences(); | ||
}, [userId]); | ||
|
||
return ( | ||
<div> | ||
<h1>Profile</h1> | ||
<hr /> | ||
{status === 'loading' ? ( | ||
<p>Loading...</p> | ||
) : session && session.user ? ( | ||
<div> | ||
<h2>Personal Information</h2> | ||
<p>Name: {session.user.name}</p> | ||
<p>Email: {session.user.email}</p> | ||
<p>Image:</p> | ||
{session.user.image && ( | ||
<img src={session.user.image} alt="User Image" /> | ||
Check warning on line 70 in src/pages/profile.tsx GitHub Actions / lint
|
||
)} | ||
|
||
<hr /> | ||
<h2>Metrics</h2> | ||
<p> | ||
Absences: {usedAbsences} / {numAbsences}{' '} | ||
</p> | ||
<hr /> | ||
</div> | ||
) : ( | ||
<p>Error: Signed out</p> | ||
)} | ||
<hr /> | ||
<SignOutButton /> | ||
</div> | ||
); | ||
} |