-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add user endpoints (#137)
- Loading branch information
1 parent
c97b1db
commit 5672c77
Showing
2 changed files
with
65 additions
and
33 deletions.
There are no files selected for viewing
File renamed without changes.
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,42 +1,74 @@ | ||
import db from "@lib/db" | ||
import { JSONResponse } from "@lib/json"; | ||
import { JsonObject } from "@prisma/client/runtime/library" | ||
import { NextRequest, NextResponse } from "next/server" | ||
|
||
const userQuery = () => db.user.findMany() | ||
async function getUsersData(){ | ||
let users = await userQuery(); | ||
return users; | ||
} | ||
|
||
export async function POST(req: Request) { | ||
await db.user.createMany({ | ||
data: [ | ||
{ | ||
// mock data | ||
username: "johhn", | ||
firstname: "John", | ||
lastname: "Doe", | ||
email: "john.doe@gmail.com", | ||
role: "CLIENT", | ||
password: "password" | ||
} | ||
] | ||
}); | ||
const res: JSONResponse = { data: { success: true } }; | ||
return Response.json(res); | ||
// GET api/users?ids=id1,id2 | ||
/* | ||
The user list returned is filtered based on ids. | ||
If id param is not provided, the api will return all users. | ||
*/ | ||
const getusrDatQuery = (ids?: number[]) => db.user.findMany({ | ||
where: { | ||
id: { | ||
in: ids || undefined | ||
} | ||
}, | ||
// to filter out password field, prisma still working on a better way for this | ||
// https://github.com/prisma/prisma/issues/5042 | ||
select: { | ||
email: true, | ||
firstname: true, | ||
lastname: true, | ||
username: true, | ||
role: true, | ||
}, | ||
}) | ||
|
||
async function getUsrData(ids?: number[]) { | ||
let data = await getusrDatQuery(ids) | ||
return {result: {data}} as const | ||
} | ||
|
||
export type UsersDataResponse = { | ||
status: number, | ||
result: { | ||
data: any | ||
export async function GET(req: NextRequest){ | ||
const searchParams = req.nextUrl.searchParams | ||
const param = searchParams.get('id') | ||
let usrIds: number[] | undefined = undefined | ||
if (param !== null) { | ||
usrIds = paramToIntList(param!) | ||
} | ||
try{ | ||
let result = await getUsrData(usrIds); | ||
return NextResponse.json(result,{status: 200}) | ||
}catch(e){ | ||
return NextResponse.json({message: e}, {status: 400}) | ||
} | ||
} | ||
|
||
export async function GET(req: Request){ | ||
let users = await getUsersData(); | ||
const res: JSONResponse = {data: users} | ||
return Response.json({ | ||
status: 200, | ||
result: res | ||
} as UsersDataResponse); | ||
function paramToIntList(param: string){ | ||
let stringVals = param.split(",") | ||
return stringVals.map(v => Number(v)) | ||
} | ||
|
||
// PUT api/users?id=1 | ||
// UserId is required in parameters | ||
const updateUsrDatQuery = (id: number, data: JsonObject) => db.user.update({ | ||
where:{ | ||
id: id | ||
}, | ||
data: data | ||
}) | ||
export async function PUT(req: NextRequest) { | ||
const searchParams = req.nextUrl.searchParams | ||
const param = searchParams.get('id') | ||
if (param === null) { | ||
return NextResponse.json({message: "Missing user id"}, {status: 400}); | ||
} | ||
try{ | ||
const data = await req.json() | ||
let result = await updateUsrDatQuery(Number(param), data); | ||
return NextResponse.json(result, {status: 200}) | ||
}catch(e){ | ||
return NextResponse.json({message: e}, {status: 400}) | ||
} | ||
} |