Skip to content

Commit

Permalink
🗃️ Add unique key to TrainCheckin (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Nov 6, 2021
1 parent 667e4d3 commit 57e2e01
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 24 deletions.
10 changes: 10 additions & 0 deletions app/Exceptions/TrainCheckinAlreadyExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Exceptions;

use Exception;

class TrainCheckinAlreadyExistException extends Exception
{
//
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/API/TransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function TrainCheckin(Request $request) {
return $this->sendError('Given stations are not on the trip.', 400);
} catch (Throwable $exception) {
report($exception);
return $this->sendError('Unknown Error occured', 500);
return $this->sendError('Unknown Error occurred', 500);
}

}
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/API/v1/TransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Exceptions\CheckInCollisionException;
use App\Exceptions\HafasException;
use App\Exceptions\StationNotOnTripException;
use App\Exceptions\TrainCheckinAlreadyExistException;
use App\Http\Controllers\API\ResponseController;
use App\Http\Controllers\HafasController;
use App\Http\Controllers\StatusController as StatusBackend;
Expand Down Expand Up @@ -179,6 +180,8 @@ public function create(Request $request): JsonResponse {
} catch (HafasException $exception) {
$status?->delete();
return $this->sendv1Error($exception->getMessage(), 400);
} catch (TrainCheckinAlreadyExistException) {
return $this->sendv1Error('CheckIn already exists', 409);
}
}

Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/FrontendTransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Enum\TravelType;
use App\Exceptions\CheckInCollisionException;
use App\Exceptions\HafasException;
use App\Exceptions\TrainCheckinAlreadyExistException;
use App\Http\Controllers\Backend\EventController as EventBackend;
use App\Http\Controllers\TransportController as TransportBackend;
use Carbon\Carbon;
Expand Down Expand Up @@ -177,6 +178,8 @@ public function TrainCheckin(Request $request): RedirectResponse {
]
));

} catch (TrainCheckinAlreadyExistException) {
return redirect()->route('dashboard')->with('error', __('messages.exception.general'));
} catch (Throwable $exception) {
report($exception);
return redirect()
Expand Down
67 changes: 44 additions & 23 deletions app/Http/Controllers/TransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Exceptions\CheckInCollisionException;
use App\Exceptions\HafasException;
use App\Exceptions\StationNotOnTripException;
use App\Exceptions\TrainCheckinAlreadyExistException;
use App\Http\Controllers\Backend\GeoController;
use App\Http\Controllers\Backend\Social\TwitterController;
use App\Http\Controllers\Backend\Transport\PointsCalculationController;
Expand All @@ -28,6 +29,7 @@
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -251,6 +253,7 @@ public static function getTrainTrip(string $tripId, string $lineName, string $st
* @throws CheckInCollisionException
* @throws HafasException
* @throws StationNotOnTripException
* @throws TrainCheckinAlreadyExistException
* @deprecated replaced by createTrainCheckin()
*/
#[ArrayShape([
Expand Down Expand Up @@ -350,18 +353,26 @@ public static function TrainCheckin($tripId,
$stopovers[$offset2]['plannedArrival'] ?? $stopovers[$offset2]['plannedDeparture']
);

$trainCheckin = TrainCheckin::create([
'status_id' => $status->id,
'user_id' => $user->id,
'trip_id' => $tripId,
'origin' => $originStation->ibnr,
'destination' => $destinationStation->ibnr,
'distance' => $distanceInMeters,
'points' => $points,
'departure' => $plannedDeparture,
'arrival' => $plannedArrival
]);

try {
$trainCheckin = TrainCheckin::create([
'status_id' => $status->id,
'user_id' => $user->id,
'trip_id' => $tripId,
'origin' => $originStation->ibnr,
'destination' => $destinationStation->ibnr,
'distance' => $distanceInMeters,
'points' => $points,
'departure' => $plannedDeparture,
'arrival' => $plannedArrival
]);
} catch (QueryException $exception) {
$errorCode = $exception->errorInfo[1];
if ($errorCode == 1062) {
//duplicate entry
$status->delete();
throw new TrainCheckinAlreadyExistException();
}
}
$user->load(['statuses']);

// Let's connect our statuses and the events
Expand Down Expand Up @@ -513,6 +524,7 @@ public static function postTwitter(Status $status): void {
* @throws StationNotOnTripException
* @throws CheckInCollisionException
* @throws ModelNotFoundException
* @throws TrainCheckinAlreadyExistException
* @api v1
*/
public static function createTrainCheckin(
Expand Down Expand Up @@ -561,18 +573,27 @@ public static function createTrainCheckin(
departure: $firstStop->departure,
arrival: $lastStop->arrival
);
try {
$trainCheckin = TrainCheckin::create([
'status_id' => $status->id,
'user_id' => auth()->user()->id,
'trip_id' => $trip->trip_id,
'origin' => $firstStop->trainStation->ibnr,
'destination' => $lastStop->trainStation->ibnr,
'distance' => $distance,
'points' => $points,
'departure' => $firstStop->departure_planned,
'arrival' => $lastStop->arrival_planned
]);
} catch (QueryException $exception) {
$errorCode = $exception->errorInfo[1];
if ($errorCode == 1062) {
//duplicate entry
$status->delete();
throw new TrainCheckinAlreadyExistException();
}
}

$trainCheckin = TrainCheckin::create([
'status_id' => $status->id,
'user_id' => auth()->user()->id,
'trip_id' => $trip->trip_id,
'origin' => $firstStop->trainStation->ibnr,
'destination' => $lastStop->trainStation->ibnr,
'distance' => $distance,
'points' => $points,
'departure' => $firstStop->departure_planned,
'arrival' => $lastStop->arrival_planned
]);
foreach ($trainCheckin->alsoOnThisConnection as $otherStatus) {
if ($otherStatus?->user) {
$otherStatus->user->notify(new UserJoinedConnection(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

class AddUniqueKeyToTrainCheckins extends Migration
{

public function up(): void {
Schema::table('train_checkins', function(Blueprint $table) {
$table->unique(['user_id', 'trip_id', 'origin', 'departure'], 'user_trip_origin_departure');
});
}

public function down(): void {
Schema::table('train_checkins', function(Blueprint $table) {
$table->dropUnique('user_trip_origin_departure');
});
}
}

0 comments on commit 57e2e01

Please sign in to comment.