Skip to content

Commit 8be4b8b

Browse files
author
nejc
committed
Add missing store() and destroy() methods to VoteController
- Add store() method for voting on feature requests by slug - Add destroy() method for removing votes by slug - Methods work with slug-based routes instead of ID-based routes - Default to 'up' vote for simple voting interface - Return proper JSON responses with statistics
1 parent 1b5dd57 commit 8be4b8b

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/Http/Controllers/VoteController.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,81 @@ public function __construct(
2121
$this->featureRequestService = $featureRequestService;
2222
}
2323

24+
/**
25+
* Store a vote on a feature request (alias for vote method).
26+
*/
27+
public function store(Request $request, string $slug): JsonResponse
28+
{
29+
// Get the feature request by slug
30+
$featureRequest = $this->featureRequestService->findBySlug($slug);
31+
32+
if (!$featureRequest) {
33+
return response()->json([
34+
'message' => 'Feature request not found.'
35+
], 404);
36+
}
37+
38+
if (!$this->voteService->canVoteOn($featureRequest->id)) {
39+
return response()->json([
40+
'message' => 'You cannot vote on this feature request.'
41+
], 403);
42+
}
43+
44+
try {
45+
$vote = $this->voteService->vote(
46+
$featureRequest->id,
47+
'up', // Default to up vote for simple voting
48+
null
49+
);
50+
51+
$statistics = $this->voteService->getVoteStatistics($featureRequest->id);
52+
53+
return response()->json([
54+
'message' => 'Vote recorded successfully.',
55+
'data' => [
56+
'vote' => $vote,
57+
'statistics' => $statistics
58+
]
59+
]);
60+
} catch (\Exception $e) {
61+
return response()->json([
62+
'message' => $e->getMessage()
63+
], 400);
64+
}
65+
}
66+
67+
/**
68+
* Remove a vote from a feature request (alias for removeVote method).
69+
*/
70+
public function destroy(Request $request, string $slug): JsonResponse
71+
{
72+
// Get the feature request by slug
73+
$featureRequest = $this->featureRequestService->findBySlug($slug);
74+
75+
if (!$featureRequest) {
76+
return response()->json([
77+
'message' => 'Feature request not found.'
78+
], 404);
79+
}
80+
81+
try {
82+
$this->voteService->removeVote($featureRequest->id);
83+
84+
$statistics = $this->voteService->getVoteStatistics($featureRequest->id);
85+
86+
return response()->json([
87+
'message' => 'Vote removed successfully.',
88+
'data' => [
89+
'statistics' => $statistics
90+
]
91+
]);
92+
} catch (\Exception $e) {
93+
return response()->json([
94+
'message' => $e->getMessage()
95+
], 400);
96+
}
97+
}
98+
2499
/**
25100
* Vote on a feature request.
26101
*/

0 commit comments

Comments
 (0)