continue scaffolding and refine the basic foundation
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
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;
|
||||
});
|
||||
Reference in New Issue
Block a user