mirror of
https://github.com/Einundzwanzig-Podcast/einundzwanzig-portal.git
synced 2025-12-11 06:46:47 +00:00
voting added
This commit is contained in:
107
app/Http/Livewire/ProjectProposal/ProjectProposalVoting.php
Normal file
107
app/Http/Livewire/ProjectProposal/ProjectProposalVoting.php
Normal 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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user