From 16df584334b3945038096a646ef5578be349a6f6 Mon Sep 17 00:00:00 2001 From: Benny Samir Hierl Date: Sun, 6 Feb 2022 11:44:50 +0100 Subject: [PATCH] #1 delete endpoint added --- examples.http | 16 ++++++++++ src/api/models/wishlist/index.ts | 14 +++++++++ src/api/routes/wishlist/delete.ts | 50 +++++++++++++++++++++++++++++++ src/api/routes/wishlist/index.ts | 3 ++ src/api/routes/wishlist/update.ts | 4 +-- 5 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/api/routes/wishlist/delete.ts diff --git a/examples.http b/examples.http index eefce8d..9ff63c8 100644 --- a/examples.http +++ b/examples.http @@ -55,3 +55,19 @@ Content-Type: application/json "imageSrc": "https://www.lego.com/cdn/cs/set/assets/blt1fc37afef51cfa9f/40442.jpg?fit=bounds&format=jpg&quality=80&width=1500&height=1500&dpr=1", "description": "Cute goldfish and fry, build-and-display BrickHeadz™ model" } + +### +# @name updateItemOnFirstWishlist +PUT {{BASE_URL}}/wishlist/{{getWishlists.response.body.0.id}}/item/1 +Content-Type: application/json + +{ + "title": "Goldfish | BrickHeadz", + "url": "https://www.lego.com/en-de/product/goldfish-40442", + "imageSrc": "https://www.lego.com/cdn/cs/set/assets/blt1fc37afef51cfa9f/40442.jpg?fit=bounds&format=jpg&quality=80&width=1500&height=1500&dpr=1", + "description": "Cute goldfish and fry, build-and-display BrickHeadz™ model" +} + +### +# @name deleteItemToFirstWishlist +DELETE {{BASE_URL}}/wishlist/{{getWishlists.response.body.0.id}}/item/2 diff --git a/src/api/models/wishlist/index.ts b/src/api/models/wishlist/index.ts index 570f159..183f227 100644 --- a/src/api/models/wishlist/index.ts +++ b/src/api/models/wishlist/index.ts @@ -30,6 +30,13 @@ export default { }, }) }, + delete: async (id: string) => { + return await prisma.client.wishlist.delete({ + where: { + id: id, + }, + }) + }, createItem: async (wishlistId: string, payload: WishlistItem) => { const wishlist = await prisma.client.wishlist.update({ where: { @@ -57,4 +64,11 @@ export default { }, }) }, + deleteItem: async (itemId: number) => { + return await prisma.client.item.delete({ + where: { + id: itemId, + }, + }) + }, } diff --git a/src/api/routes/wishlist/delete.ts b/src/api/routes/wishlist/delete.ts new file mode 100644 index 0000000..ebeb9e9 --- /dev/null +++ b/src/api/routes/wishlist/delete.ts @@ -0,0 +1,50 @@ +import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify' +import { wishlist } from '../../models' + +interface deleteRequest extends FastifyRequest { + params: { + wishlistId: string + } +} + +interface deleteItemRequest extends FastifyRequest { + params: { + wishlistId: string + itemId: number + } +} + +export const deleteList = { + method: 'DELETE', + url: '/:wishlistId', + schema: { + params: { + type: 'object', + properties: { + wishlistId: { type: 'string' }, + }, + }, + }, + handler: async (request: deleteRequest, reply: FastifyReply) => { + await wishlist.delete(request.params.wishlistId) + reply.code(204).send() + }, +} + +export const deleteItem = { + method: 'DELETE', + url: '/:wishlistId/item/:itemId', + schema: { + params: { + type: 'object', + properties: { + wishlistId: { type: 'string' }, + itemId: { type: 'number' }, + }, + }, + }, + handler: async (request: deleteItemRequest, reply: FastifyReply) => { + await wishlist.deleteItem(request.params.itemId) + reply.code(204).send() + }, +} diff --git a/src/api/routes/wishlist/index.ts b/src/api/routes/wishlist/index.ts index ec37853..13ad069 100644 --- a/src/api/routes/wishlist/index.ts +++ b/src/api/routes/wishlist/index.ts @@ -2,6 +2,7 @@ import { FastifyInstance } from 'fastify' import { getAll, getBySlugUrl } from './read' import { updateList, updateItem } from './update' import { createList, createItem } from './create' +import { deleteList, deleteItem } from './delete' export default async (app: FastifyInstance) => { await app.route(getAll) @@ -10,4 +11,6 @@ export default async (app: FastifyInstance) => { await app.route(createItem) await app.route(updateList) await app.route(updateItem) + await app.route(deleteList) + await app.route(deleteItem) } diff --git a/src/api/routes/wishlist/update.ts b/src/api/routes/wishlist/update.ts index 78d78fb..2f64817 100644 --- a/src/api/routes/wishlist/update.ts +++ b/src/api/routes/wishlist/update.ts @@ -64,8 +64,6 @@ export const updateItem = { }, handler: async (request: updateItemRequest, reply: FastifyReply) => { request.log.debug(request.body) - reply.send( - await wishlist.updateItem(Number(request.params.itemId), request.body) - ) + reply.send(await wishlist.updateItem(request.params.itemId, request.body)) }, }