diff --git a/app/Http/Controllers/API/v1/Election/VoteController.php b/app/Http/Controllers/API/v1/Election/VoteController.php index 227b352..f3be8e3 100644 --- a/app/Http/Controllers/API/v1/Election/VoteController.php +++ b/app/Http/Controllers/API/v1/Election/VoteController.php @@ -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; } } diff --git a/resources/assets/js/components/demo/voter/home/vote.vue b/resources/assets/js/components/demo/voter/home/vote.vue index 3909601..f3dd18b 100644 --- a/resources/assets/js/components/demo/voter/home/vote.vue +++ b/resources/assets/js/components/demo/voter/home/vote.vue @@ -1,12 +1,19 @@ @@ -76,7 +83,7 @@ export default { for (var i in this.data.positions) { this.selected.push({ position_id: this.data.positions[i]['id'], - nominee_id: null, + nominee_id: [], }); } }, @@ -84,40 +91,53 @@ export default { data: function () { return { selected: [], - name: {} + name: {}, + selectedNominees: { + }, } }, methods: { - test: function(){ + test: function () { console.log('tab'); this.util.showModal('#vote-modal') }, submit: function () { - // this.util.hideModal('#vote-modal'); - if (this.hasNullVote()) return; + if (Object.keys(this.selected).length === 0) { + // No selections made + this.util.notify('Please select at least one nominee.', 'error'); + return; + } + var vm = this; this.util.notify('Submitting your vote, please wait...', 'loading'); - let data = {}; - data['vote'] = this.selected; - axios.post(config.API+'election/vote', data) - .then(response=>{ + console.log(this.selected) + // Prepare data to send to the server + let data = { + vote: this.selected + }; + + // Log the data to ensure it's correctly formatted + console.log(data); + + // Send a POST request to the server + axios.post(config.API + 'election/vote', data) + .then(response => { $.notifyClose(); if (vm.util.showResult(response, 'success')) { vm.data.result = response.data.result; vm.$router.push({ name: 'Voter Home' }); } - }) - .catch(error=>{ + .catch(error => { $.notifyClose(); vm.util.showResult(error, 'error'); - }) - + }); }, hasNullVote: function () { - let selected = this.selected; + console.log('null' + this.selectedNominees); + let selected = this.selectedNominees; for (var i in selected) if (selected[i]['nominee_id'] == null) { this.util.notify('You must vote on all position', 'error'); @@ -127,19 +147,27 @@ export default { }, reset: function () { - let selected = this.selected; + console.log(this.selectedNominees); + let selected = this.selectedNominees; for (var i in selected) { - $('#'+selected[i]['nominee_id']).selected(false); + $('#' + selected[i]['nominee_id']).selected(false); selected[i]['nominee_id'] = null; } }, vote: function (position_id, nominee_id) { - $('#'+nominee_id).selected(); - for (var i in this.selected) - if (this.selected[i]['position_id'] == position_id) { - this.selected[i]['nominee_id'] = nominee_id; - } + if (!this.selected[0]['position_id']) { + this.$set(this.selected[0], position_id, []); + } + const index = this.selected[0]['nominee_id'].indexOf(nominee_id); + if (index === -1) { + // Nominee not found, add it + this.selected[0]['nominee_id'].push(nominee_id); + } else { + // Nominee found, remove it + this.selected[0]['nominee_id'].splice(index, 1); + } + console.log(this.selected[0]) }, getName: function (id) { @@ -152,17 +180,29 @@ export default { }, getNominee: function (id) { + let positions = this.data.positions; let nominees = this.data.nominees; for (var i in nominees) - if (nominees[i]['id'] == id) return nominees[i]['name']; + if (nominees[i]['id']==id) return nominees[i].name; + return ''; }, + // getNominee: function (id) { + // let nominees = this.data.nominees; + // for (var i in nominees) + // if (nominees[i]['id'] == id) return nominees[i]['name']; + // }, + getPartylist: function (id) { let partylists = this.data.partylists; for (var i in partylists) if (partylists[i]['id'] == id) return partylists[i]['name']; return 'No Partylist'; - } + }, + + // uncheck: function (checkedName) { + // this.checkedName = this.checkedNames.filter(name => name !== checkedName); + // } } } \ No newline at end of file