136 lines
3.9 KiB
PHP
136 lines
3.9 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 Carbon\CarbonPeriod;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
|
|
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',
|
|
]);
|
|
|
|
$rainfalls = $request->user()
|
|
->rainfalls()
|
|
->whereBetween('date', [$data['start'], $data['end']])
|
|
->orderBy('date')
|
|
->get()
|
|
->groupBy('date');
|
|
|
|
$results = collect();
|
|
$index = 0;
|
|
foreach ($rainfalls as $date => $rainfall) {
|
|
if ($index === 0 && $date !== $data['start']) {
|
|
[$results, $index] = $this->addEmptyDays($results, $index, $data['start'], (new Carbon($date))->subDay());
|
|
} elseif ($index > 0 && (new Carbon($results->last()['date']))->addDay()->format('Y-m-d') !== $date) {
|
|
[$results, $index] = $this->addEmptyDays($results, $index, (new Carbon($results->last()['date']))->addDay(), (new Carbon($date))->subDay());
|
|
}
|
|
|
|
$results->push([
|
|
'id' => $index++,
|
|
'date' => $date,
|
|
'value' => $rainfall->sum('value'),
|
|
]);
|
|
}
|
|
|
|
if ($results->isNotEmpty() && $results->last()['date'] !== $data['end']) {
|
|
[$results] = $this->addEmptyDays($results, $index, (new Carbon($results->last()['date']))->addDay(), $data['end']);
|
|
}
|
|
|
|
return response()->json($results);
|
|
}
|
|
|
|
private function addEmptyDays(Collection $results, int $index, Carbon|string $start, Carbon|string $end)
|
|
{
|
|
foreach (CarbonPeriod::create($start, $end) as $date) {
|
|
$results->push([
|
|
'id' => $index++,
|
|
'date' => $date->format('Y-m-d'),
|
|
'value' => 0,
|
|
]);
|
|
}
|
|
|
|
return [$results, $index];
|
|
}
|
|
}
|