@@ -57,14 +57,14 @@ public function index()
57
57
*
58
58
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
59
59
*/
60
- public function show (Request $ request , $ slug )
60
+ public function show (Request $ request , $ id )
61
61
{
62
- $ poll = Poll::whereSlug ( $ slug )-> firstOrFail ( );
62
+ $ poll = Poll::findOrFail ( $ id );
63
63
$ user = $ request ->user ();
64
64
$ user_has_voted = $ poll ->voters ->where ('user_id ' , '= ' , $ user ->id )->isNotEmpty ();
65
65
66
66
if ($ user_has_voted ) {
67
- return redirect (' polls/ ' . $ poll ->slug . ' /result ' )
67
+ return redirect ()-> route ( ' poll_results ' , [ ' id ' => $ poll ->id ] )
68
68
->withInfo ('You have already vote on this poll. Here are the results. ' );
69
69
}
70
70
@@ -82,10 +82,11 @@ public function vote(VoteOnPoll $request)
82
82
{
83
83
$ user = $ request ->user ();
84
84
$ poll = Option::findOrFail ($ request ->input ('option.0 ' ))->poll ;
85
-
86
- // Extract the logic to function validateVoter()
87
- if (!$ this ->validateVoter ($ request , $ user , $ poll )) {
88
- return redirect ('polls/ ' .$ poll ->slug .'/result ' )
85
+ $ voted = Voter::where ('user_id ' , '= ' , $ user ->id )
86
+ ->where ('poll_id ' , '= ' , $ poll ->id )
87
+ ->exists ();
88
+ if ($ voted ) {
89
+ return redirect ()->route ('poll_results ' , ['id ' => $ poll ->id ])
89
90
->withErrors ('Bro have already vote on this poll. Your vote has not been counted. ' );
90
91
}
91
92
@@ -98,7 +99,6 @@ public function vote(VoteOnPoll $request)
98
99
$ vote = new Voter ();
99
100
$ vote ->poll_id = $ poll ->id ;
100
101
$ vote ->user_id = $ user ->id ;
101
- $ vote ->ip_address = $ request ->ip ();
102
102
$ vote ->save ();
103
103
104
104
$ poll_url = hrefPoll ($ poll );
@@ -108,7 +108,7 @@ public function vote(VoteOnPoll $request)
108
108
"[url= {$ profile_url }] {$ user ->username }[/url] has voted on poll [url= {$ poll_url }] {$ poll ->title }[/url] "
109
109
);
110
110
111
- return redirect (' polls/ ' . $ poll ->slug . ' /result ' )
111
+ return redirect ()-> route ( ' poll_results ' , [ ' id ' => $ poll ->id ] )
112
112
->withSuccess ('Your vote has been counted. ' );
113
113
}
114
114
@@ -119,44 +119,14 @@ public function vote(VoteOnPoll $request)
119
119
*
120
120
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
121
121
*/
122
- public function result ($ slug )
122
+ public function result ($ id )
123
123
{
124
- $ poll = Poll::whereSlug ( $ slug )-> firstOrFail ( );
124
+ $ poll = Poll::findOrFail ( $ id );
125
125
$ map = [
126
126
'poll ' => $ poll ,
127
127
'total_votes ' => $ poll ->totalVotes (),
128
128
];
129
129
130
130
return view ('poll.result ' , $ map );
131
131
}
132
-
133
- /**
134
- * Check If Voter Validated.
135
- *
136
- * @param VoteOnPoll $request
137
- * @param $user
138
- * @param Poll $poll
139
- *
140
- * @return bool
141
- */
142
- private function validateVoter (VoteOnPoll $ request , $ user , Poll $ poll ): bool
143
- {
144
- // Expect for simplifying the logic below while remain semantic.
145
- return !(
146
- // One user should never vote on one poll twice.
147
- (
148
- Voter::where ('user_id ' , '= ' , $ user ->id )
149
- ->where ('poll_id ' , '= ' , $ poll ->id )
150
- ->exists ()
151
- ) ||
152
- // If ip_checking is set, further examine request's IP.
153
- (
154
- $ poll ->ip_checking == 1
155
- &&
156
- Voter::where ('user_id ' , '= ' , $ user ->id )
157
- ->where ('ip_address ' , '= ' , $ request ->ip ())
158
- ->exists ()
159
- )
160
- );
161
- }
162
132
}
0 commit comments