Skip to content

Commit

Permalink
feat: add freeze entry metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelblijleven committed Oct 23, 2023
1 parent bfc534a commit 35a47c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/app/dashboard/components/stats-card-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export async function StatsCardSection() {
value={metrics.beansCount}
href={"/coffee"}
/>
<StatsCard
title={"Freezer"}
subtitle={"Frozen entries"}
value={metrics.freezerCount}
href={"/coffee/freeze"}
/>
<StatsCard
title={"Roasters"}
subtitle={"Unique roasters"}
Expand Down Expand Up @@ -62,6 +68,7 @@ export function StatsCardSectionSkeleton() {
<StatsCardSkeleton />
<StatsCardSkeleton />
<StatsCardSkeleton />
<StatsCardSkeleton />
</Layout>
)
}
9 changes: 8 additions & 1 deletion src/lib/db/get-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import {cache} from "react";
import {eq, sql} from "drizzle-orm";

import {db} from "@/db";
import {beans, cafeBrews, roasters} from "@/db/schema";
import {beans, cafeBrews, freezeEntries, roasters} from "@/db/schema";

type Metrics = {
roasterCount: number;
beansCount: number;
freezerCount: number;
cafeBrewsCount: number;
}

Expand All @@ -16,6 +17,7 @@ export const getMetrics = cache(async (userId: number): Promise<Metrics> => {
if (!userId) return {
roasterCount: 0,
beansCount: 0,
freezerCount: 0,
cafeBrewsCount: 0,
}

Expand All @@ -31,10 +33,15 @@ export const getMetrics = cache(async (userId: number): Promise<Metrics> => {
.select({count: sql<number>`count(*)`})
.from(cafeBrews)
.where(eq(cafeBrews.userId, userId))
const [freezer] = await db
.select({count: sql<number>`count(*)`})
.from(freezeEntries)
.where(eq(freezeEntries.userId, userId))

return {
roasterCount: roaster.count,
beansCount: bean.count,
freezerCount: freezer.count,
cafeBrewsCount: cafeBrew.count,
};
});

0 comments on commit 35a47c2

Please sign in to comment.