-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
295 additions
and
482 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Concerns; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
trait HasTemporaryTable | ||
{ | ||
public function getTemporaryTable(): string | ||
{ | ||
return '_temp_' . $this->getTable(); | ||
} | ||
|
||
public static function saveToTemporaryTable(array $values): void | ||
{ | ||
DB::table((new static())->getTemporaryTable())->insert($values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Contracts; | ||
|
||
interface TemporaryTable | ||
{ | ||
public function getTemporaryTable(): string; | ||
|
||
public function getTemporaryTableUniqueColumns(): array; | ||
|
||
public static function saveToTemporaryTable(array $values): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
|
||
class CountryCodeNotFoundException extends Exception | ||
{ | ||
public function __construct(string $name) | ||
{ | ||
parent::__construct("Could not find country code for: {$name}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Contracts\TemporaryTable; | ||
use Exception; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class DeleteTemporaryTableData implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
public string $model; | ||
|
||
public int $electionId; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct(string $model, int $electionId) | ||
{ | ||
$this->model = $model; | ||
$this->electionId = $electionId; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$model = new $this->model; | ||
|
||
if (! $model instanceof TemporaryTable) { | ||
throw new Exception('Model must implement TemporaryTable contract'); | ||
} | ||
|
||
DB::table($model->getTemporaryTable()) | ||
->where('election_id', $this->electionId) | ||
->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 0 additions & 108 deletions
108
app/Jobs/Europarl240609/Turnout/ImportCountyStatisticsJob.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Contracts\TemporaryTable; | ||
use Exception; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class PersistTemporaryTableData implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
public string $model; | ||
|
||
public int $electionId; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct(string $model, int $electionId) | ||
{ | ||
$this->model = $model; | ||
$this->electionId = $electionId; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$model = new $this->model; | ||
|
||
if (! $model instanceof TemporaryTable) { | ||
throw new Exception('Model must implement TemporaryTable contract'); | ||
} | ||
|
||
$columns = collect($model->getFillable()) | ||
->sort(); | ||
|
||
$selectColumns = $columns | ||
->map(fn (string $column) => "`$column`") | ||
->implode(', '); | ||
|
||
$updateColumns = $columns | ||
->reject(fn (string $column) => \in_array($column, $model->getTemporaryTableUniqueColumns())) | ||
->map(fn (string $column) => "`$column` = `{$model->getTemporaryTable()}`.`$column`") | ||
->implode(', '); | ||
|
||
DB::unprepared(<<<"SQL" | ||
INSERT INTO `{$model->getTable()}` ({$selectColumns}) | ||
SELECT {$selectColumns} FROM `{$model->getTemporaryTable()}` WHERE `election_id` = {$this->electionId} | ||
ON DUPLICATE KEY UPDATE {$updateColumns}; | ||
SQL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.