Skip to content

Implement REST API endpoints for Labs#9

Open
mateokalafatovich wants to merge 6 commits intomainfrom
task/lab-APIs
Open

Implement REST API endpoints for Labs#9
mateokalafatovich wants to merge 6 commits intomainfrom
task/lab-APIs

Conversation

@mateokalafatovich
Copy link
Copy Markdown
Collaborator

Describe your changes

[ Couple of bullet points ]

  • Created labs.ts in the services folder for use in API endpoint functions
  • Created GET and POST functions for Labs
  • Created id specific routes GET, PUT, DELETE by id for Labs
  • Updated the Lab DB Schema to allow the use of functions from services/labs.ts

Issue ticket number and link

[ Insert Link & Ticket #]

Checklist before requesting a review

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • This change requires a documentation update

Copy link
Copy Markdown
Collaborator

@arnavjk007 arnavjk007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor changes

return NextResponse.json({ message: "Invalid ID" },
{ status: 400 });
}
const item = await getLab(parsedParams.data.id);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good but let's update this to return a list of labs when we pass in user id. Because what if a user is a part of multiple labs?

Copy link
Copy Markdown
Collaborator

@arnavjk007 arnavjk007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also have a function that returns all labs but paginated so we only fetch for example 10 labs per query until end. Also, remember to add unit testing.

Copy link
Copy Markdown
Collaborator

@arnavjk007 arnavjk007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing some CRUD functions also for unit tests please send a ss of all tests passing.

await connectToDatabase();
const lab = await LabModel.findById(id).exec();
if (!lab) {
throw new Error(`Lab with ID ${id} not found`);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to return false here instead of throwing.

{ status: 500 });
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a POST function

console.error(err);
return NextResponse.json({ message: "Internal server error" }, { status: 500 });
}
} No newline at end of file
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing PUT and DELETE

if (!parsed.success) {
return NextResponse.json({ message: "Invalid data" }, { status: 400 });
}
const newLab = await addLab(parsed.data as unknown as Lab);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type casting may not work for all cases and is a bit unsafe we can just change header of addLab to this.. export async function addLab(newLab: LabInput | Omit<Lab, 'id'>): Promise {
await connectToDatabase();
const createdLab = await LabModel.create(newLab);
return toLab(createdLab);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants