voting added

This commit is contained in:
HolgerHatGarKeineNode
2023-03-10 23:03:19 +01:00
parent 4905134ea6
commit c2b7042eab
50 changed files with 1955 additions and 592 deletions

View File

@@ -0,0 +1,107 @@
<?php
namespace App\Http\Livewire\ProjectProposal;
use App\Models\Country;
use App\Models\ProjectProposal;
use App\Models\User;
use App\Models\Vote;
use Illuminate\Validation\Rule;
use Livewire\Component;
class ProjectProposalVoting extends Component
{
public Country $country;
public ?ProjectProposal $projectProposal = null;
public ?Vote $vote = null;
public ?string $fromUrl = '';
protected $queryString = [
'fromUrl' => ['except' => ''],
];
public function rules()
{
return [
'vote.user_id' => 'required',
'vote.project_proposal_id' => 'required',
'vote.value' => 'required|boolean',
'vote.reason' => [
Rule::requiredIf(!$this->vote->value),
]
];
}
public function mount()
{
$vote = Vote::query()
->where('user_id', auth()->id())
->where('project_proposal_id', $this->projectProposal->id)
->first();
if ($vote) {
$this->vote = $vote;
} else {
$this->vote = new Vote();
$this->vote->user_id = auth()->id();
$this->vote->project_proposal_id = $this->projectProposal->id;
$this->vote->value = false;
}
}
public function yes()
{
$this->vote->value = true;
$this->vote->save();
return to_route('project.voting.projectFunding',
['country' => $this->country, 'projectProposal' => $this->projectProposal, 'fromUrl' => $this->fromUrl]);
}
public function no()
{
$this->validate();
$this->vote->value = false;
$this->vote->save();
return to_route('project.voting.projectFunding',
['country' => $this->country, 'projectProposal' => $this->projectProposal, 'fromUrl' => $this->fromUrl]);
}
public function render()
{
return view('livewire.project-proposal.project-proposal-voting', [
'entitledVoters' => User::query()
->with([
'votes' => fn($query) => $query->where('project_proposal_id',
$this->projectProposal->id)
])
->withCount([
'votes' => fn($query) => $query->where('project_proposal_id',
$this->projectProposal->id)
])
->whereHas('roles', function ($query) {
return $query->where('roles.name', 'entitled-voter');
})
->orderByDesc('votes_count')
->get(),
'otherVoters' => User::query()
->with([
'votes' => fn($query) => $query->where('project_proposal_id',
$this->projectProposal->id)
])
->withCount([
'votes' => fn($query) => $query->where('project_proposal_id',
$this->projectProposal->id)
])
->whereDoesntHave('roles', function ($query) {
return $query->where('roles.name', 'entitled-voter');
})
->orderByDesc('votes_count')
->get(),
]);
}
}