Skip to content

Commit

Permalink
Add logging for health data access and implement user-specific health…
Browse files Browse the repository at this point in the history
… data retrieval
  • Loading branch information
kennedyowusu committed Jan 4, 2025
1 parent c5549f6 commit b357ac6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
18 changes: 18 additions & 0 deletions app/Http/Controllers/api/v1/user/HealthDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Http\Resources\v1\HealthDataResource;
use App\Models\HealthData;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

/**
* @group Health Data
Expand Down Expand Up @@ -50,6 +51,11 @@ public function store(StoreHealthDataRequest $request)
*/
public function show(HealthData $healthData, Request $request)
{
Log::info('User attempting to view health data', [
'user_id' => $request->user()->id,
'health_data_id' => $healthData->id,
]);

if ($request->user()->cannot('view', $healthData)) {
return response()->json(['message' => 'Unauthorized.'], 403);
}
Expand Down Expand Up @@ -97,4 +103,16 @@ public function destroy(HealthData $healthData, Request $request)
$healthData->delete();
return response()->json(['message' => 'Health data deleted successfully.'], 200);
}

public function getHealthDataByUser($userId)
{
$healthData = HealthData::where('user_id', $userId)->first();

if (!$healthData) {
return response()->json(['message' => 'Health data not found.'], 404);
}

return new HealthDataResource($healthData);
}

}
4 changes: 2 additions & 2 deletions app/Http/Requests/v1/StoreHealthDataRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function authorize(): bool
public function rules(): array
{
return [
'height' => 'required|numeric|min:1|max:2.5',
'height' => 'required|numeric|min:1|max:2.72',
'weight_goal' => 'required|in:gain,lose,maintain',
];
}
Expand All @@ -37,7 +37,7 @@ public function messages(): array
{
return [
'height.min' => 'The height must be at least 1 meter.',
'height.max' => 'The height must be at most 2.5 meters.',
'height.max' => 'The height must be at most 2.72 meters.',
'weight_goal.in' => 'The weight_goal must be one of: gain, lose, maintain.',
];
}
Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
Route::get('health-data/{healthData}', [HealthDataController::class, 'show'])->middleware('throttle:20,1');
Route::put('health-data/{healthData}', [HealthDataController::class, 'update'])->middleware('throttle:10,1');
Route::delete('health-data/{healthData}', [HealthDataController::class, 'destroy'])->middleware('throttle:5,1');
Route::get('user-health-data/{userId}', [HealthDataController::class, 'getHealthDataByUser']);

// Admin-specific routes
Route::prefix('admin')->middleware(AdminMiddleware::class)->group(function () {
Expand Down

0 comments on commit b357ac6

Please sign in to comment.