Conversation
+ 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
arnavjk007
left a comment
There was a problem hiding this comment.
Lots of error handling cases + GET filtering & pagination changes
app/api/inventory/[id]/route.ts
Outdated
| return NextResponse.json({ message: "Invalid id" }, { status: 400 }); | ||
| } | ||
|
|
||
| const updateData = await request.json(); |
There was a problem hiding this comment.
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(),
});
There was a problem hiding this comment.
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
arnavjk007
left a comment
There was a problem hiding this comment.
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 } }) { |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
Same thing, lets do the try catch with connectToDB before thsi try catch in all functs
There was a problem hiding this comment.
This should also be filtered like they should be able to get by item name as well
| @@ -1 +1 @@ | |||
| import { | |||
There was a problem hiding this comment.
Will also have to add indexes on item name so we can query using that for th filtered get
services/items.ts
Outdated
| // TODO ** Paginate and limit to 10 per page | ||
| // Build query with filters | ||
| export async function getItems(): Promise<Item[]> { | ||
| await connectToDatabase(); |
There was a problem hiding this comment.
remove this since it will be in the route files
services/items.ts
Outdated
| // Build query with filters | ||
| export async function getItems(): Promise<Item[]> { | ||
| await connectToDatabase(); | ||
| const items = await ItemModel.find().exec(); |
|
|
||
| return toItem(item); | ||
| } | ||
|
|
There was a problem hiding this comment.
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
arnavjk007
left a comment
There was a problem hiding this comment.
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
app/api/inventory/route.ts
Outdated
| const connectionResponse = await connect(); | ||
| if (connectionResponse) { | ||
| return connectionResponse; | ||
| } |
There was a problem hiding this comment.
Not really sure what this is doing, we only have to await connect() here...
+ Created filteredGet in services + Implemented .lean() so services return plain objects rather than documents + Proper item update is enforced with Zod schema
arnavjk007
left a comment
There was a problem hiding this comment.
connectToDatabase() should be moved to route files. I put a comment on what it shoudl look like. Also, please add unit testing that mocks DB calls to make sure our functions work. You can create a tests folder in each directory with your files like api/inventory/tests/ and in there you will have tests that test all route files.
Also you should change the name of the route.ts pathway since it is app/api/demo/rout.ts, it shouldn't be demo.
| * @returns filtered items in the form of a JS object | ||
| */ | ||
| export async function filteredGet(options: getItemOptions) { | ||
| await connectToDatabase(); |
There was a problem hiding this comment.
All DB connections should be done in route files through a try catch like this
try {
await connectToDatabase();
} catch {
return NextResponse.json(
{ success: false, message: "Error connecting to database." },
{ status: 500 }
);
}
Describe your changes
Issue ticket number and link
[ Insert Link & Ticket #]
Checklist before requesting a review