From 288adc5666826af9afcc2255c663b1b8294284c8 Mon Sep 17 00:00:00 2001 From: TZGyn Date: Tue, 19 Sep 2023 06:09:09 +0800 Subject: [PATCH] Frontend shortener table to display visitor count for each shortener --- elysia/src/index.ts | 13 ++++++++++++- react-frontend/src/App.tsx | 26 ++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/elysia/src/index.ts b/elysia/src/index.ts index 4e45484..66bbce1 100644 --- a/elysia/src/index.ts +++ b/elysia/src/index.ts @@ -11,7 +11,18 @@ app.get('/', () => 'Hello Elysia') app.get('/invalid', () => 'Invalid Shortener') app.get('/link', async () => { - const shorteners = await db.selectFrom('shortener').selectAll().execute() + const shorteners = await db + .selectFrom('shortener') + .leftJoin('visitor', 'visitor.shortener_id', 'shortener.id') + .select(({ fn }) => [ + 'shortener.id', + 'shortener.link', + 'shortener.code', + 'shortener.created_at', + fn.count('visitor.id').as('visitor_count'), + ]) + .groupBy('shortener.id') + .execute() return { shorteners } }) diff --git a/react-frontend/src/App.tsx b/react-frontend/src/App.tsx index 53861fd..fdcfe46 100644 --- a/react-frontend/src/App.tsx +++ b/react-frontend/src/App.tsx @@ -45,6 +45,7 @@ type Shortener = { id: number link: string code: string + visitor_count: string } export default function App() { @@ -87,13 +88,24 @@ export default function App() { ) } -const Navbar = ({ getShorteners }: { getShorteners: () => Promise }) => { +export const Navbar = ({ + getShorteners, +}: { + getShorteners?: () => Promise +}) => { + const navigate = useNavigate() return (
-
Shortener
+
navigate('/')}> + Shortener +
- + {getShorteners && ( + + )}
@@ -114,7 +126,7 @@ const CreateShortener = ({ }, method: 'POST', body: JSON.stringify({ - link: link, + link: link.startsWith('https://') ? link : 'https://' + link, }), }).then(() => { getShorteners() @@ -194,6 +206,9 @@ const ShortenerTable = ({ Link Shortener + + Visitors + @@ -212,6 +227,9 @@ const ShortenerTable = ({
+ + {shortener.visitor_count} +