diff --git a/ui/app/api/chats/[id]/route.ts b/ui/app/api/chats/[id]/route.ts
new file mode 100644
index 0000000..6891454
--- /dev/null
+++ b/ui/app/api/chats/[id]/route.ts
@@ -0,0 +1,69 @@
+import db from '@/lib/db';
+import { chats, messages } from '@/lib/db/schema';
+import { eq } from 'drizzle-orm';
+
+export const GET = async (
+ req: Request,
+ { params }: { params: Promise<{ id: string }> },
+) => {
+ try {
+ const { id } = await params;
+
+ const chatExists = await db.query.chats.findFirst({
+ where: eq(chats.id, id),
+ });
+
+ if (!chatExists) {
+ return Response.json({ message: 'Chat not found' }, { status: 404 });
+ }
+
+ const chatMessages = await db.query.messages.findMany({
+ where: eq(messages.chatId, id),
+ });
+
+ return Response.json(
+ {
+ chat: chatExists,
+ messages: chatMessages,
+ },
+ { status: 200 },
+ );
+ } catch (err) {
+ console.error('Error in getting chat by id: ', err);
+ return Response.json(
+ { message: 'An error has occurred.' },
+ { status: 500 },
+ );
+ }
+};
+
+export const DELETE = async (
+ req: Request,
+ { params }: { params: Promise<{ id: string }> },
+) => {
+ try {
+ const { id } = await params;
+
+ const chatExists = await db.query.chats.findFirst({
+ where: eq(chats.id, id),
+ });
+
+ if (!chatExists) {
+ return Response.json({ message: 'Chat not found' }, { status: 404 });
+ }
+
+ await db.delete(chats).where(eq(chats.id, id)).execute();
+ await db.delete(messages).where(eq(messages.chatId, id)).execute();
+
+ return Response.json(
+ { message: 'Chat deleted successfully' },
+ { status: 200 },
+ );
+ } catch (err) {
+ console.error('Error in deleting chat by id: ', err);
+ return Response.json(
+ { message: 'An error has occurred.' },
+ { status: 500 },
+ );
+ }
+};
diff --git a/ui/app/api/chats/route.ts b/ui/app/api/chats/route.ts
new file mode 100644
index 0000000..986a192
--- /dev/null
+++ b/ui/app/api/chats/route.ts
@@ -0,0 +1,15 @@
+import db from '@/lib/db';
+
+export const GET = async (req: Request) => {
+ try {
+ let chats = await db.query.chats.findMany();
+ chats = chats.reverse();
+ return Response.json({ chats: chats }, { status: 200 });
+ } catch (err) {
+ console.error('Error in getting chats: ', err);
+ return Response.json(
+ { message: 'An error has occurred.' },
+ { status: 500 },
+ );
+ }
+};
diff --git a/ui/app/c/[chatId]/page.tsx b/ui/app/c/[chatId]/page.tsx
index dc3c92a..aac125a 100644
--- a/ui/app/c/[chatId]/page.tsx
+++ b/ui/app/c/[chatId]/page.tsx
@@ -1,7 +1,9 @@
import ChatWindow from '@/components/ChatWindow';
+import React from 'react';
-const Page = ({ params }: { params: { chatId: string } }) => {
- return ;
+const Page = ({ params }: { params: Promise<{ chatId: string }> }) => {
+ const { chatId } = React.use(params);
+ return ;
};
export default Page;
diff --git a/ui/app/library/page.tsx b/ui/app/library/page.tsx
index 379596c..9c40b2b 100644
--- a/ui/app/library/page.tsx
+++ b/ui/app/library/page.tsx
@@ -21,7 +21,7 @@ const Page = () => {
const fetchChats = async () => {
setLoading(true);
- const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/chats`, {
+ const res = await fetch(`/api/chats`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
diff --git a/ui/components/DeleteChat.tsx b/ui/components/DeleteChat.tsx
index 2857fc8..3acc9fc 100644
--- a/ui/components/DeleteChat.tsx
+++ b/ui/components/DeleteChat.tsx
@@ -29,15 +29,12 @@ const DeleteChat = ({
const handleDelete = async () => {
setLoading(true);
try {
- const res = await fetch(
- `${process.env.NEXT_PUBLIC_API_URL}/chats/${chatId}`,
- {
- method: 'DELETE',
- headers: {
- 'Content-Type': 'application/json',
- },
+ const res = await fetch(`/api/chats/${chatId}`, {
+ method: 'DELETE',
+ headers: {
+ 'Content-Type': 'application/json',
},
- );
+ });
if (res.status != 200) {
throw new Error('Failed to delete chat');