1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-26 11:05:25 -04:00
jarv.is/app/api/hits/route.ts

32 lines
676 B
TypeScript

import { NextResponse } from "next/server";
import { prisma } from "../../../lib/helpers/prisma";
import type { hits as Hits } from "@prisma/client";
export const revalidate = 900; // 15 mins
export async function GET(): Promise<
NextResponse<{
total: Pick<Hits, "hits">;
pages: Hits[];
}>
> {
// fetch all rows from db sorted by most hits
const pages = await prisma.hits.findMany({
orderBy: [
{
hits: "desc",
},
],
});
const total = { hits: 0 };
// calculate total hits
pages.forEach((page) => {
// add these hits to running tally
total.hits += page.hits;
});
return NextResponse.json({ total, pages });
}