Skip to content

Commit

Permalink
feat(api): add user endpoints (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
nganphan123 authored Nov 28, 2023
1 parent c97b1db commit 5672c77
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 33 deletions.
98 changes: 65 additions & 33 deletions app/web/src/app/api/users/route.ts
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})
}
}

0 comments on commit 5672c77

Please sign in to comment.