diff --git a/pages/api/views/index.ts b/pages/api/views/index.ts new file mode 100644 index 0000000..bb5443a --- /dev/null +++ b/pages/api/views/index.ts @@ -0,0 +1,18 @@ +import type {NextApiRequest, NextApiResponse} from 'next' +import {prisma} from '@/lib/prisma' + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + try { + const totalViews: { _sum: { count: any } } = await prisma.views.aggregate({ + _sum: { + count: true + } + }) + + return res.status(200).json({ + views: totalViews._sum.count.toString() + }) + } catch (e: any) { + return res.status(500).json({message: e.message}) + } +} \ No newline at end of file diff --git a/pages/dashboard.tsx b/pages/dashboard.tsx index 794743c..0ae054f 100644 --- a/pages/dashboard.tsx +++ b/pages/dashboard.tsx @@ -1,9 +1,11 @@ import Head from 'next/head' +import {GetStaticProps} from 'next' +import useSWR from 'swr' import {SimpleLayout} from '@/components/SimpleLayout' import {Card} from '@/components/Card' import {numberFormat} from '@/lib/numberFormat' +import fetcher from '@/lib/fetcher' import {getTotalFollowers, getTotalRepos, getTotalStars} from '@/lib/github' -import {GetStaticProps} from 'next' type DashboardProps = { totalRepos: number @@ -12,6 +14,9 @@ type DashboardProps = { } export default function Dashboard({totalRepos, totalFollowers, totalStars}: DashboardProps) { + const {data} = useSWR('/api/views/', fetcher) + const totalArticleViews = Number(data?.views) + return ( <> @@ -62,6 +67,14 @@ export default function Dashboard({totalRepos, totalFollowers, totalStars}: Dash {numberFormat(totalStars)} + +

+ Total Article Views +

+ + {totalArticleViews > 0 ? numberFormat(totalArticleViews) : '—'} + +