Skip to content

Commit

Permalink
sorting scorms by id and version
Browse files Browse the repository at this point in the history
  • Loading branch information
dawid.miklas committed Apr 20, 2023
1 parent 219518e commit 8685690
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/Http/Controllers/ScormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EscolaLms\Scorm\Http\Controllers;

use EscolaLms\Core\Dtos\OrderDto;
use EscolaLms\Scorm\Http\Controllers\Swagger\ScormControllerContract;
use EscolaLms\Scorm\Http\Requests\ScormDeleteRequest;
use EscolaLms\Scorm\Services\Contracts\ScormQueryServiceContract;
Expand Down Expand Up @@ -71,7 +72,7 @@ public function show(string $uuid, Request $request): View

public function index(ScormListRequest $request): JsonResponse
{
$list = $this->scormQueryService->get($request->pageParams(), ['*'], $request->searchParams());
$list = $this->scormQueryService->get($request->pageParams(), ['*'], $request->searchParams(), OrderDto::instantiateFromRequest($request));
return $this->sendResponse($list, "Scorm list fetched successfully");
}

Expand Down
7 changes: 4 additions & 3 deletions src/Services/Contracts/ScormQueryServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

namespace EscolaLms\Scorm\Services\Contracts;

use EscolaLms\Core\Dtos\OrderDto;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

interface ScormQueryServiceContract
{
public function get($per_page = 15, array $columns = ['*'], ?array $search = []);
public function paginated($per_page = 15, array $columns = ['*'], ?array $search = []): LengthAwarePaginator;
public function all(array $columns = ['*'], ?array $search = []): Collection;
public function get($per_page = 15, array $columns = ['*'], ?array $search = [], ?OrderDto $orderDto = null);
public function paginated($per_page = 15, array $columns = ['*'], ?array $search = [], ?OrderDto $orderDto = null): LengthAwarePaginator;
public function all(array $columns = ['*'], ?array $search = [], ?OrderDto $orderDto = null): Collection;
public function allScos(array $columns = ['*']): Collection;
}
13 changes: 8 additions & 5 deletions src/Services/ScormQueryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EscolaLms\Scorm\Services;

use EscolaLms\Core\Dtos\OrderDto;
use EscolaLms\Scorm\Repositories\Contracts\ScormRepositoryContract;
use EscolaLms\Scorm\Services\Contracts\ScormQueryServiceContract;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
Expand All @@ -17,24 +18,26 @@ public function __construct(ScormRepositoryContract $scormRepository)
$this->scormRepository = $scormRepository;
}

public function get($per_page = 15, array $columns = ['*'], ?array $search = [])
public function get($per_page = 15, array $columns = ['*'], ?array $search = [], ?OrderDto $orderDto = null)
{
return $per_page === null || $per_page === 0
? ['data' => $this->all($columns, $search)]
: $this->paginated(intval($per_page), $columns, $search);
? ['data' => $this->all($columns, $search, $orderDto)]
: $this->paginated(intval($per_page), $columns, $search, $orderDto);
}

public function paginated($per_page = 15, array $columns = ['*'], ?array $search = []): LengthAwarePaginator
public function paginated($per_page = 15, array $columns = ['*'], ?array $search = [], ?OrderDto $orderDto = null): LengthAwarePaginator
{
return $this->scormRepository
->listQuery($columns, $search)
->orderBy($orderDto?->getOrderBy() ?? 'id', $orderDto?->getOrder() ?? 'desc')
->paginate(intval($per_page));
}

public function all(array $columns = ['*'], ?array $search = []): Collection
public function all(array $columns = ['*'], ?array $search = [], ?OrderDto $orderDto = null): Collection
{
return $this->scormRepository
->listQuery($columns, $search)
->orderBy($orderDto?->getOrderBy() ?? 'id', $orderDto?->getOrder() ?? 'desc')
->get();
}

Expand Down
47 changes: 47 additions & 0 deletions tests/API/ScormAdminApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Illuminate\Http\UploadedFile;
use EscolaLms\Scorm\Tests\TestCase;
use Illuminate\Support\Facades\Storage;
use Peopleaps\Scorm\Entity\Scorm;
use Peopleaps\Scorm\Model\ScormModel;
use Peopleaps\Scorm\Model\ScormScoModel;

class ScormAdminApiTest extends TestCase
Expand Down Expand Up @@ -100,6 +102,51 @@ public function test_get_model_list_unpaginated(): void
->assertJsonCount(30, 'data.data');
}

public function test_get_model_list_with_sorts(): void
{
$scormOne = $this->createScorm();
$scormOne->version = Scorm::SCORM_12;
$scormOne->save();

$scormTwo = $this->createScorm();
$scormTwo->version = Scorm::SCORM_2004;
$scormTwo->save();


$response = $this->actingAs($this->user, 'api')->json('GET', '/api/admin/scorm', [
'order_by' => 'version',
'order' => 'ASC',
]);

$this->assertTrue($response->getData()->data->data[0]->version === Scorm::SCORM_12);
$this->assertTrue($response->getData()->data->data[1]->version === Scorm::SCORM_2004);

$response = $this->actingAs($this->user, 'api')->json('GET', '/api/admin/scorm', [
'order_by' => 'version',
'order' => 'DESC',
]);

$this->assertTrue($response->getData()->data->data[0]->version === Scorm::SCORM_2004);
$this->assertTrue($response->getData()->data->data[1]->version === Scorm::SCORM_12);

$response = $this->actingAs($this->user, 'api')->json('GET', '/api/admin/scorm', [
'order_by' => 'id',
'order' => 'ASC',
]);

$this->assertTrue($response->getData()->data->data[0]->id === $scormOne->getKey());
$this->assertTrue($response->getData()->data->data[1]->id === $scormTwo->getKey());

$response = $this->actingAs($this->user, 'api')->json('GET', '/api/admin/scorm', [
'order_by' => 'id',
'order' => 'DESC',
]);

$this->assertTrue($response->getData()->data->data[0]->id === $scormTwo->getKey());
$this->assertTrue($response->getData()->data->data[1]->id === $scormOne->getKey());

}

public function test_search_model_list(): void
{
$scormSco = $this->createManyScos(10)[3];
Expand Down

0 comments on commit 8685690

Please sign in to comment.