From dc920360dc495da306d3361d98ba5521cad5ccf3 Mon Sep 17 00:00:00 2001
From: Benny Samir Hierl <bennysamir@posteo.de>
Date: Wed, 9 Feb 2022 22:21:43 +0100
Subject: [PATCH] #3 add new endpoint to mark item as bought

Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
---
 src/api/routes/wishlist/index.ts    |  3 ++-
 src/api/routes/wishlist/update.ts   | 22 ++++++++++++++++++++++
 src/composables/useWishlistStore.ts |  6 +++---
 src/views/DetailView.vue            |  4 ++--
 4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/src/api/routes/wishlist/index.ts b/src/api/routes/wishlist/index.ts
index 13ad069..a26fd49 100644
--- a/src/api/routes/wishlist/index.ts
+++ b/src/api/routes/wishlist/index.ts
@@ -1,6 +1,6 @@
 import { FastifyInstance } from 'fastify'
 import { getAll, getBySlugUrl } from './read'
-import { updateList, updateItem } from './update'
+import { updateList, updateItem, itemBought } from './update'
 import { createList, createItem } from './create'
 import { deleteList, deleteItem } from './delete'
 
@@ -11,6 +11,7 @@ export default async (app: FastifyInstance) => {
   await app.route(createItem)
   await app.route(updateList)
   await app.route(updateItem)
+  await app.route(itemBought)
   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 9eb2efc..8e9b07b 100644
--- a/src/api/routes/wishlist/update.ts
+++ b/src/api/routes/wishlist/update.ts
@@ -73,3 +73,25 @@ export const updateItem = <RouteOptions>{
     reply.send(await wishlist.updateItem(request.params.itemId, request.body))
   },
 }
+
+export const itemBought = <RouteOptions>{
+  method: 'POST',
+  url: '/:wishlistId/item/:itemId/bought',
+  schema: {
+    params: {
+      type: 'object',
+      properties: {
+        wishlistId: { type: 'string' },
+        itemId: { type: 'number' },
+      },
+    },
+    response: {
+      200: wishlistItemResponseSchema,
+    },
+  },
+  handler: async (request: updateItemRequest, reply: FastifyReply) => {
+    reply.send(
+      await wishlist.updateItem(request.params.itemId, { bought: true })
+    )
+  },
+}
diff --git a/src/composables/useWishlistStore.ts b/src/composables/useWishlistStore.ts
index caac117..f4eb658 100644
--- a/src/composables/useWishlistStore.ts
+++ b/src/composables/useWishlistStore.ts
@@ -17,14 +17,14 @@ const fetch = async (slugText: string): Promise<void> => {
   }
 }
 
-const updateItem = async (item: WishlistItem): Promise<void> => {
-  await client.put(`/wishlist/${item.wishlistId}/item/${item.id}`, item)
+const itemBought = async (item: WishlistItem): Promise<void> => {
+  await client.post(`/wishlist/${item.wishlistId}/item/${item.id}/bought`, item)
 }
 
 export const useWishlistStore = () => {
   return {
     list,
     fetch,
-    updateItem,
+    itemBought,
   }
 }
diff --git a/src/views/DetailView.vue b/src/views/DetailView.vue
index 7f19bb8..50c777b 100644
--- a/src/views/DetailView.vue
+++ b/src/views/DetailView.vue
@@ -13,7 +13,7 @@ const modal = useModal()
 
 const { t } = useI18n()
 
-const { list, fetch, updateItem } = useWishlistStore()
+const { list, fetch, itemBought } = useWishlistStore()
 await fetch(route.params.slug as string)
 
 const notBoughtItems = computed(() => {
@@ -31,7 +31,7 @@ const bought = async (item: WishlistItemType): Promise<void> => {
   )
   if (confirmed) {
     item.bought = true
-    updateItem(item)
+    itemBought(item)
   }
 }
 </script>