Initial commit: full Laravel app + GPS API
This commit is contained in:
8
app/Http/Controllers/Controller.php
Normal file
8
app/Http/Controllers/Controller.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
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',
|
||||
];
|
||||
}
|
||||
48
app/Models/User.php
Normal file
48
app/Models/User.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
}
|
||||
24
app/Providers/AppServiceProvider.php
Normal file
24
app/Providers/AppServiceProvider.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user