Skip to content

Commit

Permalink
Paginate methods added
Browse files Browse the repository at this point in the history
  • Loading branch information
Suraj Datheputhe committed Nov 26, 2024
1 parent aa3713c commit af85de8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ return api()->response('Operation completed successfully', $data = [], $status =
"data": []
}
```

#### `success`
Shortcut for a successful operation with HTTP status code 200.
Method for a successful operation with HTTP status code 200.

`success(string $message, mixed $data = [])`

Expand Down Expand Up @@ -116,6 +117,39 @@ public function index()
}
```

#### `paginate`
Return for a successful operation with HTTP paginated data.

`paginate(string $message, mixed $data = [])`

```php
public function index()
{
$users = UserResource::collection(User::active()->paginate(10));

return api()->success('Data fetched successfully.', $users);
}

// Result
{
"success": true,
"message": "Request processed successfully.",
"total": 20,
"total_pages": 2,
"per_page": 10,
"data": [
{
"id": 1",
"name": "Suraj....",
},
{
"id": 2",
"name": "Rabin....",
}
]
}
```

#### `created`
Returns a response indicating that a resource has been successfully created with HTTP status code 201.

Expand Down
1 change: 1 addition & 0 deletions src/Facades/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* @method static \Illuminate\Http\JsonResponse response(string $message, mixed $data = [], int $status = 200)
* @method static \Illuminate\Http\JsonResponse success(string $message, mixed $data = [])
* @method static \Illuminate\Http\JsonResponse paginate(string $message, mixed $data = [])
* @method static \Illuminate\Http\JsonResponse created(string $message, mixed $data = [])
* @method static \Illuminate\Http\JsonResponse error(string $message, int $status = 400)
* @method static \Illuminate\Http\JsonResponse unauthorized(string $message)
Expand Down
25 changes: 25 additions & 0 deletions src/Http/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SurazDott\ApiResponse\Http;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;

class ApiResponse
{
Expand Down Expand Up @@ -48,6 +49,30 @@ public function success(string $message, mixed $data = []): JsonResponse
return $this->toJson($response, 200);
}

/**
* Create a new paginated JSON response instance.
*/
public function paginate(string $message, mixed $data = []): JsonResponse
{
$response = [
'success' => true,
'message' => $message,
];

$resource = $data;

if ($resource instanceof JsonResource) {
$response['total'] = $data->total();
$response['total_pages'] = $data->lastPage();
$response['per_page'] = $data->perPage();
$response['data'] = $data;
}

$response['data'] = $data;

return $this->toJson($response, 200);
}

/**
* Method to return a response for successful resource creation.
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/ApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tests;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Pagination\LengthAwarePaginator;
use PHPUnit\Framework\Attributes\Test;
use SurazDott\ApiResponse\Facades\Api;

Expand Down Expand Up @@ -43,6 +45,36 @@ public function test_it_returns_a_success_response(): void
], $response->getData(true));
}

#[Test]
public function test_it_returns_a_paginated_response(): void
{
$message = 'Paginated response';

$data = new JsonResource(new LengthAwarePaginator(
['item1', 'item2'], // Items
100, // Total records
10, // Per page
1, // Current page
['path' => LengthAwarePaginator::resolveCurrentPath()]
));

$response = Api::paginate($message, $data);

$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals(200, $response->status());

$responseData = $response->getData(true);

$this->assertEquals([
'success' => true,
'message' => $message,
'total' => $responseData['total'],
'total_pages' => $responseData['total_pages'],
'per_page' => $responseData['per_page'],
'data' => $responseData['data'],
], $responseData);
}

#[Test]
public function test_it_returns_a_created_response(): void
{
Expand Down

0 comments on commit af85de8

Please sign in to comment.