-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from TEDx-SJEC/bug_fix
Bug fix
- Loading branch information
Showing
5 changed files
with
107 additions
and
96 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
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,49 +1,57 @@ | ||
import { getServerSideSession } from "@/lib/get-server-session"; | ||
import prisma from "@/server/db"; | ||
import { NextResponse } from "next/server"; | ||
export const dynamic = "force-dynamic"; | ||
|
||
export async function GET(req: Request) { | ||
const { searchParams } = new URL(req.url); | ||
try { | ||
const page = Math.max(1, parseInt(searchParams.get("page") || "1", 10)); | ||
const search = searchParams.get("search") || ""; | ||
const limit = 10; | ||
const session = await getServerSideSession(); | ||
if (!session) { | ||
return NextResponse.json({ message: "Unauthorized" }, { status: 401 }); | ||
} | ||
|
||
if (session.user?.role !== "ADMIN") { | ||
return NextResponse.json({ error: "Unauthorized" }, { status: 403 }); | ||
} | ||
|
||
const { searchParams } = new URL(req.url); | ||
try { | ||
const page = Math.max(1, parseInt(searchParams.get("page") || "1", 10)); | ||
const search = searchParams.get("search") || ""; | ||
const limit = 10; | ||
|
||
const [users, totalCount] = await Promise.all([ | ||
prisma.user.findMany({ | ||
skip: (page - 1) * limit, | ||
take: limit, | ||
where: { | ||
name: { | ||
contains: search, | ||
}, | ||
}, | ||
}), | ||
prisma.user.count({ | ||
// Get the total number of users for pagination | ||
where: { | ||
name: { | ||
contains: search, | ||
}, | ||
}, | ||
}), | ||
]); | ||
const [users, totalCount] = await Promise.all([ | ||
prisma.user.findMany({ | ||
skip: (page - 1) * limit, | ||
take: limit, | ||
where: { | ||
name: { | ||
contains: search, | ||
}, | ||
}, | ||
}), | ||
prisma.user.count({ | ||
// Get the total number of users for pagination | ||
where: { | ||
name: { | ||
contains: search, | ||
}, | ||
}, | ||
}), | ||
]); | ||
|
||
const totalPages = Math.ceil(totalCount / limit); | ||
const totalPages = Math.ceil(totalCount / limit); | ||
|
||
return NextResponse.json({ | ||
users, | ||
pagination: { | ||
currentPage: page, | ||
totalPages, | ||
totalCount, | ||
limit, | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error("Failed to fetch users:", error); | ||
return NextResponse.json( | ||
{ message: "Failed to fetch users", status: 500 }, | ||
{ status: 500 }, | ||
); | ||
} | ||
return NextResponse.json({ | ||
users, | ||
pagination: { | ||
currentPage: page, | ||
totalPages, | ||
totalCount, | ||
limit, | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error("Failed to fetch users:", error); | ||
return NextResponse.json({ message: "Failed to fetch users", status: 500 }, { 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
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