Skip to content

Commit

Permalink
Refactor and clean up make queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Ru Chern Chong committed Jun 23, 2024
1 parent ca316a4 commit 6d58bc3
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ app.get("/make", async (c) => {

app.get("/make/:make", async (c) => {
const make = c.req.param("make");
const vehicleType = c.req.query("vehicleType");
const { month, fuelType, vehicleType } = c.req.query();

const filter = {
...(make && { make: new RegExp(make, "i") }),
...(month && { month }),
...(fuelType && { fuel_type: new RegExp(`^${fuelType}$`, "i") }),
...(vehicleType && { vehicle_type: new RegExp(vehicleType, "i") }),
};

return c.json(
await db
.collection<Car>("cars")
.find({
make: new RegExp(make, "i"),
vehicle_type: new RegExp(vehicleType, "i"),
})
.find(filter)
.sort({ month: -1, fuel_type: 1, vehicle_type: 1 })
.toArray(),
);
Expand Down

0 comments on commit 6d58bc3

Please sign in to comment.