-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added api routes , added zod validation for titel
which should contain atleast 1 string
- Loading branch information
1 parent
41dd75a
commit 9064f8d
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import prisma from "@/lib/db/prisma"; | ||
import { createNoteSchema } from "@/lib/validation/note"; | ||
import { auth } from "@clerk/nextjs"; | ||
|
||
export async function POST(req: Request) { | ||
try { | ||
const body = await req.json(); | ||
|
||
const parseResult = createNoteSchema.safeParse(body); | ||
|
||
if (!parseResult.success) { | ||
console.error(parseResult.error); | ||
return Response.json({ error: "Invalid input" }, { status: 400 }); | ||
} | ||
|
||
const { title, content } = parseResult.data; | ||
|
||
const { userId } = auth(); | ||
|
||
if (!userId) { | ||
return Response.json({ error: "Unauthorized" }, { status: 401 }); | ||
} | ||
|
||
const note = await prisma.note.create({ | ||
data: { | ||
title, | ||
content, | ||
userId, | ||
}, | ||
}); | ||
|
||
return Response.json({ note }, { status: 201 }); | ||
} catch (error) { | ||
console.error(error); | ||
return Response.json({ error: "Internal server error" }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { z } from "zod"; | ||
|
||
export const createNoteSchema = z.object({ | ||
title: z.string().min(1, { message: "Title is required" }), | ||
content: z.string().optional(), | ||
}); | ||
|
||
export type CreateNoteSchema = z.infer<typeof createNoteSchema>; |