|
| 1 | +// import type { NextApiRequest, NextApiResponse } from 'next'; |
| 2 | +import { NextRequest, NextResponse } from 'next/server'; |
| 3 | +import prisma from '@/lib/prisma'; |
| 4 | +// import { type Note, list, geById, create, update, remove } from "@/logic/note"; |
| 5 | + |
| 6 | +// GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, 405 Method Not Allowed |
| 7 | + |
| 8 | +// export default async function handler( |
| 9 | +// req: NextApiRequest, |
| 10 | +// res: NextApiResponse<Note[] | string>, |
| 11 | +// ) { |
| 12 | +// console.log("handler", req.method); |
| 13 | +// |
| 14 | +// switch (req.method) { |
| 15 | +// case "GET": |
| 16 | +// return res.status(200).json(await list()); |
| 17 | +// case "POST": |
| 18 | +// return res.status(201).json(await create(req.body)); |
| 19 | +// case "PUT": |
| 20 | +// const updated = await update(req.body); |
| 21 | +// return res.status(updated.length > 0 ? 200 : 404).json(updated); |
| 22 | +// case "DELETE": |
| 23 | +// const removed = await remove(req.body); |
| 24 | +// return res.status(removed.length > 0 ? 204 : 404).end(); |
| 25 | +// default: |
| 26 | +// return res.status(405).send("Method Not Allowed"); |
| 27 | +// } |
| 28 | +// } |
| 29 | + |
| 30 | +// get |
| 31 | +export async function GET(request: NextRequest) { |
| 32 | + const searchParams = request.nextUrl.searchParams; |
| 33 | + const id = searchParams.get('id'); |
| 34 | + let response: NextResponse; |
| 35 | + |
| 36 | + if(id) { |
| 37 | + response = await prisma.note.findUniqueOrThrow({ |
| 38 | + where: { id: Number(id) }, |
| 39 | + }) |
| 40 | + .then((note) => { |
| 41 | + return NextResponse.json({ success: true, note: note }, { status: 200 }); |
| 42 | + }) |
| 43 | + .catch((error) => { |
| 44 | + return NextResponse.json({ error: error }, { status: 500 }); |
| 45 | + }); |
| 46 | + } else { |
| 47 | + response = await prisma.note.findMany() |
| 48 | + .then((notes) => { |
| 49 | + return NextResponse.json({ success: true, notes: notes }, { status: 200 }); |
| 50 | + }) |
| 51 | + .catch((error) => { |
| 52 | + return NextResponse.json({ error: error }, { status: 500 }); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + return response; |
| 57 | +} |
| 58 | +// create |
| 59 | +export async function POST(request: NextRequest) { |
| 60 | + const body = await request.json(); |
| 61 | + |
| 62 | + const newNote = await prisma.note.create({ data: body }) |
| 63 | + .then((note) => { |
| 64 | + // res.status(201).json(note); |
| 65 | + return new Response(JSON.stringify({ success: true, note: note }), { |
| 66 | + status: 200, |
| 67 | + headers: { "Content-Type": "application/json" }, |
| 68 | + }); |
| 69 | + }) |
| 70 | + .catch((error) => { |
| 71 | + return new Response(JSON.stringify({ error: error }), { |
| 72 | + status: 500, |
| 73 | + headers: { "Content-Type": "application/json" }, |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + return newNote; |
| 78 | +} |
| 79 | +// update |
| 80 | +export async function PUT(request: NextRequest) { |
| 81 | + const body = await request.json(); |
| 82 | + const { id, title, content } = body; |
| 83 | + |
| 84 | + const result = await prisma.note.update({ |
| 85 | + data: { title: title, content: content }, |
| 86 | + where: { id: id }, |
| 87 | + }) |
| 88 | + .then((note) => { |
| 89 | + return NextResponse.json({ success: true, note: note }, { status: 200 }); |
| 90 | + }) |
| 91 | + .catch((error) => { |
| 92 | + return NextResponse.json({ error: error }, { status: 500 }); |
| 93 | + }); |
| 94 | + return result; |
| 95 | +} |
| 96 | +// delete |
| 97 | +export async function DELETE(request: NextRequest) { |
| 98 | + const body = await request.json(); |
| 99 | + |
| 100 | + const result = await prisma.note.delete({ |
| 101 | + where: { id: body.id }, |
| 102 | + }) |
| 103 | + .then((note) => { |
| 104 | + return NextResponse.json({ success: true, note: note }, { status: 200 }); |
| 105 | + }) |
| 106 | + .catch((error) => { |
| 107 | + return NextResponse.json({ error: error }, { status: 500 }); |
| 108 | + }); |
| 109 | + return result; |
| 110 | +} |
0 commit comments