Skip to content

Commit

Permalink
🗃️ add database layout for IFOPT
Browse files Browse the repository at this point in the history
see #2411
  • Loading branch information
MrKrisKrisu committed Mar 10, 2024
1 parent 334ecdd commit 6c9d80a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
29 changes: 26 additions & 3 deletions app/Models/Station.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,45 @@ class Station extends Model
use HasFactory, LogsActivity;

protected $table = 'train_stations';
protected $fillable = ['ibnr', 'wikidata_id', 'rilIdentifier', 'name', 'latitude', 'longitude', 'time_offset', 'shift_time'];
protected $fillable = [
'ibnr', 'wikidata_id', 'rilIdentifier',
'ifopt_a', 'ifopt_b', 'ifopt_c', 'ifopt_d', 'ifopt_e',
'name', 'latitude', 'longitude', 'time_offset', 'shift_time'
];
protected $hidden = ['created_at', 'updated_at', 'time_offset', 'shift_time'];
protected $casts = [
'id' => 'integer',
'rilIdentifier' => 'string',
'name' => 'string',
'ibnr' => 'integer',
'wikidata_id' => 'string',
'ifopt_a' => 'string',
'ifopt_b' => 'integer',
'ifopt_c' => 'integer',
'ifopt_d' => 'integer',
'ifopt_e' => 'integer',
'rilIdentifier' => 'string',
'name' => 'string',
'latitude' => 'float',
'longitude' => 'float',
];
protected $appends = ['ifopt'];

public function wikidataEntity(): BelongsTo {
return $this->belongsTo(WikidataEntity::class, 'wikidata_id', 'id');
}

public function getIfoptAttribute(): ?string {
if (!$this->ifopt_a) {
return null;
}
$ifopt = $this->ifopt_a;
foreach (['b', 'c', 'd', 'e'] as $level) {
if ($this->{"ifopt_$level"}) {
$ifopt .= ':' . $this->{"ifopt_$level"};
}
}
return $ifopt;
}

public function getActivitylogOptions(): LogOptions {
return LogOptions::defaults()
->dontSubmitEmptyLogs()
Expand Down
10 changes: 9 additions & 1 deletion database/factories/StationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ class StationFactory extends Factory
public function definition(): array {
return [
'ibnr' => $this->faker->unique()->numberBetween(8000001, 8999999),
'wikidata_id' => null,
'ifopt_a' => $this->faker->randomElement(['de', 'at', 'ch', 'fr']),
'ifopt_b' => $this->faker->numberBetween(10000, 99999),
'ifopt_c' => $this->faker->numberBetween(1, 9999),
'ifopt_d' => $this->faker->boolean() ? $this->faker->numberBetween(1, 9999) : null,
'ifopt_e' => $this->faker->boolean() ? $this->faker->numberBetween(1, 9) : null,
'rilIdentifier' => substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4),
'name' => $this->faker->unique()->city,
'latitude' => $this->faker->latitude,
'longitude' => $this->faker->longitude,
'rilIdentifier' => substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4),
'time_offset' => null,
'shift_time' => null,
];
}
}
31 changes: 31 additions & 0 deletions database/migrations/2024_03_10_211526_add_ifopt_to_stations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void {
Schema::table('train_stations', static function(Blueprint $table) {
$table->unsignedInteger('ifopt_e')->nullable()->comment('Platform')->after('wikidata_id');
$table->unsignedInteger('ifopt_d')->nullable()->comment('Area')->after('wikidata_id');
$table->unsignedInteger('ifopt_c')->nullable()->comment('Station')->after('wikidata_id');
$table->unsignedInteger('ifopt_b')->nullable()->comment('Municipal')->after('wikidata_id');
$table->string('ifopt_a')->nullable()->comment('Country')->after('wikidata_id');

$table->index(['ifopt_a', 'ifopt_b', 'ifopt_c', 'ifopt_d', 'ifopt_e'], 'ifopt');
});
}

public function down(): void {
Schema::table('train_stations', static function(Blueprint $table) {
$table->dropIndex('ifopt');
$table->dropColumn('ifopt_e');
$table->dropColumn('ifopt_d');
$table->dropColumn('ifopt_c');
$table->dropColumn('ifopt_b');
$table->dropColumn('ifopt_a');
});
}
};
8 changes: 8 additions & 0 deletions resources/views/admin/stations/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
<th>Name</th>
<td>{{ $station->name }}</td>
</tr>
<tr>
<th>IBNR</th>
<td>{{ $station->ibnr }}</td>
</tr>
<tr>
<th>IFOPT</th>
<td>{{ $station->ifopt }}</td>
</tr>
</table>
</div>
</div>
Expand Down

0 comments on commit 6c9d80a

Please sign in to comment.