Skip to content

Commit

Permalink
fix author delete
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-run committed Feb 3, 2024
1 parent 0897133 commit aba625d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ describe('API sanity checks', () => {
const authorBooksData = await authorBooksResponse.json()
expect(authorBooksData).toHaveLength(1)
})

test('DELETE /author/:uuid', async () => {
const response = await fetch('http://localhost:6969/authors')
const data = (await response.json()) as { uuid: string }[]
const authorUuid = data[0].uuid

const deleteResponse = await fetch(`http://localhost:6969/author/${authorUuid}`, {
method: 'DELETE',
})

expect(deleteResponse.status).toBe(200)
})
})

async function serverReady(): Promise<void> {
Expand Down
12 changes: 6 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,19 @@ const app = new Elysia()
}

await client.query('BEGIN')
const author = await client.query('SELECT * FROM authors WHERE uuid = $1', [uuid])
const id = author.rows[0].id
const authorQuery = await client.query<DbAuthor>('SELECT * FROM authors WHERE uuid = $1', [uuid])
const author = authorQuery.rows[0]

const deletedBooks = await client.query('DELETE FROM books WHERE author_id = $1', [id])
const deletedAuthor = await client.query('DELETE FROM authors WHERE id = $1', [id])
const deletedBooks = await client.query('DELETE FROM books WHERE author_id = $1', [author.id])
const deletedAuthor = await client.query('DELETE FROM authors WHERE id = $1', [author.id])
await client.query('COMMIT')

console.info(`Deleted ${deletedBooks.rowCount} books and ${deletedAuthor.rowCount} author`)

return { author_id: id }
return { uuid: author.uuid }
},
{
response: t.Nullable(t.Object({ author_id: t.String() })),
response: t.Nullable(t.Object({ uuid: t.String() })),
},
)
.get(
Expand Down

0 comments on commit aba625d

Please sign in to comment.