diff --git a/__test__/example.http b/__test__/example.http index 1067b8ef..37ce8344 100644 --- a/__test__/example.http +++ b/__test__/example.http @@ -10,7 +10,7 @@ GET http://localhost:3000/api/users HTTP/1.1 ### -GET http://localhost:3000/api/users/127 HTTP/1.1 +GET http://localhost:3000/api/users/10 HTTP/1.1 ### @@ -24,7 +24,7 @@ Content-Type: application/json ### -PUT http://localhost:3000/api/users/127 HTTP/1.1 +PUT http://localhost:3000/api/users/37 HTTP/1.1 Content-Type: application/json { @@ -34,7 +34,7 @@ Content-Type: application/json ### -DELETE http://localhost:3000/api/users/127 HTTP/1.1 +DELETE http://localhost:3000/api/users/37 HTTP/1.1 ### # 投稿一覧取得 @@ -42,7 +42,7 @@ GET http://localhost:3000/api/posts HTTP/1.1 ### # 特定の投稿を取得 -GET http://localhost:3000/api/posts/36 HTTP/1.1 +GET http://localhost:3000/api/posts/5 HTTP/1.1 ### # 投稿を作成 @@ -53,12 +53,12 @@ Content-Type: application/json "title": "Test Post", "body": "This is a test post.", "tags": ["test", "example"], - "author_id": 182 + "author_id": 10 } ### # 投稿を更新 -PUT http://localhost:3000/api/posts/36 HTTP/1.1 +PUT http://localhost:3000/api/posts/6 HTTP/1.1 Content-Type: application/json { @@ -69,4 +69,4 @@ Content-Type: application/json ### # 投稿を削除 -DELETE http://localhost:3000/api/posts/36 HTTP/1.1 +DELETE http://localhost:3000/api/posts/6 HTTP/1.1 diff --git a/__test__/posts.test.ts b/__test__/posts.tests.ts similarity index 79% rename from __test__/posts.test.ts rename to __test__/posts.tests.ts index 15606b5c..40d8ae07 100644 --- a/__test__/posts.test.ts +++ b/__test__/posts.tests.ts @@ -18,6 +18,15 @@ describe('Posts API', () => { userId = user.id; }); + afterEach(async () => { + await prisma.$transaction([ + prisma.postTag.deleteMany(), + prisma.tag.deleteMany(), + prisma.post.deleteMany(), + prisma.user.deleteMany(), + ]); + }); + it('should create a post and return 201 status', async () => { const requestBody = JSON.stringify({ title: 'Test Post', @@ -138,4 +147,40 @@ describe('Posts API', () => { expect(deleteResponse.status).toBe(204); }); + + it('should return 404 when trying to update a non-existent post', async () => { + const updateRequestBody = JSON.stringify({ + title: 'Non-existent Post', + body: 'This post does not exist.', + tags: ['nonexistent'], + }); + const updateRequest = new Request( + `http://localhost:3000/api/posts/999999`, + { + method: 'PUT', + body: updateRequestBody, + }, + ); + + const updateResponse = await PUT(updateRequest, { + params: { id: '999999' }, + }); + + expect(updateResponse.status).toBe(404); + }); + + it('should return 404 when trying to delete a non-existent post', async () => { + const deleteRequest = new Request( + `http://localhost:3000/api/posts/999999`, + { + method: 'DELETE', + }, + ); + + const deleteResponse = await DELETE(deleteRequest, { + params: { id: '999999' }, + }); + + expect(deleteResponse.status).toBe(404); + }); }); diff --git a/__test__/users.test.ts b/__test__/users.test.ts index 5acf3d80..e8d3f0c6 100644 --- a/__test__/users.test.ts +++ b/__test__/users.test.ts @@ -40,6 +40,8 @@ describe('Users API', () => { const createResponse = await POST(createRequest); const createdUser = await createResponse.json(); + console.debug(createdUser); + console.debug(createdUser.id); const response = await GET_ID( new Request(`http://localhost:3000/api/users/${createdUser.id}`), diff --git a/package.json b/package.json index eb9721ba..bfb0e18f 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "format:check": "prettier --check .", "prepare": "husky install", "db:seed": "npx prisma db seed --schema ./src/app/prisma/schema.prisma", - "db:setup": "npx prisma generate --schema ./src/app/prisma/schema.prisma", + "db:generate": "npx prisma generate --schema ./src/app/prisma/schema.prisma", "db:drop": "npx prisma db query 'DROP DATABASE your_database_name' --schema ./src/app/prisma/schema.prisma", "db:migrate": "npx prisma migrate dev --schema ./src/app/prisma/schema.prisma", "db:reset": "npx prisma migrate reset --schema ./src/app/prisma/schema.prisma", diff --git a/src/app/api/posts/[id]/route.ts b/src/app/api/posts/[id]/route.ts index d3d45d54..a1acde25 100644 --- a/src/app/api/posts/[id]/route.ts +++ b/src/app/api/posts/[id]/route.ts @@ -38,6 +38,15 @@ export async function PUT( { params }: { params: { id: string } }, ) { try { + const postId = parseInt(params.id, 10); + const existingPost = await prisma.post.findUnique({ + where: { id: postId }, + }); + + if (!existingPost) { + return NextResponse.json({ message: 'Post not found' }, { status: 404 }); + } + const { title, body, draft, notice, tags, scope, published_at } = await request.json(); @@ -55,7 +64,7 @@ export async function PUT( } const post = await prisma.post.update({ - where: { id: parseInt(params.id, 10) }, + where: { id: postId }, data: { title, body, @@ -72,7 +81,7 @@ export async function PUT( // ポストとタグの関連付けをリセットして再設定 await prisma.postTag.deleteMany({ - where: { postId: parseInt(params.id, 10) }, + where: { postId: postId }, }); if (tagIds.length > 0) { @@ -110,11 +119,20 @@ export async function DELETE( { params }: { params: { id: string } }, ) { try { + const postId = parseInt(params.id, 10); + const existingPost = await prisma.post.findUnique({ + where: { id: postId }, + }); + + if (!existingPost) { + return NextResponse.json({ message: 'Post not found' }, { status: 404 }); + } + await prisma.postTag.deleteMany({ - where: { postId: parseInt(params.id, 10) }, + where: { postId: postId }, }); await prisma.post.delete({ - where: { id: parseInt(params.id, 10) }, + where: { id: postId }, }); return new Response(null, { status: 204 }); } catch (error) {