diff --git a/concord-server/schema.prisma b/concord-server/schema.prisma index 69e738c..fdfebd7 100644 --- a/concord-server/schema.prisma +++ b/concord-server/schema.prisma @@ -82,10 +82,10 @@ model Message { } model Reply { - message Message @relation("MessageToReply", fields: [messageId], references: [id]) - messageId String @unique - repliesTo Message @relation("ReplyToMessage", fields: [repliesToId], references: [id]) - repliesToId String @unique + message Message @relation("MessageToReply", fields: [messageId], references: [id]) //message text + messageId String @unique //message id of the reply + repliesTo Message @relation("ReplyToMessage", fields: [repliesToId], references: [id]) //message id that this message replies to + repliesToId String @unique //replies to this message id @@unique([messageId, repliesToId]) } diff --git a/concord-server/src/controller/realtime.ts b/concord-server/src/controller/realtime.ts index 0151393..7cacb9d 100644 --- a/concord-server/src/controller/realtime.ts +++ b/concord-server/src/controller/realtime.ts @@ -3,7 +3,6 @@ import { sendMessageToChannel, removeMessageFromChannel } from "../services/real import { success } from "zod"; - export async function postMessageToChannel( io: any, c: Context @@ -114,7 +113,7 @@ export async function deleteMessageFromChannel( message: "Message deleted successfully", status: 200 }) - + } catch (err) { const errMessage = err as Error; @@ -125,4 +124,4 @@ export async function deleteMessageFromChannel( status: 500 }); } -} \ No newline at end of file +} diff --git a/concord-server/src/routes/realtime.ts b/concord-server/src/routes/realtime.ts new file mode 100644 index 0000000..27fb941 --- /dev/null +++ b/concord-server/src/routes/realtime.ts @@ -0,0 +1,28 @@ +import { Hono } from "hono"; +import { zValidator } from "@hono/zod-validator"; +import { describeRoute, resolver } from "hono-openapi"; +import { postMessageToChannel, + deleteMessageFromChannel +} from "../controller/realtime"; + +const app = new Hono(); + +app.post( + "message/", + zValidator({ + body: z.object({ + content: z.string().min(1).max(500) + }) + }), + async (c) => { + const { instanceId, categoryId, channelId } = c.req.params; + const { content } = c.req.body; + + return postMessageToChannel(c.get("io"), { + instanceId, + categoryId, + channelId, + content + }); + } +); \ No newline at end of file diff --git a/concord-server/src/services/message.ts b/concord-server/src/services/message.ts new file mode 100644 index 0000000..4e666ee --- /dev/null +++ b/concord-server/src/services/message.ts @@ -0,0 +1,77 @@ +import { + Message, + MessagePing, + PrismaClient, + Role, + Reply + +} from "@prisma/client"; +import { CreateUserInput } from '../validators/userValidator'; + +const prisma = new PrismaClient(); + +class MessageService { + public async function sendMessageToChannel( + channelId: string, + userId: string, + content: string, + repliedMessageId: string | null + ): Promise<{ + id: string, + channelId: string, + userId: string, + text: string, + deleted: boolean, + replies: null | { + messageId: string, + repliesToId: string, + repliesToText: string + } + } | null> { + try { + + + const newMessage = await prisma.message.create({ + data: { + channelId: channelId, + userId: userId, + text: content, + deleted: false, + + } + }) + + let origMessage; + if(repliedMessageId){ + origMessage = await prisma.message.findUnique({ + where: { + id: repliedMessageId + } + }) + + if(!origMessage){ + throw new Error("could not find original message to reply to"); + } + + await prisma.reply.create({ + data: { + messageId: newMessage.id, + repliesToId: origMessage.id + } + }) + } + + return { + ...newMessage, + replies: repliedMessageId ? { + messageId: newMessage.id, + repliesToId: origMessage?.id, + repliesToText: origMessage?.text + } : null + } + + } catch (error) { + + + } +} \ No newline at end of file diff --git a/concord-server/src/validators/messageValidator.ts b/concord-server/src/validators/messageValidator.ts new file mode 100644 index 0000000..e69de29