Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add user endpoints #137

Merged
merged 10 commits into from
Nov 28, 2023
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})
}
}
Loading