Skip to content

Creating Inventory APIs#8

Open
davdwan21 wants to merge 5 commits intomainfrom
inventory-service-apis
Open

Creating Inventory APIs#8
davdwan21 wants to merge 5 commits intomainfrom
inventory-service-apis

Conversation

@davdwan21
Copy link
Collaborator

Describe your changes

  • Created given folder structure, unsure about use case - implemented dir structure similar to demo
  • Created items.ts in services for generic services similar to the demo
  • route.ts is broken up between [id] and general for cleaner API management
  • Multiple schemas to enforce typing in items - currently implements highly strict typing
  • Created general routes for GET, POST
  • Created id specific routes for GET, PUT, DELETE by id

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

+ Created given folder structure, unsure about use case
+ Created items.ts in services for generic services similar to the demo
+ route.ts mimics demo routing for cleaner API management
+ multiple schemas to enforce typing in items

Next steps:
- Create id specific routes for GET, PUT, DELETE by id
+ Added GET, PUT, DELETE by id
@davdwan21 davdwan21 self-assigned this Feb 15, 2026
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.

Lots of error handling cases + GET filtering & pagination changes

return NextResponse.json({ message: "Invalid id" }, { status: 400 });
}

const updateData = await request.json();
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 use zod schema to validate that the item to be updated is being updated with a valid schema so something like this:

const updateSchema = z.object({
name: z.string().optional(),
quantity: z.number().min(0).optional(),
threshold: z
.object({
minQuantity: z.number().min(0),
enabled: z.boolean(),
})
.optional(),
});

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How strict should the schema validation be? For example should we include all items like category and event as well?

+ Added exception handling to services and APIs
+ Adjusted typing to handle ItemInput (DB) vs Item (query)

Next steps:
- Implement pagination and search queries
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.

FilteredGet needs to be implemented so users can query by item name. Moved DB connection from services to route files. Some other nit comments


// GET: fetch single item by id
// TODO ** try catch
export async function GET(_: Request, { params }: { params: { id: string } }) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should connectToDB here instead of in services for consistency across all our services. We used a cached conn from:

import { connectToDatabase } from "@/lib/mongoose";

Put this before calling the services functions
try {
await connectToDatabase();
} catch {
return NextResponse.json(
{ success: false, message: "Error connecting to database." },
{ status: 500 }
);
}


// GET: fetch all items
// implement page, limit, labid search params
export async function GET() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same thing, lets do the try catch with connectToDB before thsi try catch in all functs

Copy link
Collaborator

Choose a reason for hiding this comment

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

This should also be filtered like they should be able to get by item name as well

@@ -1 +1 @@
import {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will also have to add indexes on item name so we can query using that for th filtered get

// TODO ** Paginate and limit to 10 per page
// Build query with filters
export async function getItems(): Promise<Item[]> {
await connectToDatabase();
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove this since it will be in the route files

// Build query with filters
export async function getItems(): Promise<Item[]> {
await connectToDatabase();
const items = await ItemModel.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.

also have .lean()


return toItem(item);
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Add get filteredItems for querying by item id + pagination

+ General route.ts no longer uses a cached connection. Also returns NextResponse upon failed connection attempt.
+ GET and POST now use new connection attempt
+ POST utilizes safeParse instead of parse to validate body with schema.
+ Model exports cleaned up and condensed.
+ Services no longer attempt to connect to database
+ Comment descriptor for each method
+ Methods no longer throw errors, now simply return null upon nonexistent item

Next steps:
- implement getFiltered service and API
- fix id specific routing
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.

Comments about usage of connect. We only have to do await connect() and dont need to return anything. Also some comments aren't addressed likek using .lean when using .exec

const connectionResponse = await connect();
if (connectionResponse) {
return connectionResponse;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not really sure what this is doing, we only have to await connect() here...

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