feat: history and calculate needs for user
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user