-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ create api endpoint for creating trips manually [closed-beta] (#2145)
- Loading branch information
1 parent
55bb49c
commit 5990031
Showing
15 changed files
with
370 additions
and
62 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,68 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\API\v1; | ||
|
||
use App\Enum\HafasTravelType; | ||
use App\Http\Controllers\Backend\Transport\ManualTripCreator; | ||
use App\Http\Resources\HafasTripResource; | ||
use App\Models\HafasOperator; | ||
use App\Models\HafasTrip; | ||
use App\Models\TrainStation; | ||
use App\Models\TrainStopover; | ||
use Carbon\Carbon; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Validation\Rules\Enum; | ||
|
||
class TripController extends Controller | ||
{ | ||
|
||
/** | ||
* Undocumented beta endpoint - only specific users have access | ||
* | ||
* @param Request $request | ||
* | ||
* @return HafasTripResource | ||
* | ||
* @todo add stopovers | ||
* @todo currently the stations need to be in the database. We need to add a fallback to HAFAS. | ||
* -> later solve the problem for non-existing stations | ||
*/ | ||
public function createTrip(Request $request): HafasTripResource { | ||
if (!auth()->user()?->hasRole('closed-beta')) { | ||
abort(403, 'this endpoint is currently only available for beta users'); | ||
} | ||
|
||
$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'], | ||
'originId' => ['required', 'exists:train_stations,ibnr'], | ||
'originDeparturePlanned' => ['required', 'date'], | ||
'destinationId' => ['required', 'exists:train_stations,ibnr'], | ||
'destinationArrivalPlanned' => ['required', 'date'], | ||
]); | ||
|
||
DB::beginTransaction(); | ||
|
||
$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['originId'])->firstOrFail(); | ||
$creator->originDeparturePlanned = Carbon::parse($validated['originDeparturePlanned']); | ||
$creator->destination = TrainStation::where('ibnr', $validated['destinationId'])->firstOrFail(); | ||
$creator->destinationArrivalPlanned = Carbon::parse($validated['destinationArrivalPlanned']); | ||
|
||
$trip = $creator->createTrip(); | ||
$creator->createOriginStopover(); | ||
$creator->createDestinationStopover(); | ||
|
||
DB::commit(); | ||
|
||
return new HafasTripResource($trip); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
app/Http/Controllers/Backend/Transport/ManualTripCreator.php
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,79 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Backend\Transport; | ||
|
||
use App\Enum\HafasTravelType; | ||
use App\Enum\TripSource; | ||
use App\Http\Controllers\Backend\Transport\ManualTripCreator as TripBackend; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\HafasOperator; | ||
use App\Models\HafasTrip; | ||
use App\Models\TrainStation; | ||
use App\Models\TrainStopover; | ||
use Carbon\Carbon; | ||
use Illuminate\Support\Str; | ||
|
||
class ManualTripCreator extends Controller | ||
{ | ||
|
||
private ?HafasTrip $trip; | ||
// | ||
public HafasTravelType $category; | ||
public string $lineName; | ||
public ?int $journeyNumber; | ||
public ?HafasOperator $operator; | ||
public TrainStation $origin; | ||
public Carbon $originDeparturePlanned; | ||
public TrainStation $destination; | ||
public Carbon $destinationArrivalPlanned; | ||
|
||
public function createTrip(): HafasTrip { | ||
$this->trip = HafasTrip::create([ | ||
'trip_id' => TripBackend::generateUniqueTripId(), | ||
'category' => $this->category, | ||
'number' => $this->lineName, | ||
'linename' => $this->lineName, | ||
'journey_number' => $this->journeyNumber, | ||
'operator_id' => $this->operator->id ?? null, | ||
'origin' => $this->origin->ibnr, | ||
'destination' => $this->destination->ibnr, | ||
'departure' => $this->originDeparturePlanned, | ||
'arrival' => $this->destinationArrivalPlanned, | ||
'source' => TripSource::USER, | ||
'user_id' => auth()->user()?->id ?? null, | ||
]); | ||
return $this->trip; | ||
} | ||
|
||
public function createOriginStopover(): TrainStopover { | ||
if ($this->trip === null) { | ||
throw new \InvalidArgumentException('Cannot create stopover without trip'); | ||
} | ||
return TrainStopover::create([ | ||
'trip_id' => $this->trip->trip_id, | ||
'train_station_id' => $this->origin->id, | ||
'arrival_planned' => $this->originDeparturePlanned, | ||
'departure_planned' => $this->originDeparturePlanned, | ||
]); | ||
} | ||
|
||
public function createDestinationStopover(): TrainStopover { | ||
if ($this->trip === null) { | ||
throw new \InvalidArgumentException('Cannot create stopover without trip'); | ||
} | ||
return TrainStopover::create([ | ||
'trip_id' => $this->trip->trip_id, | ||
'train_station_id' => $this->destination->id, | ||
'arrival_planned' => $this->destinationArrivalPlanned, | ||
'departure_planned' => $this->destinationArrivalPlanned, | ||
]); | ||
} | ||
|
||
public static function generateUniqueTripId(): string { | ||
$tripId = Str::uuid(); | ||
while (HafasTrip::where('trip_id', $tripId)->exists()) { | ||
return self::generateUniqueTripId(); | ||
} | ||
return $tripId; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.