feat: add wishlist
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
const coverSchema = z.object({
|
||||
cover: z.string().regex(OPEN_LIBRARY_COVER, 'Not an Open Library cover')
|
||||
})
|
||||
|
||||
// Public: anyone can fill in a missing cover, but only with an Open Library cover URL.
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = Number(getRouterParam(event, 'id'))
|
||||
const body = coverSchema.safeParse(await readBody(event))
|
||||
if (!Number.isInteger(id) || !body.success) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid cover' })
|
||||
}
|
||||
|
||||
const table = wishlistTable(event)
|
||||
const rows = await listRows<WishlistRow>(event, table)
|
||||
const row = rows.find(row => row.id === id)
|
||||
if (!row) throw createError({ statusCode: 404, statusMessage: 'No such book' })
|
||||
if (row.cover) return toWishlistBook(row)
|
||||
|
||||
return toWishlistBook(await updateRow<WishlistRow>(event, table, id, { cover: body.data.cover }))
|
||||
})
|
||||
@@ -0,0 +1,4 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const rows = await listRows<WishlistRow>(event, wishlistTable(event))
|
||||
return rows.map(toWishlistBook).filter(book => book.title)
|
||||
})
|
||||
Reference in New Issue
Block a user