Skip to content

Commit

Permalink
add error case
Browse files Browse the repository at this point in the history
  • Loading branch information
susumutomita committed May 20, 2024
1 parent 7c50701 commit 4b7462c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
14 changes: 7 additions & 7 deletions __test__/example.http
Original file line number Diff line number Diff line change
Expand Up @@ -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

###

Expand All @@ -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

{
Expand All @@ -34,15 +34,15 @@ Content-Type: application/json

###

DELETE http://localhost:3000/api/users/127 HTTP/1.1
DELETE http://localhost:3000/api/users/37 HTTP/1.1

###
# 投稿一覧取得
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

###
# 投稿を作成
Expand All @@ -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

{
Expand All @@ -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
45 changes: 45 additions & 0 deletions __test__/posts.test.ts → __test__/posts.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
});
});
2 changes: 2 additions & 0 deletions __test__/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 22 additions & 4 deletions src/app/api/posts/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4b7462c

Please sign in to comment.