Skip to content

Commit

Permalink
Statments browser list endpoint (#12)
Browse files Browse the repository at this point in the history
* WIP

* WIP

* Add policy

* Add order by filter

* fix

Co-authored-by: Tomasz Smolarek <[email protected]>
  • Loading branch information
dyfero and dyfero authored Apr 13, 2022
1 parent 497879f commit 930ee10
Show file tree
Hide file tree
Showing 24 changed files with 865 additions and 10 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
],
"psr-4": {
"EscolaLms\\Lrs\\": "src",
"EscolaLms\\Lrs\\Database\\Factories\\": "database/factories",
"EscolaLms\\Lrs\\Database\\Seeders\\": "database/seeders",
"EscolaLms\\Lrs\\Tests\\": "tests"
}
},
"autoload-dev": {
"psr-4": {
"EscolaLms\\Lrs\\": "src",
"EscolaLms\\Lrs\\Database\\Factories\\": "database/factories",
"EscolaLms\\Lrs\\Database\\Seeders\\": "database/seeders",
"EscolaLms\\Lrs\\Tests\\": "tests",
"App\\Exceptions\\": "tests/Exceptions"
Expand Down
56 changes: 56 additions & 0 deletions database/factories/StatementFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace EscolaLms\Lrs\Database\Factories;

use EscolaLms\Lrs\Models\Statement;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Trax\Auth\Stores\Accesses\Access;
use Trax\Auth\Stores\Owners\Owner;

class StatementFactory extends Factory
{
protected $model = Statement::class;

public function definition()
{
return [
'uuid' => (string) Str::uuid(),
'data' => $this->getJsonData(),
'voided' => false,
'owner_id' => Owner::inRandomOrder()->first(),
'entity_id' => null,
'client_id' => null,
'access_id' => Access::inRandomOrder()->first(),
'pending' => false,
'validation' => 1
];
}

private function getJsonData(): array
{
return [
'id' => (string) Str::uuid(),
'actor' => [
'objectType' => 'Agent',
'account' => [
'homePage' => "https://escolalms.com",
'name' => $this->faker->firstName . ' ' . $this->faker->lastName,
]
],
'context' => [
'registration' => (string) Str::uuid(),
],
'verb' => [
'id' => "http://adlnet.gov/expapi/verbs/progressed",
"display" => [
"en-US" => "progressed"
]
],
'object' => [
'objectType' => 'Activity'
],
'version' => '1.0.0'
];
}
}
24 changes: 24 additions & 0 deletions database/seeders/LrsPermissionSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace EscolaLms\Lrs\Database\Seeders;

use EscolaLms\Lrs\Enums\LrsPermissionEnum;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class LrsPermissionSeeder extends Seeder
{
public function run()
{
$admin = Role::findOrCreate('admin', 'api');

foreach (LrsPermissionEnum::asArray() as $const => $value) {
Permission::findOrCreate($value, 'api');
}

$admin->givePermissionTo([
LrsPermissionEnum::STATEMENT_LIST,
]);
}
}
1 change: 0 additions & 1 deletion database/seeders/LrsSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class LrsSeeder extends Seeder
{
public function run()
{

$name = 'EscolaLMS';

$owner = Owner::firstOrCreate([
Expand Down
6 changes: 5 additions & 1 deletion src/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace EscolaLms\Lrs;

use EscolaLms\Lrs\Extensions\AccessTokenGuard;
use EscolaLms\Lrs\Models\Statement;
use EscolaLms\Lrs\Policies\StatementPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Laravel\Passport\Passport;
Expand All @@ -14,7 +16,9 @@ class AuthServiceProvider extends ServiceProvider
*
* @var array
*/
protected $policies = [];
protected $policies = [
Statement::class => StatementPolicy::class
];

/**
* Register any authentication / authorization services.
Expand Down
81 changes: 81 additions & 0 deletions src/Dto/StatementSearchDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace EscolaLms\Lrs\Dto;

use Carbon\Carbon;
use EscolaLms\Core\Dtos\Contracts\DtoContract;
use EscolaLms\Core\Dtos\Contracts\InstantiateFromRequest;
use EscolaLms\Core\Dtos\CriteriaDto;
use EscolaLms\Core\Repositories\Criteria\Primitives\DateCriterion;
use EscolaLms\Lrs\Enums\QueryEnum;
use EscolaLms\Lrs\Repositories\Criteria\JsonCriteria;
use EscolaLms\Lrs\Repositories\Criteria\OrderCriterion;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class StatementSearchDto extends CriteriaDto implements DtoContract, InstantiateFromRequest
{
public static function instantiateFromRequest(Request $request): self
{
$criteria = new Collection();

$criteria->push(
new OrderCriterion(
$request->get('order_by') ?? QueryEnum::DEFAULT_SORT,
$request->get('order') ?? QueryEnum::DEFAULT_SORT_DIRECTION
)
);

if ($request->get('verb')) {
$criteria->push(
new JsonCriteria('data->verb->display->en-US', $request->get('verb'))
);
$criteria->push(
new JsonCriteria('data->verb->id', $request->get('verb'), null, false)
);
}
if ($request->get('account')) {
$criteria->push(
new JsonCriteria('data->actor->account->name', $request->get('account'))
);
$criteria->push(
new JsonCriteria('data->actor->account->homePage', $request->get('account'), null, false)
);
}
if ($request->get('registration')) {
$criteria->push(
new JsonCriteria('data->context->registration', $request->get('registration'))
);
}
if ($request->get('object')) {
$criteria->push(
new JsonCriteria('data->object->objectType', $request->get('object'))
);
}
if ($request->get('version')) {
$criteria->push(
new JsonCriteria('data->version', $request->get('version'))
);
}
if ($request->get('date_from')) {
$criteria->push(
new DateCriterion(
'created_at',
new Carbon($request->get('date_from')),
'>='
)
);
}
if ($request->get('date_to')) {
$criteria->push(
new DateCriterion(
'created_at',
new Carbon($request->get('date_to')),
'<='
)
);
}

return new static($criteria);
}
}
10 changes: 10 additions & 0 deletions src/Enums/LrsPermissionEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace EscolaLms\Lrs\Enums;

use EscolaLms\Core\Enums\BasicEnum;

class LrsPermissionEnum extends BasicEnum
{
public const STATEMENT_LIST = 'lrs_statement-list';
}
11 changes: 11 additions & 0 deletions src/Enums/QueryEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace EscolaLms\Lrs\Enums;

use EscolaLms\Core\Enums\BasicEnum;

class QueryEnum extends BasicEnum
{
public const DEFAULT_SORT = 'id';
public const DEFAULT_SORT_DIRECTION = 'desc';
}
7 changes: 6 additions & 1 deletion src/EscolaLmsLrsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace EscolaLms\Lrs;

use EscolaLms\Lrs\Repositories\Contracts\StatementRepositoryContract;
use EscolaLms\Lrs\Repositories\StatementRepository;
use EscolaLms\Lrs\Services\Contracts\StatementServiceContract;
use EscolaLms\Lrs\Services\StatementService;
use Illuminate\Support\ServiceProvider;
use EscolaLms\Lrs\Services\Contracts\LrsServiceContract;
use EscolaLms\Lrs\Services\LrsService;
Expand All @@ -19,6 +23,8 @@ class EscolaLmsLrsServiceProvider extends ServiceProvider
{
public $singletons = [
LrsServiceContract::class => LrsService::class,
StatementServiceContract::class => StatementService::class,
StatementRepositoryContract::class => StatementRepository::class,
];

private $requiredProviders = [
Expand All @@ -35,7 +41,6 @@ class EscolaLmsLrsServiceProvider extends ServiceProvider
*/
public function boot(): void
{

foreach ($this->requiredProviders as $provider) {
if (!app()->bound($provider)) {
$this->app->register($provider);
Expand Down
31 changes: 31 additions & 0 deletions src/Http/Controllers/StatementController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace EscolaLms\Lrs\Http\Controllers;

use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\Lrs\Dto\StatementSearchDto;
use EscolaLms\Lrs\Http\Controllers\Swagger\StatementSwagger;
use EscolaLms\Lrs\Http\Requests\StatementListRequest;
use EscolaLms\Lrs\Http\Resources\StatementResource;
use EscolaLms\Lrs\Services\Contracts\StatementServiceContract;
use Illuminate\Http\JsonResponse;

class StatementController extends EscolaLmsBaseController implements StatementSwagger
{
private StatementServiceContract $statementService;

public function __construct(StatementServiceContract $statementService)
{
$this->statementService = $statementService;
}

public function statements(StatementListRequest $request): JsonResponse
{
$results = $this->statementService->searchAndPaginate(
StatementSearchDto::instantiateFromRequest($request),
$request->get('per_page') ?? 15
);

return $this->sendResponseForResource(StatementResource::make($results), 'Statements retrieved successfully');
}
}
Loading

0 comments on commit 930ee10

Please sign in to comment.