Skip to content

Commit

Permalink
Added basecontroller
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisA2 committed Mar 20, 2024
1 parent 994ac79 commit 48809c2
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Http/Controllers/ContentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace EscolaLms\HeadlessH5P\Http\Controllers;

use EscolaLms\Core\Dtos\OrderDto;
use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
// use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\HeadlessH5P\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\HeadlessH5P\Dtos\ContentFilterCriteriaDto;
use EscolaLms\HeadlessH5P\Http\Controllers\Swagger\ContentApiSwagger;
use EscolaLms\HeadlessH5P\Http\Requests\ContentCreateRequest;
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Controllers/EditorApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace EscolaLms\HeadlessH5P\Http\Controllers;

use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
// use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\HeadlessH5P\Http\Controllers\EscolaLmsBaseController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use EscolaLms\HeadlessH5P\Http\Controllers\Swagger\EditorApiSwagger;
Expand Down
78 changes: 78 additions & 0 deletions src/Http/Controllers/EscolaLmsBaseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace EscolaLms\Core\Http\Controllers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Response;

abstract class EscolaLmsBaseController extends Controller
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

public function sendResponse($data, string $message = '', int $code = 200): JsonResponse
{
$body = [
'success' => $code >= 200 && $code < 300,
'message' => $message,
];
if (!is_null($data)) {
$body['data'] = $data;
}
return Response::json($body, $code);
}

public function sendError(string $error = '', int $code = 404): JsonResponse
{
return $this->sendResponse(null, $error, $code);
}

public function sendSuccess(string $message = '', int $code = 200): JsonResponse
{
return $this->sendResponse(null, $message, $code);
}

public function sendResponseForResource(JsonResource $resource, string $message = ''): JsonResponse
{
$request = request();
$wrappedResource = $resource->resource;
if ($wrappedResource instanceof LengthAwarePaginator) {
return $this->sendResponseForWrappedPaginator($request, $resource, $message);
}
if ($wrappedResource instanceof Model && $wrappedResource->wasRecentlyCreated) {
return $this->sendResponse($resource->toArray($request), $message, 201);
}
return $this->sendResponse($resource->toArray($request), $message);
}

private function sendResponseForWrappedPaginator(Request $request, JsonResource $resource, string $message = ''): JsonResponse
{
$wrappedResource = $resource->resource;
$meta = $wrappedResource->toArray();
if ($resource instanceof ResourceCollection) {
$data = $resource->toArray($request);
} else {
$data = $meta['data'];
}
unset($meta['data']);
return $this->sendResponseWithMeta($data, $meta, $message);
}

public function sendResponseWithMeta(array $data, array $meta, string $message = '', int $code = 200): JsonResponse
{
return Response::json([
'success' => $code >= 200 && $code < 300,
'data' => $data,
'meta' => $meta,
'message' => $message,
], $code);
}
}
3 changes: 2 additions & 1 deletion src/Http/Controllers/FilesApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace EscolaLms\HeadlessH5P\Http\Controllers;

use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
// use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\HeadlessH5P\Http\Controllers\EscolaLmsBaseController;
use Exception;
use Illuminate\Http\JsonResponse;
use EscolaLms\HeadlessH5P\Http\Controllers\Swagger\FilesApiSwagger;
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Controllers/LibraryApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace EscolaLms\HeadlessH5P\Http\Controllers;

use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
// use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\HeadlessH5P\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\HeadlessH5P\Http\Requests\LibraryDeleteRequest;
use EscolaLms\HeadlessH5P\Http\Requests\LibraryFilterRequest;
use EscolaLms\HeadlessH5P\Http\Requests\LibraryInstallRequest;
Expand Down

0 comments on commit 48809c2

Please sign in to comment.