Skip to content

Commit

Permalink
Add category and age filtering on bottle list
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed May 24, 2023
1 parent 362b8d0 commit d0c883f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
22 changes: 21 additions & 1 deletion apps/api/src/routes/listBottles.ts
Expand Up @@ -5,7 +5,7 @@ import { IncomingMessage, Server, ServerResponse } from "http";
import { z } from "zod";
import zodToJsonSchema from "zod-to-json-schema";
import { db } from "../db";
import { bottles, bottlesToDistillers, entities } from "../db/schema";
import { Category, bottles, bottlesToDistillers, entities } from "../db/schema";
import { buildPageLink } from "../lib/paging";
import { serialize } from "../lib/serializers";
import { BottleSerializer } from "../lib/serializers/bottle";
Expand All @@ -23,6 +23,18 @@ export default {
brand: { type: "number" },
distiller: { type: "number" },
entity: { type: "number" },
category: {
type: "string",
enum: [
"blend",
"bourbon",
"rye",
"single_grain",
"single_malt",
"spirit",
],
},
age: { type: "number" },
},
},
response: {
Expand Down Expand Up @@ -78,6 +90,12 @@ export default {
),
);
}
if (req.query.category) {
where.push(eq(bottles.category, req.query.category));
}
if (req.query.age) {
where.push(eq(bottles.statedAge, req.query.age));
}

let orderBy: SQL<unknown>;
switch (req.query.sort) {
Expand Down Expand Up @@ -127,6 +145,8 @@ export default {
brand?: number;
distiller?: number;
entity?: number;
category?: Category;
age?: number;
sort?: "name";
};
}
Expand Down
16 changes: 14 additions & 2 deletions apps/web/src/components/bottleTable.tsx
Expand Up @@ -93,10 +93,22 @@ export default ({
)}
</td>
<td className="hidden px-3 py-4 text-right text-sm sm:table-cell">
{formatCategoryName(bottle.category)}
{!!bottle.category && (
<Link
to={`/bottles/?category=${bottle.category}`}
className="hover:underline"
>
{formatCategoryName(bottle.category)}
</Link>
)}
</td>
<td className="hidden py-4 pl-3 pr-4 text-right text-sm sm:table-cell sm:pr-3">
{bottle.statedAge && `${bottle.statedAge} years`}
{bottle.statedAge && (
<Link
className="hover:underline"
to={`/bottles/?categoagery=${bottle.statedAge}`}
>{`${bottle.statedAge} years`}</Link>
)}
</td>
</tr>,
];
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/routes/bottles.tsx
Expand Up @@ -17,6 +17,7 @@ export default function BottleList() {
queryFn: (): Promise<Paginated<Bottle>> =>
api.get("/bottles", {
query: {
...Object.fromEntries(qs.entries()),
page,
sort: "name",
},
Expand Down

0 comments on commit d0c883f

Please sign in to comment.