Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🗃️ add database layout for IFOPT #2413

Merged
merged 7 commits into from
Mar 15, 2024
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'];
HerrLevin marked this conversation as resolved.
Show resolved Hide resolved
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('Stop Place Component (or unused)')->after('wikidata_id');
$table->unsignedInteger('ifopt_d')->nullable()->comment('Stop Place or Stop Place Component')->after('wikidata_id');
$table->unsignedInteger('ifopt_c')->nullable()->comment('Mode or Stop Place')->after('wikidata_id');
$table->unsignedInteger('ifopt_b')->nullable()->comment('Administrative Area')->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
Loading