Move logic from route to controller
This commit is contained in:
parent
c5e64efb97
commit
733094b435
2 changed files with 88 additions and 77 deletions
85
app/Http/Controllers/TrackerController.php
Normal file
85
app/Http/Controllers/TrackerController.php
Normal file
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use DateTime;
|
||||
use App\Mail\WeeklyDigest;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class TrackerController extends Controller
|
||||
{
|
||||
public function show_past_trips_list() {
|
||||
return view('past-trips');
|
||||
}
|
||||
|
||||
public function show_trip(Request $request, string $tripId = null) {
|
||||
if (!$tripId && !config('app.current_trip_id')) return view('no-trip');
|
||||
|
||||
$viewMode = $request->input('show', null);
|
||||
|
||||
// If there is a file in the local cache that is less than 1–3 hours old
|
||||
// (depending on whether it was actively tracking at last check), use that.
|
||||
$tripFileName = ( $tripId ?? config('app.current_trip_id') ) . '.json';
|
||||
|
||||
if (Storage::disk('local')->exists($tripFileName)) {
|
||||
Log::debug("Cached trip file '{$tripFileName}' found...");
|
||||
$cachedData = json_decode(Storage::disk('local')->get($tripFileName))->trip;
|
||||
|
||||
if ($cachedData->is_active) {
|
||||
Log::debug("Cached trip file '{$tripFileName}' is for an active trip.");
|
||||
|
||||
$cachedDataUpdatedAt = new DateTime($cachedData->updated_at);
|
||||
$now = new DateTime();
|
||||
$cachingTimeout = ($cachedData->is_tracking) ? 1 : 3;
|
||||
$cachedDataAge = intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600);
|
||||
|
||||
if ($cachedDataAge <= $cachingTimeout) {
|
||||
Log::debug("Cached trip file '{$tripFileName}' is younger than {$cachingTimeout} hours, showing from cache...");
|
||||
return view('tracker', ['trip' => $cachedData, 'showAllCheckins' => ($viewMode === 'all')]);
|
||||
}
|
||||
} else {
|
||||
Log::debug("Cached trip file '{$tripFileName}' is for an old trip, showing from cache...");
|
||||
return view('tracker', ['trip' => $cachedData, 'showAllCheckins' => ($viewMode === 'all')]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Otherwise, download the latest tracking data.
|
||||
*/
|
||||
Log::debug("No cached trip file found for '{$tripFileName}'.");
|
||||
$client = new Client([
|
||||
'base_uri' => 'https://app.wayward.travel/',
|
||||
'timeout' => 3.0
|
||||
]);
|
||||
$response = $client->get('trip/'.($tripId ?? config('app.current_trip_id')).'/user/zmld8ko6qy7d9j3xvq10/json');
|
||||
|
||||
if ($response->getStatusCode() == 200) {
|
||||
$data = json_decode($response->getBody());
|
||||
|
||||
// Cache the downloaded file if it does not exist locally.
|
||||
if (Storage::disk('local')->missing($tripFileName)) {
|
||||
Log::debug("Caching new trip file '{$tripFileName}'.");
|
||||
Storage::disk('local')->put($tripFileName, json_encode($data));
|
||||
} else {
|
||||
$cachedData = json_decode(Storage::disk('local')->get($tripFileName));
|
||||
if ($data->trip->updated_at !== $cachedData->trip->updated_at) {
|
||||
Log::debug("Cached trip file '{$tripFileName}' has different 'updated_at' time, updating cache...");
|
||||
Storage::disk('local')->put($tripFileName, json_encode($data));
|
||||
// TODO: Cache photos locally
|
||||
} else {
|
||||
Log::debug("Cached trip file '{$tripFileName}' has same 'updated_at' time, showing from cache...");
|
||||
$data = $cachedData;
|
||||
}
|
||||
}
|
||||
|
||||
return view('tracker', ['trip' => $data->trip, 'showAllCheckins' => ($viewMode === 'all')]);
|
||||
} else {
|
||||
// TODO: Return proper error
|
||||
return 'Something went wrong';
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,82 +1,8 @@
|
|||
<?php
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Mail\WeeklyDigest;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use App\Http\Controllers\TrackerController;
|
||||
|
||||
Route::get('/past-trips', function () {
|
||||
return view('past-trips');
|
||||
});
|
||||
Route::get('/past-trips', [TrackerController::class, 'show_past_trips_list']);
|
||||
|
||||
Route::get('/{tripId?}', function (Request $request, $tripId = null) {
|
||||
//Mail::to('me@bengoldsworthy.net')->send(new WeeklyDigest());
|
||||
|
||||
if (!$tripId && !config('app.current_trip_id')) return view('no-trip');
|
||||
|
||||
$viewMode = $request->input('show', null);
|
||||
|
||||
// If there is a file in the local cache that is less than 1–3 hours old
|
||||
// (depending on whether it was actively tracking at last check), use that.
|
||||
$tripFileName = ( $tripId ?? config('app.current_trip_id') ) . '.json';
|
||||
|
||||
if (Storage::disk('local')->exists($tripFileName)) {
|
||||
Log::debug("Cached trip file '{$tripFileName}' found...");
|
||||
$cachedData = json_decode(Storage::disk('local')->get($tripFileName))->trip;
|
||||
|
||||
if ($cachedData->is_active) {
|
||||
Log::debug("Cached trip file '{$tripFileName}' is for an active trip.");
|
||||
|
||||
$cachedDataUpdatedAt = new DateTime($cachedData->updated_at);
|
||||
$now = new DateTime();
|
||||
$cachingTimeout = ($cachedData->is_tracking) ? 1 : 3;
|
||||
$cachedDataAge = intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600);
|
||||
|
||||
if ($cachedDataAge <= $cachingTimeout) {
|
||||
Log::debug("Cached trip file '{$tripFileName}' is younger than {$cachingTimeout} hours, showing from cache...");
|
||||
return view('tracker', ['trip' => $cachedData, 'showAllCheckins' => ($viewMode === 'all')]);
|
||||
}
|
||||
} else {
|
||||
Log::debug("Cached trip file '{$tripFileName}' is for an old trip, showing from cache...");
|
||||
return view('tracker', ['trip' => $cachedData, 'showAllCheckins' => ($viewMode === 'all')]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Otherwise, download the latest tracking data.
|
||||
*/
|
||||
Log::debug("No cached trip file found for '{$tripFileName}'.");
|
||||
$client = new Client([
|
||||
'base_uri' => 'https://app.wayward.travel/',
|
||||
'timeout' => 3.0
|
||||
]);
|
||||
$response = $client->get('trip/'.($tripId ?? config('app.current_trip_id')).'/user/zmld8ko6qy7d9j3xvq10/json');
|
||||
|
||||
if ($response->getStatusCode() == 200) {
|
||||
$data = json_decode($response->getBody());
|
||||
|
||||
// Cache the downloaded file if it does not exist locally.
|
||||
if (Storage::disk('local')->missing($tripFileName)) {
|
||||
Log::debug("Caching new trip file '{$tripFileName}'.");
|
||||
Storage::disk('local')->put($tripFileName, json_encode($data));
|
||||
} else {
|
||||
$cachedData = json_decode(Storage::disk('local')->get($tripFileName));
|
||||
if ($data->trip->updated_at !== $cachedData->trip->updated_at) {
|
||||
Log::debug("Cached trip file '{$tripFileName}' has different 'updated_at' time, updating cache...");
|
||||
Storage::disk('local')->put($tripFileName, json_encode($data));
|
||||
// TODO: Cache photos locally
|
||||
} else {
|
||||
Log::debug("Cached trip file '{$tripFileName}' has same 'updated_at' time, showing from cache...");
|
||||
$data = $cachedData;
|
||||
}
|
||||
}
|
||||
|
||||
return view('tracker', ['trip' => $data->trip, 'showAllCheckins' => ($viewMode === 'all')]);
|
||||
} else {
|
||||
// TODO: Return proper error
|
||||
return 'Something went wrong';
|
||||
}
|
||||
})->where('tripId', '[0-9a-z]{20}');
|
||||
Route::get('/{tripId?}', [TrackerController::class, 'show_trip'])->where('tripId', '[0-9a-z]{20}');
|
||||
|
|
Loading…
Reference in a new issue