Skip to content

Commit

Permalink
#50 - maintain response style (#57)
Browse files Browse the repository at this point in the history
* #55 - change routes for admin

* #43 - permission policy
#94 - add permissions

* #50 - maintain response style

* #51 - search elements by title and/or library_id (#58)

Co-authored-by: KD <do>

Co-authored-by: KD <do>
Co-authored-by: krz <krzysztof.dziedziech@escola.pl>
  • Loading branch information
KrzysztofDziedziechEscolasoft and krzEscola authored Jan 18, 2022
1 parent be83c53 commit 0bd52e9
Show file tree
Hide file tree
Showing 15 changed files with 318 additions and 133 deletions.
27 changes: 27 additions & 0 deletions src/Dtos/ContentFilterCriteriaDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace EscolaLms\HeadlessH5P\Dtos;

use EscolaLms\Core\Dtos\Contracts\InstantiateFromRequest;
use EscolaLms\Core\Dtos\CriteriaDto;
use EscolaLms\Core\Repositories\Criteria\Primitives\EqualCriterion;
use EscolaLms\Core\Repositories\Criteria\Primitives\LikeCriterion;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

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

if ($request->has('title')) {
$criteria->push(new LikeCriterion('title', $request->input('title')));
}
if ($request->has('library_id')) {
$criteria->push(new EqualCriterion('library_id', $request->input('library_id')));
}

return new self($criteria);
}
}
61 changes: 22 additions & 39 deletions src/Http/Controllers/ContentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

namespace EscolaLms\HeadlessH5P\Http\Controllers;

//use App\Http\Controllers\Controller;
use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\HeadlessH5P\Dtos\ContentFilterCriteriaDto;
use EscolaLms\HeadlessH5P\Http\Controllers\Swagger\ContentApiSwagger;
use EscolaLms\HeadlessH5P\Http\Requests\ContentDeleteRequest;
use EscolaLms\HeadlessH5P\Http\Requests\ContentListRequest;
use EscolaLms\HeadlessH5P\Http\Requests\ContentReadRequest;
use EscolaLms\HeadlessH5P\Http\Requests\ContentStoreRequest;
use EscolaLms\HeadlessH5P\Http\Requests\LibraryStoreRequest;
use EscolaLms\HeadlessH5P\Http\Resources\ContentIndexResource;
use EscolaLms\HeadlessH5P\Http\Resources\ContentResource;
use EscolaLms\HeadlessH5P\Repositories\Contracts\H5PContentRepositoryContract;
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ContentApiController extends Controller implements ContentApiSwagger
class ContentApiController extends EscolaLmsBaseController implements ContentApiSwagger
{
private HeadlessH5PServiceContract $hh5pService;
private H5PContentRepositoryContract $contentRepository;
Expand All @@ -29,90 +31,71 @@ public function __construct(HeadlessH5PServiceContract $hh5pService, H5PContentR

public function index(ContentListRequest $request): JsonResponse
{
$contentFilterDto = ContentFilterCriteriaDto::instantiateFromRequest($request);
$columns = ['title', 'id', 'library_id'];
$list = $request->get('per_page') !== null && $request->get('per_page') == 0 ? $this->contentRepository->unpaginatedList($columns) : $this->contentRepository->list($request->get('per_page'), $columns);
$list = $request->get('per_page') !== null && $request->get('per_page') == 0 ?
$this->contentRepository->unpaginatedList($contentFilterDto, $columns) :
$this->contentRepository->list($contentFilterDto, $request->get('per_page'), $columns);

return response()->json($list, 200);
return $this->sendResponseForResource(ContentIndexResource::collection($list));
}

public function update(ContentStoreRequest $request, int $id): JsonResponse
{
try {
$contentId = $this->contentRepository->edit($id, $request->get('title'), $request->get('library'), $request->get('params'), $request->get('nonce'));
} catch (Exception $error) {
return response()->json([
'error' => $error->getMessage(),
], 422);
return $this->sendError($error->getMessage(), 422);
}

return response()->json([
'id' => $contentId,
], 200);
return $this->sendResponse(['id' => $contentId]);
}

public function store(ContentStoreRequest $request): JsonResponse
{
try {
$contentId = $this->contentRepository->create($request->get('title'), $request->get('library'), $request->get('params'), $request->get('nonce'));
} catch (Exception $error) {
return response()->json([
'error' => $error->getMessage(),
], 422);
return $this->sendError($error->getMessage(), 422);
}

return response()->json([
'id' => $contentId,
], 200);
return $this->sendResponse(['id' => $contentId]);
}

public function destroy(ContentDeleteRequest $request, int $id): JsonResponse
{
try {
$contentId = $this->contentRepository->delete($id);
} catch (Exception $error) {
return response()->json([
'error' => $error->getMessage(),
], 422);
return $this->sendError($error->getMessage(), 422);
}

return response()->json([
'id' => $contentId,
], 200);
return $this->sendResponse(['id' => $contentId]);
}

public function show(ContentReadRequest $request, int $id): JsonResponse
{
try {
$settings = $this->hh5pService->getContentSettings($id);
} catch (Exception $error) {
return response()->json([
'error' => $error->getMessage(),
], 422);
return $this->sendError($error->getMessage(), 422);
}

return response()->json(
$settings,
200
);
return $this->sendResponse($settings);
}

public function upload(LibraryStoreRequest $request): JsonResponse
{
try {
$content = $this->contentRepository->upload($request->file('h5p_file'));
} catch (Exception $error) {
return response()->json([
'error' => $error->getMessage(),
], 422);
return $this->sendError($error->getMessage(), 422);
}

return response()->json(
$content,
200
);
return $this->sendResponseForResource(ContentResource::make($content));
}

public function download(ContentReadRequest $request, $id)
public function download(ContentReadRequest $request, $id): BinaryFileResponse
{
$filepath = $this->contentRepository->download($id);

Expand Down
12 changes: 5 additions & 7 deletions src/Http/Controllers/EditorApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

namespace EscolaLms\HeadlessH5P\Http\Controllers;

//use App\Http\Controllers\Controller;
use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use EscolaLms\HeadlessH5P\Http\Controllers\Swagger\EditorApiSwagger;
use EscolaLms\HeadlessH5P\Services\HeadlessH5PService;
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;

use Illuminate\Routing\Controller;
use Exception;

class EditorApiController extends Controller implements EditorApiSwagger
class EditorApiController extends EscolaLmsBaseController implements EditorApiSwagger
{
private HeadlessH5PServiceContract $hh5pService;

Expand All @@ -25,9 +22,10 @@ public function __invoke(Request $request, $id = null): JsonResponse
{
try {
$settings = $this->hh5pService->getEditorSettings($id);
return response()->json($settings, 200);

return $this->sendResponse($settings);
} catch (Exception $error) {
return response()->json(['error'=>$error->getMessage()], 422);
return $this->sendError($error->getMessage(), 422);
}
}
}
20 changes: 11 additions & 9 deletions src/Http/Controllers/FilesApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

namespace EscolaLms\HeadlessH5P\Http\Controllers;

use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use EscolaLms\HeadlessH5P\Http\Controllers\Swagger\FilesApiSwagger;
use EscolaLms\HeadlessH5P\Services\HeadlessH5PService;
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;
use EscolaLms\HeadlessH5P\Http\Requests\FilesStoreRequest;

// TODO add swagger

class FilesApiController extends Controller implements FilesApiSwagger
class FilesApiController extends EscolaLmsBaseController implements FilesApiSwagger
{
private HeadlessH5PServiceContract $hh5pService;

Expand All @@ -25,10 +21,16 @@ public function __construct(HeadlessH5PServiceContract $hh5pService)
public function __invoke(FilesStoreRequest $request, String $nonce = null): JsonResponse
{
try {
$result = $this->hh5pService->uploadFile($request->get('contentId'), $request->get('field'), $request->get('_token'), $nonce);
return response()->json($result);
$result = $this->hh5pService->uploadFile(
$request->get('contentId'),
$request->get('field'),
$request->get('_token'),
$nonce
);

return $this->sendResponse($result);
} catch (Exception $error) {
return response()->json(['error'=>$error->getMessage()], 422);
return $this->sendError($error->getMessage(), 422);
}
}
}
31 changes: 13 additions & 18 deletions src/Http/Controllers/LibraryApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

namespace EscolaLms\HeadlessH5P\Http\Controllers;

//use App\Http\Controllers\Controller;
use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\HeadlessH5P\Http\Requests\LibraryDeleteRequest;
use EscolaLms\HeadlessH5P\Http\Requests\LibraryListRequest;
use EscolaLms\HeadlessH5P\Http\Resources\LibraryResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use EscolaLms\HeadlessH5P\Http\Controllers\Swagger\LibraryApiSwagger;
use EscolaLms\HeadlessH5P\Services\HeadlessH5PService;
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;

use EscolaLms\HeadlessH5P\Http\Requests\LibraryStoreRequest;
use Illuminate\Routing\Controller;

class LibraryApiController extends Controller implements LibraryApiSwagger
class LibraryApiController extends EscolaLmsBaseController implements LibraryApiSwagger
{
private HeadlessH5PServiceContract $hh5pService;

Expand All @@ -27,21 +24,19 @@ public function index(LibraryListRequest $request): JsonResponse
{
$libraries = $this->hh5pService->listLibraries();

return response()->json($libraries, 200);
return $this->sendResponseForResource(LibraryResource::collection($libraries));
}

public function store(LibraryStoreRequest $request): JsonResponse
{
$valid = $this->hh5pService->validatePackage($request->file('h5p_file'));
if ($valid) {
$this->hh5pService->savePackage();

return $this->sendResponse($this->hh5pService->getMessages('updated'));
}

return response()->json([
'valid' => $valid,
'messages' => $this->hh5pService->getMessages('updated'),
'errors' => $this->hh5pService->getMessages('error'),
], $valid ? 200 : 422);
return $this->sendError($this->hh5pService->getMessages('error'), 422);
}

public function libraries(LibraryListRequest $request): JsonResponse
Expand All @@ -52,17 +47,17 @@ public function libraries(LibraryListRequest $request): JsonResponse
$request->get('minorVersion')
);

return response()->json($libraries, 200);
return $this->sendResponseForResource(LibraryResource::collection($libraries));
}

public function destroy(LibraryDeleteRequest $request, int $id): JsonResponse
{
$valid = $this->hh5pService->deleteLibrary($id);

return response()->json([
'valid' => $valid,
'messages' => $valid ? "Library $id deleted" : "",
'errors' => !$valid ? "Library $id note deleted" : "",
], $valid ? 200 : 422);
if ($valid) {
return $this->sendSuccess("Library $id deleted");
}

return $this->sendError("Library $id note deleted", 422);
}
}
18 changes: 18 additions & 0 deletions src/Http/Controllers/Swagger/ContentApiSwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,24 @@ public function update(ContentStoreRequest $request, int $id): JsonResponse;
* type="integer",
* )
* ),
* @OA\Parameter(
* name="title",
* description="search items by title",
* in="query",
* required=false,
* @OA\Schema(
* type="string",
* )
* ),
* @OA\Parameter(
* name="library_id",
* description="search items by library_id",
* in="query",
* required=false,
* @OA\Schema(
* type="integer",
* )
* ),
* @OA\Response(
* response=200,
* description="successful operation",
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Requests/ContentListRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public function authorize(): bool

public function rules(): array
{
return [];
return [
'title' => ['sometimes', 'nullable', 'string'],
'library_id' => ['sometimes', 'nullable', 'integer'],
];
}
}
17 changes: 17 additions & 0 deletions src/Http/Resources/ContentIndexResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace EscolaLms\HeadlessH5P\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ContentIndexResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'library_id' => $this->library_id,
];
}
}
34 changes: 34 additions & 0 deletions src/Http/Resources/ContentResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace EscolaLms\HeadlessH5P\Http\Resources;

use EscolaLms\HeadlessH5P\Models\H5PContent;
use Illuminate\Http\Resources\Json\JsonResource;

class ContentResource extends JsonResource
{
public function __construct(H5PContent $content)
{
$this->resource = $content;
}

public function toArray($request): array
{
return [
'id' => $this->id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'user_id' => $this->user_id,
'title' => $this->title,
'library_id' => $this->library_id,
'parameters' => $this->parameters,
'params' => $this->params,
'metadata' => $this->metadata,
'slug' => $this->slug,
'filtered' => $this->filtered,
'disable' => $this->disable,
'embed_type' => $this->embed_type,
'nonce' => $this->nonce,
];
}
}
Loading

0 comments on commit 0bd52e9

Please sign in to comment.