149 lines
4.3 KiB
PHP
149 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\RainfallRequest;
|
|
use App\Http\Resources\RainfallCollection;
|
|
use App\Http\Resources\RainfallResource;
|
|
use App\Models\Rainfall;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
|
|
class RainfallController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the rainfalls.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$rainfalls = $request->user()
|
|
->rainfalls()
|
|
->orderByDesc('date')
|
|
->paginate(15);
|
|
|
|
return new RainfallCollection($rainfalls);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(RainfallRequest $request)
|
|
{
|
|
$rainfall = $request->user()->rainfalls()->create($request->validated());
|
|
|
|
return response()->json(new RainfallResource($rainfall), 201);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Request $request, Rainfall $rainfall)
|
|
{
|
|
$rainfall = $request->user()->rainfalls()
|
|
->where('id', $rainfall->id)
|
|
->first();
|
|
|
|
return response()->json(new RainfallResource($rainfall));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(RainfallRequest $request, Rainfall $rainfall)
|
|
{
|
|
$rainfall = $request->user()->rainfalls()
|
|
->where('id', $rainfall->id)
|
|
->first()?->update($request->validated());
|
|
|
|
return response()->json(new RainfallResource($rainfall));
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Request $request, Rainfall $rainfall)
|
|
{
|
|
$request->user()->rainfalls()
|
|
->where('id', $rainfall->id)
|
|
->first()?->delete();
|
|
|
|
return response()->noContent();
|
|
}
|
|
|
|
public function lastRainfalls(Request $request)
|
|
{
|
|
$rainfalls = $request->user()
|
|
->rainfalls()
|
|
->orderByDesc('date')
|
|
->limit(5)
|
|
->get();
|
|
|
|
return response()->json(RainfallResource::collection($rainfalls));
|
|
}
|
|
|
|
public function graphValue(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'start' => 'date',
|
|
'end' => 'date',
|
|
'period' => ['regex:/^day|week|month|year$/'],
|
|
]);
|
|
|
|
$rainfalls = $request->user()
|
|
->rainfalls()
|
|
->whereBetween('date', [$data['start'], $data['end']])
|
|
->orderBy('date')
|
|
->get();
|
|
|
|
$results = [];
|
|
$currentDate = $data['start'];
|
|
while ($currentDate <= $data['end']) {
|
|
$key = Rainfall::getDateKey($currentDate, $data['period']);
|
|
if (! isset($results[$key])) {
|
|
$keyData = Rainfall::getDateKeyData($currentDate, $data);
|
|
$results[$key] = [...$keyData, 'value' => 0];
|
|
}
|
|
|
|
$results[$key]['value'] += $rainfalls->where('date', new Carbon($currentDate))->sum('value');
|
|
$currentDate = (new Carbon($currentDate))->addDay()->format('Y-m-d');
|
|
}
|
|
|
|
return response()->json(array_values($results));
|
|
}
|
|
|
|
public function lastMonths(Request $request)
|
|
{
|
|
$lang = array_values(array_filter($request->getLanguages(), fn ($v) => str_contains($v, '_')))[0] ?? 'en_US';
|
|
setlocale(LC_TIME, $lang);
|
|
Carbon::setLocale(explode('_', $lang)[0]);
|
|
|
|
$firstOfLastYear = now()->subYear()->firstOfYear();
|
|
$diff = now()->diffInMonths($firstOfLastYear);
|
|
|
|
$result = [];
|
|
for ($i = $diff; $i >= 0; $i--) {
|
|
$date = now()->subMonths($i);
|
|
$month = $date->month;
|
|
$firstOfMonth = now()->subMonths($i)->firstOfMonth();
|
|
$lastOfMonth = now()->subMonths($i)->lastOfMonth();
|
|
$rainfalls = $request->user()
|
|
->rainfalls()
|
|
->whereBetween('date', [$firstOfMonth, $lastOfMonth])
|
|
->sum('value');
|
|
|
|
if (! isset($result[$month])) {
|
|
$result[$month] = [];
|
|
}
|
|
|
|
$result[$month][] = [
|
|
'year' => $date->year,
|
|
'month' => $date->month,
|
|
'label' => $date->monthName,
|
|
'values' => (int) $rainfalls,
|
|
];
|
|
}
|
|
|
|
return response()->json($result);
|
|
}
|
|
}
|