Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Nov 21, 2024
1 parent 404ed47 commit 4204ce5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/Concerns/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function fillSlugs(): void

$this->slug = Str::slug($this->slug);

if (! $this->slug || $this->slugAlreadyUsed($this->slug)) {
if (blank($this->slug) || $this->slugAlreadyUsed($this->slug)) {
$this->slug = $this->generateSlug();
}
}
Expand Down
28 changes: 20 additions & 8 deletions app/Console/Commands/Import/ImportElectionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Enums\ElectionType;
use App\Models\Election;
use Carbon\Carbon;
use stdClass;

class ImportElectionsCommand extends Command
Expand Down Expand Up @@ -45,16 +46,27 @@ public function handle(): int
);

$query->each(function (stdClass $row) {
$type = match ($row->BallotType) {
0 => ElectionType::REFERENDUM,
1 => ElectionType::PRESIDENTIAL,
2,3 => ElectionType::PARLIAMENTARY,
7 => ElectionType::EURO,
default => ElectionType::LOCAL,
};

$date = Carbon::parse($row->Date);

$slug = match ($type) {
ElectionType::PRESIDENTIAL => "prezidentiale-{$row->Name}-{$date->year}",
ElectionType::EURO => "europarlamentare {$date->year}",
default => "{$row->Name}-{$date->year}",
};

Election::create([
'title' => $row->Name,
'type' => match ($row->BallotType) {
0 => ElectionType::REFERENDUM,
1 => ElectionType::PRESIDENTIAL,
2,3 => ElectionType::PARLIAMENTARY,
7 => ElectionType::EURO,
default => ElectionType::LOCAL,
},
'date' => $row->Date,
'slug' => $slug,
'type' => $type,
'date' => $date->toDateString(),
'is_live' => false,
'has_lists' => match ($row->BallotType) {
// Referendum = 0,
Expand Down
7 changes: 7 additions & 0 deletions app/Livewire/Pages/ElectionResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Party;
use App\Models\Record;
use App\Models\Vote;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Number;
Expand All @@ -34,6 +35,9 @@ public function parties(): Collection
{
return Party::query()
->whereBelongsTo($this->election)
->whereHas('votes', function (Builder $query) {
$query->whereBelongsTo($this->election);
})
->with('media')
->get();
}
Expand All @@ -43,6 +47,9 @@ public function candidates(): Collection
{
return Candidate::query()
->whereBelongsTo($this->election)
->whereHas('votes', function (Builder $query) {
$query->whereBelongsTo($this->election);
})
->with('media')
->get();
}
Expand Down
5 changes: 0 additions & 5 deletions app/Models/Election.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;

class Election extends Model implements HasName, HasAvatar
{
Expand Down Expand Up @@ -56,10 +55,6 @@ protected static function booted(): void
->orderByDesc('year')
->orderByDesc('is_live');
});

static::creating(function (self $model) {
$model->slug = Str::slug("{$model->title}-{$model->date->year}");
});
}

public function scheduledJobs(): HasMany
Expand Down

0 comments on commit 4204ce5

Please sign in to comment.