Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Nov 11, 2024
1 parent 50d1678 commit bc07731
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 88 deletions.
80 changes: 80 additions & 0 deletions app/Concerns/CanGroupByDataLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use App\Enums\DataLevel;
use Illuminate\Database\Eloquent\Builder;
use Tpetry\QueryExpressions\Language\Alias;
use Tpetry\QueryExpressions\Value\Value;

trait CanGroupByDataLevel
{
public function scopeForDataLevel(Builder $query, DataLevel $level, ?string $country = null, ?int $county = null, ?int $locality = null, bool $aggregate = false): Builder
{
if ($level->is(DataLevel::TOTAL)) {
$query->groupByTotal();
}

if ($level->is(DataLevel::DIASPORA)) {
$query->whereNotNull('country_id')
->when($country, fn (Builder $query) => $query->where('country_id', $country));

if (! $aggregate) {
$query->groupByCountry();
}
}

if ($level->is(DataLevel::NATIONAL)) {
$query->whereNull('country_id');

if (filled($locality)) {
$query->where('locality_id', $locality)
->groupByLocality();
} elseif (filled($county)) {
$query->where('county_id', $county);

if ($aggregate) {
$query->groupByCounty();
} else {
$query->groupByLocality();
}
} else {
$query->whereNotNull('locality_id')
->whereNotNull('county_id');

if ($aggregate) {
$query->groupByTotal();
} else {
$query->groupByCounty();
}
}
}

return $query;
}

public function scopeGroupByCountry(Builder $query): Builder
{
return $query->groupBy('country_id')
->addSelect(new Alias('country_id', 'place'));
}

public function scopeGroupByCounty(Builder $query): Builder
{
return $query->groupBy('county_id')
->addSelect(new Alias('county_id', 'place'));
}

public function scopeGroupByLocality(Builder $query): Builder
{
return $query->groupBy('locality_id')
->addSelect(new Alias('locality_id', 'place'));
}

public function scopeGroupByTotal(Builder $query): Builder
{
return $query->addSelect(new Alias(new Value(0), 'place'));
}
}
35 changes: 0 additions & 35 deletions app/Concerns/CanGroupByPlace.php

This file was deleted.

27 changes: 27 additions & 0 deletions app/Livewire/Pages/ElectionResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace App\Livewire\Pages;

use App\Models\Vote;
use Illuminate\Support\Collection;
use Illuminate\Support\Number;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Layout;

class ElectionResults extends ElectionPage
Expand All @@ -13,4 +17,27 @@ public function render()
{
return view('livewire.pages.election-results');
}

#[Computed]
public function data(): Collection
{
return Vote::query()
->whereBelongsTo($this->election)
->forLevel(
level: $this->level,
country: null,
county: $this->county,
locality: null,
)
->with('votable')
->get()
->mapWithKeys(function (Vote $vote) {
return [
$vote->place => [
'value' => Number::format(ensureNumeric($vote->votes)),
'color' => $vote->votable->color,
],
];
});
}
}
59 changes: 8 additions & 51 deletions app/Models/Turnout.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace App\Models;

use App\Concerns\BelongsToElection;
use App\Concerns\CanGroupByPlace;
use App\Concerns\CanGroupByDataLevel;
use App\Concerns\HasTemporaryTable;
use App\Contracts\TemporaryTable;
use App\Enums\Area;
Expand All @@ -22,7 +22,7 @@
class Turnout extends Model implements TemporaryTable
{
use BelongsToElection;
use CanGroupByPlace;
use CanGroupByDataLevel;
/** @use HasFactory<TurnoutFactory> */
use HasFactory;
use HasTemporaryTable;
Expand Down Expand Up @@ -109,55 +109,12 @@ public function locality(): BelongsTo

public function scopeForLevel(Builder $query, DataLevel $level, ?string $country = null, ?int $county = null, ?int $locality = null, bool $aggregate = false): Builder
{
$query->select([
new Alias(new Sum('initial_total'), 'initial_total'),
new Alias(new Sum('total'), 'total'),
]);

if ($level->is(DataLevel::TOTAL)) {
$query->groupByTotal();
}

if ($level->is(DataLevel::DIASPORA)) {
$query
->whereNotNull('country_id')
->when(
$country,
fn (Builder $query) => $query->where('country_id', $country),
);

if (! $aggregate) {
$query->groupByCountry();
}
}

if ($level->is(DataLevel::NATIONAL)) {
$query->whereNull('country_id');

if (filled($locality)) {
$query->where('locality_id', $locality)
->groupByLocality();
} elseif (filled($county)) {
$query->where('county_id', $county);

if ($aggregate) {
$query->groupByCounty();
} else {
$query->groupByLocality();
}
} else {
$query->whereNotNull('locality_id')
->whereNotNull('county_id');

if ($aggregate) {
$query->groupByTotal();
} else {
$query->groupByCounty();
}
}
}

return $query;
return $query
->select([
new Alias(new Sum('initial_total'), 'initial_total'),
new Alias(new Sum('total'), 'total'),
])
->forDataLevel($level, $country, $county, $locality, $aggregate);
}

public static function segments(): Collection
Expand Down
21 changes: 20 additions & 1 deletion app/Models/Vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
namespace App\Models;

use App\Concerns\BelongsToElection;
use App\Concerns\CanGroupByDataLevel;
use App\Concerns\HasTemporaryTable;
use App\Contracts\TemporaryTable;
use App\Enums\DataLevel;
use App\Enums\Part;
use Database\Factories\VoteFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Tpetry\QueryExpressions\Function\Aggregate\Sum;
use Tpetry\QueryExpressions\Language\Alias;

class Vote extends Model implements TemporaryTable
{
use BelongsToElection;
use CanGroupByDataLevel;
/** @use HasFactory<VoteFactory> */
use HasFactory;
use HasTemporaryTable;
Expand All @@ -29,6 +35,7 @@ class Vote extends Model implements TemporaryTable
*/
protected $fillable = [
'election_id',
'country_id',
'county_id',
'locality_id',
'section',
Expand All @@ -46,7 +53,7 @@ class Vote extends Model implements TemporaryTable
protected function casts(): array
{
return [
// 'part' => Part::class,
'part' => Part::class,
'votes' => 'integer',
];
}
Expand All @@ -56,6 +63,18 @@ public function votable(): MorphTo
return $this->morphTo();
}

public function scopeForLevel(Builder $query, DataLevel $level, ?string $country = null, ?int $county = null, ?int $locality = null, bool $aggregate = false): Builder
{
return $query
->select([
'votable_type',
'votable_id',
new Alias(new Sum('votes'), 'votes'),
])
->groupBy('votable_type', 'votable_id')
->forDataLevel($level, $country, $county, $locality, $aggregate);
}

public function getTemporaryTableUniqueColumns(): array
{
return ['election_id', 'county_id', 'country_id', 'section', 'votable_type', 'votable_id'];
Expand Down
5 changes: 4 additions & 1 deletion resources/views/livewire/pages/election-results.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
<livewire:map
:key="$this->mapKey()"
:country="$country"
:county="$county" />
:county="$county"
:level="$level"
:actionUrl="route('front.elections.results', $election)"
:data="$this->data->toArray()" />

{{ $election }}
</div>

0 comments on commit bc07731

Please sign in to comment.