Skip to content

Commit

Permalink
🧑‍💻 use api endpoint for closed-beta createTrip-Test (#2165)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Nov 25, 2023
1 parent 6c1d0ca commit 7a15e65
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 99 deletions.
8 changes: 4 additions & 4 deletions app/Http/Controllers/API/v1/TripController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function createTrip(Request $request): HafasTripResource {
$validated = $request->validate([
'category' => ['required', new Enum(HafasTravelType::class)],
'lineName' => ['required'],
'journey_number' => ['nullable', 'numeric', 'min:1'],
'operator_id' => ['nullable', 'numeric', 'exists:hafas_operators,id'],
'journeyNumber' => ['nullable', 'numeric', 'min:1'],
'operatorId' => ['nullable', 'numeric', 'exists:hafas_operators,id'],
'originId' => ['required', 'exists:train_stations,ibnr'],
'originDeparturePlanned' => ['required', 'date'],
'destinationId' => ['required', 'exists:train_stations,ibnr'],
Expand All @@ -50,8 +50,8 @@ public function createTrip(Request $request): HafasTripResource {

$creator->category = HafasTravelType::from($validated['category']);
$creator->lineName = $validated['lineName'];
$creator->journeyNumber = $validated['journey_number'];
$creator->operator = HafasOperator::find($validated['operator_id']);
$creator->journeyNumber = $validated['journeyNumber'];
$creator->operator = HafasOperator::find($validated['operatorId']);
$creator->origin = TrainStation::where('ibnr', $validated['originId'])->firstOrFail();
$creator->originDeparturePlanned = Carbon::parse($validated['originDeparturePlanned']);
$creator->destination = TrainStation::where('ibnr', $validated['destinationId'])->firstOrFail();
Expand Down
39 changes: 0 additions & 39 deletions app/Http/Controllers/Frontend/Admin/TripController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,4 @@ public function renderTrip(string $id): View {
'trip' => $trip
]);
}

public function createTrip(Request $request): RedirectResponse {
$validated = $request->validate([
'origin' => ['required', 'numeric', 'exists:train_stations,ibnr'],
'departure' => ['required'],
'destination' => ['required', 'numeric', 'exists:train_stations,ibnr'],
'arrival' => ['required'],
'operator_id' => ['nullable', 'numeric', 'exists:hafas_operators,id'],
'category' => ['required', new Enum(HafasTravelType::class)],
'number' => ['required', 'string'],
'linename' => ['required', 'string'],
'journey_number' => ['required', 'numeric'],
]);

$creator = new ManualTripCreator();

$creator->category = HafasTravelType::from($validated['category']);
$creator->lineName = $validated['linename'];
$creator->journeyNumber = $validated['journey_number'];
$creator->operator = HafasOperator::find($validated['operator_id']);
$creator->origin = TrainStation::where('ibnr', $validated['origin'])->firstOrFail();
$creator->originDeparturePlanned = Carbon::parse(str_contains($validated['departure'], '+') && str_contains($validated['departure'], '-')
? $validated['departure'] : $validated['departure'] . '+00:00');
$creator->destination = TrainStation::where('ibnr', $validated['destination'])->firstOrFail();
$creator->destinationArrivalPlanned = Carbon::parse(str_contains($validated['arrival'], '+') && str_contains($validated['arrival'], '-')
? $validated['arrival'] : $validated['arrival'] . '+00:00');

$trip = $creator->createTrip();
$creator->createOriginStopover();
$creator->createDestinationStopover();
$trip->refresh();

return redirect()->route('trains.trip', [
'tripID' => $trip->trip_id,
'lineName' => $trip->linename,
'start' => $trip->origin,
'departure' => $trip->departure->toIso8601String(),
]);
}
}
1 change: 1 addition & 0 deletions database/seeders/UsersTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function run(): void {
'password' => Hash::make('thisisnotasecurepassword123'),
]);
$gertrud->assignRole('admin');
$gertrud->assignRole('closed-beta');

User::factory()
->create([
Expand Down
132 changes: 77 additions & 55 deletions resources/views/admin/trip/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,92 @@
It should be tested if the trip is created correctly and all data required for the trip is present, so no (500)
errors occur.
</div>
<div class="alert alert-danger">ToDo: Stopovers and some detailled information</div>
<div class="alert alert-danger">ToDo: Stopovers and some detailed information</div>

<form method="POST">
<form method="POST" action="/api/v1/trains/trip">
@csrf

<div class="form-group">
<label for="origin">Station Start</label>
<input type="number" class="form-control" id="origin" name="origin" placeholder="Station Start (IBNR!)"
required>
<div class="row">
<div class="col">
<div class="form-group">
<label for="originId">Origin Station (IBNR!)</label>
<input type="number" class="form-control" id="originId" name="originId"
placeholder="Origin Station (IBNR!)"
required>
</div>
</div>
<div class="col">
<div class="form-group">
<label for="departure">Departure at Origin (UTC!)</label>
<input type="datetime-local" class="form-control" id="originDeparturePlanned"
name="originDeparturePlanned" placeholder="Departure at Origin (UTC!)"
required>
</div>
</div>
</div>

<div class="form-group">
<label for="departure">Departure (UTC)</label>
<input type="datetime-local" class="form-control" id="departure" name="departure" placeholder="Departure"
required>
<div class="row">
<div class="col">
<div class="form-group">
<label for="destinationId">Destination Station (IBNR!)</label>
<input type="number" class="form-control" id="destinationId" name="destinationId"
placeholder="Destination Station (IBNR!)"
required>
</div>
</div>
<div class="col">
<div class="form-group">
<label for="destinationArrivalPlanned">Arrival at Destination (UTC!)</label>
<input type="datetime-local" class="form-control" id="destinationArrivalPlanned" name="destinationArrivalPlanned"
placeholder="Departure at Origin (UTC!)"
required>
</div>
</div>
</div>

<div class="form-group">
<label for="destination">Station Ziel</label>
<input type="number" class="form-control" id="destination" name="destination"
placeholder="Station Ziel (IBNR!)" required>
<div class="row">
<div class="col">
<div class="form-group">
<label for="operatorId">Operator</label>
<select class="form-control" id="operatorId" name="operatorId">
<option value="">select optionally</option>
@foreach(\App\Models\HafasOperator::orderBy('name')->get() as $operator)
<option value="{{ $operator->id }}">
{{ $operator->name }}
</option>
@endforeach
</select>
</div>
</div>
<div class="col">
<div class="form-group">
<label for="category">Category</label>
<select class="form-control" id="category" name="category" required>
<option value="">required</option>
@foreach(\App\Enum\HafasTravelType::cases() as $travelType)
<option>{{ $travelType }}</option>
@endforeach
</select>
</div>
</div>
</div>

<div class="form-group">
<label for="arrival">Arrival (UTC)</label>
<input type="datetime-local" class="form-control" id="arrival" name="arrival" placeholder="Arrival"
required>
</div>

<div class="form-group">
<label for="operator">Operator</label>
<select class="form-control" id="operator_id" name="operator_id">
<option value="">select optionally</option>
@foreach(\App\Models\HafasOperator::orderBy('name')->get() as $operator)
<option value="{{ $operator->id }}">{{ $operator->name }}</option>
@endforeach
</select>
</div>

<div class="form-group">
<label for="category">Category</label>
<select class="form-control" id="category" name="category" required>
<option value="">required</option>
@foreach(\App\Enum\HafasTravelType::cases() as $travelType)
<option>{{ $travelType }}</option>
@endforeach
</select>
</div>

<div class="form-group">
<label for="number">Number</label>
<input type="text" class="form-control" id="number" name="number"
placeholder="something internal - not important, but required" required>
</div>

<div class="form-group">
<label for="linename">Linename</label>
<input type="text" class="form-control" id="linename" name="linename" placeholder="e.g. ICE 76" required>
</div>

<div class="form-group">
<label for="journey_number">Journey Number</label>
<input type="text" class="form-control" id="journey_number" name="journey_number" placeholder="e.g. 85014"
required>
<div class="row">
<div class="col">
<div class="form-group">
<label for="journeyNumber">Journey Number</label>
<input type="text" class="form-control" id="journeyNumber" name="journeyNumber"
placeholder="e.g. 85014"
required>
</div>
</div>
<div class="col">
<div class="form-group">
<label for="lineName">LineName</label>
<input type="text" class="form-control" id="lineName" name="lineName" placeholder="e.g. ICE 76"
required>
</div>
</div>
</div>

<button type="submit" class="btn btn-success">
Expand Down
1 change: 0 additions & 1 deletion routes/web/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
Route::prefix('trip')->group(function() {
Route::view('/create', 'admin.trip.create')
->name('admin.trip.create');
Route::post('/create', [TripController::class, 'createTrip']);

Route::get('/{id}', [TripController::class, 'renderTrip'])
->name('admin.trip.show');
Expand Down

0 comments on commit 7a15e65

Please sign in to comment.