diff --git a/src/app/api/history/route.ts b/src/app/api/history/route.ts deleted file mode 100644 index ddc7b19..0000000 --- a/src/app/api/history/route.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { getServerSession } from 'next-auth' -import { authOptions } from '~/server/auth' -import { db } from '~/server/db' -import { TypingSessionRepository } from '~/server/repositories/typingSession.repository' -import { getTypingSessionLog } from './log' -import { getBookOverview } from './overview' -import { NextRequest } from 'next/server' - -export const dynamic = 'force-dynamic' - -export async function GET(_: NextRequest) { - const session = await getServerSession(authOptions) - - if (session === null) { - return Response.json({ error: 'Unauthorized' }, { status: 401 }) - } - - const typingSessionRepository = new TypingSessionRepository(db) - - const typingSessions = await typingSessionRepository.getMany({ - userId: session.user.id, - }) - - const overview = getBookOverview(typingSessions) - - const log = typingSessions - .map(a => getTypingSessionLog(a)) - .filter(a => a.numberOfVersesTyped > 0) - - return Response.json({ data: { overview, log } }, { status: 200 }) -} diff --git a/src/app/history/getHistory.ts b/src/app/history/getHistory.ts new file mode 100644 index 0000000..d5b7db0 --- /dev/null +++ b/src/app/history/getHistory.ts @@ -0,0 +1,21 @@ +import { db } from '~/server/db' +import { TypingSessionRepository } from '~/server/repositories/typingSession.repository' +import { TypingSessionLog, getTypingSessionLog } from './log' +import { BookOverview, getBookOverview } from './overview' + +export async function getHistory( + userId: string, +): Promise<{ log: TypingSessionLog[]; overview: BookOverview[] }> { + const typingSessionRepository = new TypingSessionRepository(db) + + const typingSessions = await typingSessionRepository.getMany({ + userId, + }) + + const overview = getBookOverview(typingSessions) + + const log = typingSessions + .map(a => getTypingSessionLog(a)) + .filter(a => a.numberOfVersesTyped > 0) + return { overview, log } +} diff --git a/src/app/history/history-log.tsx b/src/app/history/history-log.tsx index 883c753..755fd0d 100644 --- a/src/app/history/history-log.tsx +++ b/src/app/history/history-log.tsx @@ -1,6 +1,6 @@ 'use client' -import { TypingSessionLog } from '../api/history/log' +import { TypingSessionLog } from './log' import { Fragment } from 'react' import { format } from 'date-fns' diff --git a/src/app/history/history-overview.tsx b/src/app/history/history-overview.tsx index ac911b9..b13f597 100644 --- a/src/app/history/history-overview.tsx +++ b/src/app/history/history-overview.tsx @@ -3,7 +3,7 @@ import * as Tooltip from '@radix-ui/react-tooltip' import clsx from 'clsx' import Link from 'next/link' -import { BookOverview } from '../api/history/overview' +import { BookOverview } from './overview' export function HistoryOverview({ overview }: { overview: BookOverview[] }) { return ( diff --git a/src/app/api/history/log.ts b/src/app/history/log.ts similarity index 100% rename from src/app/api/history/log.ts rename to src/app/history/log.ts diff --git a/src/app/api/history/overview.ts b/src/app/history/overview.ts similarity index 100% rename from src/app/api/history/overview.ts rename to src/app/history/overview.ts diff --git a/src/app/history/page.tsx b/src/app/history/page.tsx index d36f5ef..917c0fa 100644 --- a/src/app/history/page.tsx +++ b/src/app/history/page.tsx @@ -4,8 +4,7 @@ import { HistoryLog } from './history-log' import { getServerSession } from 'next-auth/next' import { redirect } from 'next/navigation' import { authOptions } from '~/server/auth' -import { fetchHistory } from '~/lib/api' - +import { getHistory } from './getHistory' export const metadata: Metadata = { title: 'Type the Word - History', description: 'History of all the passages you have typed.', @@ -18,15 +17,15 @@ export default async function History() { redirect('/') } - const history = await fetchHistory() + const { overview, log } = await getHistory(session.user.id) return ( <>