Initial commit: Laravel GPS API scaffolding
This commit is contained in:
73
app/Http/Controllers/GpsController.php
Normal file
73
app/Http/Controllers/GpsController.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\GpsPoint;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class GpsController extends Controller
|
||||
{
|
||||
// POST /api/gps
|
||||
public function store(Request $req)
|
||||
{
|
||||
$data = $req->validate([
|
||||
'device_id' => ['required','string','max:64'],
|
||||
'lat' => ['required','numeric','between:-90,90'],
|
||||
'lng' => ['required','numeric','between:-180,180'],
|
||||
'speed' => ['nullable','numeric'],
|
||||
'altitude' => ['nullable','numeric'],
|
||||
'heading' => ['nullable','integer','between:0,359'],
|
||||
'recorded_at' => ['nullable','date'], // ISO8601 preferred
|
||||
'meta' => ['nullable'],
|
||||
]);
|
||||
|
||||
$data['recorded_at'] = isset($data['recorded_at'])
|
||||
? Carbon::parse($data['recorded_at'])
|
||||
: now();
|
||||
|
||||
$point = GpsPoint::create($data);
|
||||
|
||||
return response()->json(['id' => $point->id], 201);
|
||||
}
|
||||
|
||||
// GET /api/gps/latest?device_id=abc
|
||||
public function latest(Request $req)
|
||||
{
|
||||
$req->validate(['device_id' => ['required','string']]);
|
||||
|
||||
$point = GpsPoint::where('device_id', $req->device_id)
|
||||
->orderByDesc('recorded_at')
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
|
||||
if (!$point) {
|
||||
return response()->json(['message' => 'Not found'], 404);
|
||||
}
|
||||
|
||||
return response()->json($point);
|
||||
}
|
||||
|
||||
// GET /api/gps/track?device_id=abc&since=2025-08-28T00:00:00Z&until=2025-08-28T23:59:59Z
|
||||
public function track(Request $req)
|
||||
{
|
||||
$req->validate([
|
||||
'device_id' => ['required','string'],
|
||||
'since' => ['nullable','date'],
|
||||
'until' => ['nullable','date'],
|
||||
'limit' => ['nullable','integer','between:1,10000'],
|
||||
]);
|
||||
|
||||
$q = GpsPoint::where('device_id', $req->device_id);
|
||||
|
||||
if ($req->since) $q->where('recorded_at', '>=', $req->since);
|
||||
if ($req->until) $q->where('recorded_at', '<=', $req->until);
|
||||
|
||||
$limit = $req->input('limit', 1000);
|
||||
|
||||
return response()->json(
|
||||
$q->orderBy('recorded_at')->orderBy('id')->limit($limit)->get()
|
||||
);
|
||||
}
|
||||
}
|
||||
23
app/Models/GpsPoint.php
Normal file
23
app/Models/GpsPoint.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class GpsPoint extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'device_id', 'lat', 'lng', 'speed', 'altitude', 'heading',
|
||||
'recorded_at', 'meta',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'recorded_at' => 'datetime',
|
||||
'meta' => 'array',
|
||||
'lat' => 'float',
|
||||
'lng' => 'float',
|
||||
'speed' => 'float',
|
||||
'altitude' => 'float',
|
||||
'heading' => 'integer',
|
||||
];
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
<?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::create('gps_points', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('device_id', 64)->index();
|
||||
$table->decimal('lat', 10, 7);
|
||||
$table->decimal('lng', 10, 7);
|
||||
$table->decimal('speed', 8, 2)->nullable(); // km/h or m/s
|
||||
$table->decimal('altitude', 8, 2)->nullable(); // meters
|
||||
$table->unsignedInteger('heading')->nullable(); // degrees
|
||||
$table->timestamp('recorded_at')->index(); // when device recorded it
|
||||
$table->json('meta')->nullable(); // any extra fields
|
||||
$table->timestamps(); // created_at, updated_at
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('gps_points');
|
||||
}
|
||||
};
|
||||
6
routes/api.php
Normal file
6
routes/api.php
Normal file
@ -0,0 +1,6 @@
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\GpsController;
|
||||
|
||||
Route::post('/gps', [GpsController::class, 'store']);
|
||||
Route::get('/gps/latest', [GpsController::class, 'latest']);
|
||||
Route::get('/gps/track', [GpsController::class, 'track']);
|
||||
Reference in New Issue
Block a user