adding base of JSON API for others to consume

This commit is contained in:
2026-01-09 17:41:52 -07:00
parent 3bf399eb97
commit 40a7c041d1
3 changed files with 141 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\WeatherReport;
use Illuminate\Http\JsonResponse;
class WeatherController extends Controller
{
public function index(string $period = 'now'): JsonResponse
{
$period = in_array($period, ['now', 'daily', 'weekly']) ? $period : 'now';
return match ($period) {
'now' => $this->getCurrentConditions(),
'daily' => $this->getDailyForecast(),
'weekly' => $this->getWeeklyForecast(),
};
}
private function getCurrentConditions(): JsonResponse
{
$latestHourly = WeatherReport::query()
->where('type', 'hourly')
->with('periods')
->orderByDesc('reported_at')
->first();
$currentPeriod = $latestHourly?->periods->first();
if (! $currentPeriod) {
return response()->json(['error' => 'No current conditions available'], 404);
}
return response()->json([
'period' => 'now',
'reportedAt' => $latestHourly->reported_at->toIso8601String(),
'location' => [
'latitude' => $latestHourly->latitude,
'longitude' => $latestHourly->longitude,
],
'conditions' => [
'temperature' => $currentPeriod->temperature,
'temperatureUnit' => $currentPeriod->temperature_unit,
'shortForecast' => $currentPeriod->short_forecast,
'detailedForecast' => $currentPeriod->detailed_forecast,
'windSpeed' => $currentPeriod->wind_speed,
'windDirection' => $currentPeriod->wind_direction,
'humidity' => $currentPeriod->relative_humidity,
'dewpointCelsius' => $currentPeriod->dewpoint_celsius,
'precipitationProbability' => $currentPeriod->precipitation_probability,
'isDaytime' => $currentPeriod->is_daytime,
'startTime' => $currentPeriod->start_time->toIso8601String(),
'endTime' => $currentPeriod->end_time->toIso8601String(),
],
]);
}
private function getDailyForecast(): JsonResponse
{
$latestHourly = WeatherReport::query()
->where('type', 'hourly')
->with('periods')
->orderByDesc('reported_at')
->first();
if (! $latestHourly || $latestHourly->periods->isEmpty()) {
return response()->json(['error' => 'No daily forecast available'], 404);
}
$periods = $latestHourly->periods->map(fn ($period) => [
'periodNumber' => $period->period_number,
'startTime' => $period->start_time->toIso8601String(),
'endTime' => $period->end_time->toIso8601String(),
'isDaytime' => $period->is_daytime,
'temperature' => $period->temperature,
'temperatureUnit' => $period->temperature_unit,
'shortForecast' => $period->short_forecast,
'windSpeed' => $period->wind_speed,
'windDirection' => $period->wind_direction,
'humidity' => $period->relative_humidity,
'precipitationProbability' => $period->precipitation_probability,
]);
return response()->json([
'period' => 'daily',
'reportedAt' => $latestHourly->reported_at->toIso8601String(),
'location' => [
'latitude' => $latestHourly->latitude,
'longitude' => $latestHourly->longitude,
],
'periods' => $periods,
]);
}
private function getWeeklyForecast(): JsonResponse
{
$latestWeekly = WeatherReport::query()
->where('type', 'weekly')
->with('periods')
->orderByDesc('reported_at')
->first();
if (! $latestWeekly || $latestWeekly->periods->isEmpty()) {
return response()->json(['error' => 'No weekly forecast available'], 404);
}
$periods = $latestWeekly->periods->map(fn ($period) => [
'periodNumber' => $period->period_number,
'name' => $period->name,
'startTime' => $period->start_time->toIso8601String(),
'endTime' => $period->end_time->toIso8601String(),
'isDaytime' => $period->is_daytime,
'temperature' => $period->temperature,
'temperatureUnit' => $period->temperature_unit,
'shortForecast' => $period->short_forecast,
'detailedForecast' => $period->detailed_forecast,
'windSpeed' => $period->wind_speed,
'windDirection' => $period->wind_direction,
'precipitationProbability' => $period->precipitation_probability,
]);
return response()->json([
'period' => 'weekly',
'reportedAt' => $latestWeekly->reported_at->toIso8601String(),
'location' => [
'latitude' => $latestWeekly->latitude,
'longitude' => $latestWeekly->longitude,
],
'periods' => $periods,
]);
}
}