From f71e639d492bc67f92ae08b6aab8f5a4fb89dc4f Mon Sep 17 00:00:00 2001 From: PrimarchPaul Date: Sat, 27 Sep 2025 04:15:51 -0400 Subject: [PATCH] fix: app.route overload --- concord-server/src/controller/actions.ts | 6 ++ concord-server/src/index.ts | 11 +-- concord-server/src/routes/actions.ts | 17 ++++- concord-server/src/services/actions.ts | 96 ++++++++++++++++++++++-- concord-server/src/validators/actions.ts | 3 + 5 files changed, 117 insertions(+), 16 deletions(-) create mode 100644 concord-server/src/controller/actions.ts create mode 100644 concord-server/src/validators/actions.ts diff --git a/concord-server/src/controller/actions.ts b/concord-server/src/controller/actions.ts new file mode 100644 index 0000000..b1dc7fc --- /dev/null +++ b/concord-server/src/controller/actions.ts @@ -0,0 +1,6 @@ +import { getUserInformation } from "../services/actions"; + + +export async function fetchUserData(id: string) { + return await getUserInformation(id); +} \ No newline at end of file diff --git a/concord-server/src/index.ts b/concord-server/src/index.ts index da9f8a6..d47ea7e 100644 --- a/concord-server/src/index.ts +++ b/concord-server/src/index.ts @@ -42,12 +42,9 @@ app.use('*', cors({ allowHeaders: ['Content-Type', 'Authorization'], allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], credentials: true -})) -app.use(routes) +}) + +app.route('/api', routes) - - - - -export default app +export default app; diff --git a/concord-server/src/routes/actions.ts b/concord-server/src/routes/actions.ts index 68fa198..d2ca9de 100644 --- a/concord-server/src/routes/actions.ts +++ b/concord-server/src/routes/actions.ts @@ -1,7 +1,8 @@ import { Hono } from "hono" +import { fetchUserData } from "../controller/actions" const actions = new Hono() - +/* actions.post("actions/message/:id", (c) => { //todo: pass service function to send a message to user id }) @@ -14,14 +15,22 @@ actions.get("actions/message/all/:userId", (c) => { //todo: pass service function to fetch all messages f a user }) -actions.get("actions/:id", (c) => { - //todo: pass service function to fetch user data by id -}) + actions.get("actions/userChannels/:id", (c) => { //todo: pass service function to fetch all channels the id is part of }) +*/ +actions.get("actions/:id", async (c) => { + const id = c.req.param("id"); + const userData = await fetchUserData(id); + if (userData) { + return c.json(userData); + } else { + return c.json({ error: "User not found" }, 404); + } +}) export default actions \ No newline at end of file diff --git a/concord-server/src/services/actions.ts b/concord-server/src/services/actions.ts index 879d60a..797f85a 100644 --- a/concord-server/src/services/actions.ts +++ b/concord-server/src/services/actions.ts @@ -1,4 +1,4 @@ -//send a ping to a user +import { Message, MessagePing, Role, UserAuth } from "@prisma/client"; export async function getUserChannels(userId: string): Promise { try{ @@ -24,16 +24,42 @@ export async function getUserChannels(userId: string): Promise } } -export async function getUserInformation(userId: string): Promise<{id: string, name: string} | null> { +export async function getUserInformation(userId: string): + Promise<{ + id: string, + userName: string, + nickName?: string, + bio?: string, + picture?: string, + banner?: string, + admin: boolean, + status: 'online' | 'offline' | 'dnd' | 'idle' | 'invis', + role: Role[], + userAuth?: UserAuth, + messages: Message[], + MessagePing: MessagePing[] +} | null> { try{ if(!userId){ throw new Error('missing userId'); } - //TODO: add some prisma code here //to fetch user information - return {id: userId, name: "Test User"}; + return { + id: userId, + userName: "Test User", + bio: "this is a bio", + picture: "https://www.goodhousekeeping.com/life/pets/g61070837/cutest-cat-breeds/", + banner: "https://www.goodhousekeeping.com/life/pets/g61070837/cutest-cat-breeds/", + admin: false, + status: "offline", + role: [], + userAuth: undefined, + messages: [], + MessagePing: [] + }; + }catch(err){ const errMessage = err as Error @@ -47,4 +73,64 @@ export async function getUserInformation(userId: string): Promise<{id: string, n } } -export async function getAllMessage(userId: string): Promise<{} +export async function getAllMessage(userId: string): Promise<{id: string, content: string, senderId: string, receiverId: string}[] | null> { + try{ + if(!userId){ + throw new Error('missing userId'); + } + + //TODO: add some prisma code here + //to fetch all messages for a user + + return [ + {id: "1", content: "Hello", senderId: userId, receiverId: "2"}, + {id: "2", content: "Hi", senderId: "2", receiverId: userId} + ]; + }catch(err){ + const errMessage = err as Error + + if(errMessage.message === 'missing userId'){ + console.log('services::actions::getAllMessage - missing userId'); + return null; + } + + console.log('services::actions::getAllMessage - unknown error', errMessage); + return null; + } +} + + +export async function sendMessage(userId: string, content: string): Promise { + try{ + if(!userId){ + throw new Error('missing userId'); + } + + if(!content){ + throw new Error('missing content'); + } + + //TODO: add some prisma code here + //to save a message + + //todo: add some socketio code to + //emit the message to the receiver + + return true; + }catch(err){ + const errMessage = err as Error + + if(errMessage.message === 'missing userId'){ + console.log('services::actions::sendMessage - missing userId'); + return false; + } + + if(errMessage.message === 'missing content'){ + console.log('services::actions::sendMessage - missing content'); + return false; + } + + console.log('services::actions::sendMessage - unknown error', errMessage); + return false; + } +} diff --git a/concord-server/src/validators/actions.ts b/concord-server/src/validators/actions.ts new file mode 100644 index 0000000..55d73ea --- /dev/null +++ b/concord-server/src/validators/actions.ts @@ -0,0 +1,3 @@ +import { zValidator } from "@hono/zod-validator" + +export const \ No newline at end of file