Skip to content

Implement REST API endpoints for Labs#9

Open
mateokalafatovich wants to merge 1 commit intomainfrom
task/lab-APIs
Open

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

Conversation

@mateokalafatovich
Copy link
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
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
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?

*/
export async function getLabs(): Promise<Lab[]> {
await connectToDatabase();
const labs = await LabModel.find().exec();
Copy link
Collaborator

Choose a reason for hiding this comment

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

lets also add .lean() and .sort({ createdAt: -1 }) to ensure labs returned will always be in same order

*/
export async function getLab(id: string): Promise<Lab | null> {
await connectToDatabase();
const lab = await LabModel.findById(id).exec();
Copy link
Collaborator

Choose a reason for hiding this comment

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

same comment as above

// Could be used accidentally or misused maliciously to get rid of important data
export async function deleteLab(id: string): Promise<boolean> {
await connectToDatabase();
const deleted = await LabModel.findByIdAndDelete(id).exec();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's try to do this format where we get the item and then check if it exists. If it doesn't then we return item not found otherwise we return it like this:

const item = await LabModel.findById(id).exec();
if (!item) {
throw new NotFoundError("Item not found");
}

await item.deleteOne();

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