From 3cd2644582ac1445149c6400ff68c79ddb145891 Mon Sep 17 00:00:00 2001 From: kusowl Date: Thu, 22 Jan 2026 14:55:09 +0530 Subject: [PATCH] feature(deal-modal): count view interaction --- app/Enums/InteractionType.php | 1 + .../Controllers/InteractionController.php | 32 +++++++++++++++++++ ...2026_01_22_083544_add_view_interaction.php | 27 ++++++++++++++++ resources/js/deal-view-modal.js | 4 ++- routes/api/api.php | 10 ++++++ routes/api/deal.php | 19 ----------- routes/api/deals.php | 14 ++++++++ routes/api/interactions.php | 7 ++++ routes/web.php | 2 +- 9 files changed, 95 insertions(+), 21 deletions(-) create mode 100644 database/migrations/2026_01_22_083544_add_view_interaction.php create mode 100644 routes/api/api.php delete mode 100644 routes/api/deal.php create mode 100644 routes/api/deals.php create mode 100644 routes/api/interactions.php diff --git a/app/Enums/InteractionType.php b/app/Enums/InteractionType.php index e545e96..a25146f 100644 --- a/app/Enums/InteractionType.php +++ b/app/Enums/InteractionType.php @@ -11,4 +11,5 @@ enum InteractionType: string case Like = 'like'; case Favorite = 'favorite'; case Redirection = 'redirect'; + case View = 'view'; } diff --git a/app/Http/Controllers/InteractionController.php b/app/Http/Controllers/InteractionController.php index 2f09f05..95fdd5d 100644 --- a/app/Http/Controllers/InteractionController.php +++ b/app/Http/Controllers/InteractionController.php @@ -96,4 +96,36 @@ public function redirect(Deal $deal) return redirect()->away($deal->link); } + + public function view(Deal $deal) + { + ds('hi'); + try { + $interaction = $deal->interactions()->firstOrCreate([ + 'type' => InteractionType::View, + 'user_id' => Auth::id(), + ]); + + if (! $interaction->wasRecentlyCreated) { + $interaction->increment('count'); + } + ds($interaction); + + } catch (\Throwable $e) { + Log::error('Error when view a deal', + [ + 'deal_id' => $deal->id, + 'user_id' => Auth::id(), + 'error' => $e->getMessage(), + 'trace' => $e->getTraceAsString(), + ] + ); + + return response()->json(['error' => 'Something went wrong.'], 500); + } + + return response()->json([ + 'message' => 'View counted', + ]); + } } diff --git a/database/migrations/2026_01_22_083544_add_view_interaction.php b/database/migrations/2026_01_22_083544_add_view_interaction.php new file mode 100644 index 0000000..e63e575 --- /dev/null +++ b/database/migrations/2026_01_22_083544_add_view_interaction.php @@ -0,0 +1,27 @@ +enum('type', InteractionType::values())->change(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('interactions', function (Blueprint $table) {}); + } +}; diff --git a/resources/js/deal-view-modal.js b/resources/js/deal-view-modal.js index 6ba492f..8c73c49 100644 --- a/resources/js/deal-view-modal.js +++ b/resources/js/deal-view-modal.js @@ -2,7 +2,7 @@ import {showToast} from "@/toast.js"; import {closeModal, showModal} from "@/modal.js"; import {redirect} from "./interaction.js"; -async function showDealModal(dealId, dealTitle) { +async function showDealModal(dealId) { if (dealId === null || dealId === "") { showToast('Something went wrong!'); return; @@ -20,6 +20,8 @@ async function showDealModal(dealId, dealTitle) { // Add the id so that action buttons can identify the route dealModal.dataset.dealId = dealId; + // Increment the view count + await axios.post(`/api/view/${dealId}`); } catch (e) { console.error(e) closeModal('deal-modal') diff --git a/routes/api/api.php b/routes/api/api.php new file mode 100644 index 0000000..3a7e4cd --- /dev/null +++ b/routes/api/api.php @@ -0,0 +1,10 @@ +middleware('auth') + ->group(function () { + include __DIR__.'/interactions.php'; + include __DIR__.'/deals.php'; + }); diff --git a/routes/api/deal.php b/routes/api/deal.php deleted file mode 100644 index 055c399..0000000 --- a/routes/api/deal.php +++ /dev/null @@ -1,19 +0,0 @@ -middleware('auth') - ->group(function () { - Route::get('/deals/{deal}', function (Deal $deal) { - return new DealResource( - (new ExplorePageDealsQuery) - ->builder() - ->where('id', $deal->id) - ->first() - ); - }); - }); diff --git a/routes/api/deals.php b/routes/api/deals.php new file mode 100644 index 0000000..ea3872a --- /dev/null +++ b/routes/api/deals.php @@ -0,0 +1,14 @@ +builder() + ->where('id', $deal->id) + ->first() + ); +}); diff --git a/routes/api/interactions.php b/routes/api/interactions.php new file mode 100644 index 0000000..3f3eac8 --- /dev/null +++ b/routes/api/interactions.php @@ -0,0 +1,7 @@ +middleware('throttle:30,1') + ->name('view-deal'); diff --git a/routes/web.php b/routes/web.php index 88a7a4a..521431c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -25,4 +25,4 @@ * we do not want to use sanctum for web requests */ // ------------- API Routes ------------ -require __DIR__.'/api/deal.php'; +require __DIR__.'/api/api.php';