From a7d518460a5f3cf57fab7c22d2f477aa0d5c1a5b Mon Sep 17 00:00:00 2001 From: r-freeman Date: Sat, 28 Jan 2023 15:29:17 +0000 Subject: [PATCH] Added total article views to dashboard --- pages/api/views/index.ts | 18 ++++++++++++++++++ pages/dashboard.tsx | 15 ++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 pages/api/views/index.ts 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) : '—'} + +