fix vote checkbox
This commit is contained in:
@@ -32,55 +32,80 @@ class VoteController extends Controller
|
||||
|
||||
private function insertVote($request)
|
||||
{
|
||||
foreach ($request->vote as $key => $value) {
|
||||
Result::updateOrCreate([
|
||||
// dd($request->vote[0]['nominee_id']);
|
||||
$votes = array_map(function($nominee_id) use($request){
|
||||
return[
|
||||
'voter_id' => Auth::id(),
|
||||
'election_id' => Util::getCurrentElection(),
|
||||
'position_id' => $value['position_id'],
|
||||
], ['nominee_id' => $value['nominee_id']]);
|
||||
'position_id' => $request->vote[0]['position_id'],
|
||||
'nominee_id' => $nominee_id
|
||||
];
|
||||
|
||||
},$request->vote[0]['nominee_id']);
|
||||
|
||||
foreach($votes as $vote){
|
||||
Result::updateOrCreate([
|
||||
'voter_id' => $vote['voter_id'],
|
||||
'election_id' => $vote['election_id'],
|
||||
'position_id' => $vote['position_id'],
|
||||
'nominee_id' => $vote['nominee_id'],
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function validNominee($id, $position_id)
|
||||
{
|
||||
return Nominee::where([
|
||||
['id', '=', $id],
|
||||
['position_id', '=', $position_id]
|
||||
])->count();
|
||||
try{
|
||||
$nominee = Nominee::where('position_id', '=', $position_id)->whereIn('id',$id)->count();
|
||||
}catch(Exception $e){
|
||||
dd($id);
|
||||
}
|
||||
return $nominee;
|
||||
}
|
||||
|
||||
private function isPositionExist($id)
|
||||
{
|
||||
return Position::where('id', $id)->count();
|
||||
try{
|
||||
return Position::where('id', $id)->count();
|
||||
}catch(Exception $e){
|
||||
dd($id);
|
||||
}
|
||||
}
|
||||
|
||||
private function voteAllPosition($request)
|
||||
{
|
||||
$total_position = Position::where('election_id', Util::getCurrentElection())->count();
|
||||
$total_vote = count($request->vote);
|
||||
$total_vote = !empty($request->vote) ? count($request->vote) : 0;
|
||||
|
||||
return $total_vote >= $total_position;
|
||||
}
|
||||
|
||||
|
||||
private function validateRequest($request)
|
||||
{
|
||||
$result['status'] = 'failed';
|
||||
$vote = $request->vote;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Check if the user vote on all position
|
||||
*/
|
||||
if (!$this->voteAllPosition($request)) {
|
||||
dd('test');
|
||||
$result['message'] = 'You must vote on all position.';
|
||||
return $result;
|
||||
}
|
||||
|
||||
foreach ($vote as $key => $value) {
|
||||
foreach ($vote as $value) {
|
||||
/**
|
||||
* Check if position_id and nominee_id has a value
|
||||
*/
|
||||
// dd($vote,$value);
|
||||
if (empty($value['position_id']) || empty($value['nominee_id'])) {
|
||||
|
||||
dd($value,empty($value['position_id']),empty($value['nominee_id']));
|
||||
$result['message'] = 'You must vote on all position.';
|
||||
return $result;
|
||||
}
|
||||
@@ -90,6 +115,7 @@ class VoteController extends Controller
|
||||
* Check if the Position you vote exists
|
||||
*/
|
||||
if (!$this->isPositionExist($value['position_id'])) {
|
||||
dd('test');
|
||||
$result['message'] = 'Invalid Position.';
|
||||
return $result;
|
||||
}
|
||||
@@ -97,8 +123,11 @@ class VoteController extends Controller
|
||||
/**
|
||||
* Check if the Nominee you vote on certain position exists
|
||||
*/
|
||||
elseif (!$this->validNominee($value['nominee_id'], $value['position_id'])) {
|
||||
// elseif (!$this->validNominee($value['nominee_id'], $value['position_id'])) {
|
||||
elseif($this->validNominee($value['nominee_id'],$value['position_id']) <= 0){
|
||||
|
||||
$result['message'] = 'Invalid Nominee for '.Position::find($value['position_id'])->name.' position.';
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user