-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
13 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,23 @@ | ||
import { user } from "@/dao/user.dao"; | ||
import { NextResponse, NextRequest } from "next/server"; | ||
import { NextResponse } from "next/server"; | ||
import { z } from "zod"; | ||
|
||
const checkGetAllUsersParams = z.object({ | ||
const DEFAULT_PAGE = 1; | ||
const DEFAULT_PAGE_SIZE = 10; | ||
|
||
const ListAllUsersParamsSchema = z.object({ | ||
username: z.string().optional(), | ||
email: z.string().optional(), | ||
page: z.number().optional().transform((val) => (val ? Number(val) : undefined)), | ||
pageSize: z.number().optional().transform((val) => (val ? Number(val) : undefined)), | ||
page: z.number().default(DEFAULT_PAGE), | ||
pageSize: z.number().default(DEFAULT_PAGE_SIZE), | ||
}); | ||
|
||
export async function GET(req: NextRequest) { | ||
try { | ||
const queryParams = checkGetAllUsersParams.parse(Object.fromEntries(req.nextUrl.searchParams)); | ||
export async function GET(req) { | ||
const queryParams = ListAllUsersParamsSchema.parse(Object.fromEntries(req.nextUrl.searchParams)); | ||
|
||
const { username, email, page, pageSize } = queryParams; | ||
|
||
const { username, email, page, pageSize } = queryParams; | ||
|
||
const users = await user.fetchUsers({ username, email }, page, pageSize); | ||
const users = await user.fetchUsers({ username, email }, page, pageSize); | ||
|
||
return new NextResponse(JSON.stringify(users), { | ||
status: 200, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
return new NextResponse(JSON.stringify({ error: "Internal Server Error" }), { | ||
status: 500, | ||
}); | ||
} | ||
return NextResponse.json(users); | ||
} |