Skip to content

Commit

Permalink
Merge pull request #569 from Muhammad235/refactor/endpoint-for-produc…
Browse files Browse the repository at this point in the history
…t-categories

fix: refactor product categories endpoint
  • Loading branch information
timiajayi authored Aug 24, 2024
2 parents 682f395 + 8070e4e commit 1f95017
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
18 changes: 16 additions & 2 deletions app/Console/Commands/StoreApiStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function handle()
$data = [
'api_group' => $item['name'],
'method' => $item['request']['method'],
'status' => $status,
'status' => $this->determineStatus($execution),
'response_time' => $response_time,
'last_checked' => $last_checked,
'details' => $this->getDetails($execution)
Expand Down Expand Up @@ -92,10 +92,24 @@ private function getDetails($execution)

if ($response_code >= 500 || $response_code === null) {
return 'API not responding (HTTP ' . ($response_code ?? 'Unknown') . ')';
} elseif ($response_time > 400) {
} elseif ($response_time > 600) {
return 'High response time detected';
} else {
return 'All tests passed';
}
}

private function determineStatus($execution)
{
$responseCode = $execution['response']['code'] ?? null;
$responseTime = $execution['response']['responseTime'] ?? null;

if ($responseCode >= 500 || $responseCode === null) {
return 'Down';
} elseif ($responseTime > 600) {
return 'Degraded';
} else {
return 'Operational';
}
}
}
39 changes: 5 additions & 34 deletions app/Http/Controllers/Api/V1/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,19 @@ class CategoryController extends Controller
public function index(Request $request)
{
try {
// Optional query parameters
$limit = $request->query('limit', 100);
$offset = $request->query('offset', 0);
$parent_id = $request->query('parent_id', null);

// Validate query parameters
$request->validate([
'limit' => 'integer|min:1',
'offset' => 'integer|min:0',
'parent_id' => 'integer|nullable',
]);

$categories = Category::when($parent_id, function ($query) use ($parent_id) {
return $query->where('parent_id', $parent_id);
})
->limit($limit)
->offset($offset)
->get(['id', 'name', 'description', 'slug', 'parent_id']);

\Log::info($categories);
$categories = Category::all();

return response()->json([
'status_code' => 200,
'categories' => $categories,
'message' => 'Categories returned successfully',
'data' => $categories,
]);
} catch (\Illuminate\Validation\ValidationException $e) {
return response()->json([
'status_code' => 400,
'error' => [
'code' => 'INVALID_QUERY_PARAMETER',
'message' => 'The provided query parameter is invalid.',
'details' => [
'invalid_parameter' => $e->validator->errors()->keys()[0],
'reason' => $e->validator->errors()->first(),
],
],
], 400);
} catch (\Exception $e) {

}catch (\Exception $e) {
return response()->json([
'status_code' => 500,
'error' => [
'code' => 'INTERNAL_SERVER_ERROR',
'message' => 'An unexpected error occurred while processing your request.',
'details' => [
'support_email' => '[email protected]',
Expand Down

0 comments on commit 1f95017

Please sign in to comment.