29 lines
847 B
TypeScript
29 lines
847 B
TypeScript
import { eq } from "drizzle-orm";
|
|
import { messages, topics } from "~~/db/schema";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
await protectRoute(event);
|
|
|
|
const db = useDrizzle();
|
|
|
|
const { id } = event.context.params!;
|
|
|
|
const [topic] = await db.select().from(topics).where(eq(topics.id, id));
|
|
if (topic === undefined || topic.userId !== event.context.user.id) {
|
|
throw createError({ statusCode: 404, statusMessage: 'Topic not found' });
|
|
}
|
|
|
|
const { content } = await readBody(event);
|
|
if (!content) {
|
|
throw createError({ statusCode: 400, statusMessage: 'No content provided' });
|
|
}
|
|
|
|
const [message] = await db.insert(messages).values({
|
|
topicId: topic.id,
|
|
userId: event.context.user.id,
|
|
content,
|
|
isUser: true,
|
|
}).returning();
|
|
|
|
return message;
|
|
}); |