Files
leonm 69372a49ed
CI / 🧪 Tests Laravel (push) Failing after 2m18s
CI / 🐳 Build & Push Images (push) Has been skipped
feat: history and calculate needs for user
2026-08-14 12:45:37 +02:00

56 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\ListWeightEntriesRequest;
use App\Http\Requests\StoreWeightEntryRequest;
use App\Http\Resources\WeightEntryResource;
use App\Models\WeightEntry;
use Carbon\CarbonImmutable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Response;
class WeightEntryController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(ListWeightEntriesRequest $request): AnonymousResourceCollection
{
$from = now()->subDays($request->integer('days', 365))->startOfDay();
$entries = $request->user()
->weightEntries()
->where('measured_at', '>=', $from)
->latest('measured_at')
->get();
return WeightEntryResource::collection($entries);
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreWeightEntryRequest $request): WeightEntryResource
{
$entry = $request->user()->weightEntries()->create([
'weight_kg' => $request->validated('weightKg'),
'measured_at' => CarbonImmutable::parse($request->validated('measuredAt') ?? now()),
]);
return new WeightEntryResource($entry);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Request $request, WeightEntry $weightEntry): Response
{
abort_unless($weightEntry->user_id === $request->user()->getKey(), 404);
$weightEntry->delete();
return response()->noContent();
}
}