Skip to content

Commit

Permalink
Add assignable morph fields (#37)
Browse files Browse the repository at this point in the history
* Add assignable morph fields

* add fields to resources

---------

Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero committed Mar 6, 2024
1 parent a097a12 commit 1e1a8cd
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddAssignableColumnsToFabricPdfsTable extends Migration
{

public function up(): void
{
Schema::table('fabric_pdfs', function (Blueprint $table) {
$table->nullableMorphs('assignable');
});
}

public function down(): void
{
Schema::table('fabric_pdfs', function (Blueprint $table) {
$table->dropMorphs('assignable');
});
}
}
5 changes: 3 additions & 2 deletions src/Core/PdfChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class PdfChannel extends AbstractTemplateChannelClass implements TemplateChannel
{
public static function send(EventWrapper $event, array $sections): bool
{

$varsService = TemplateFacade::getVariableClassName($event->eventClass(), PdfChannel::class);
$vars = array_merge(SettingsVariables::getSettingsValues(), $varsService::variablesFromEvent($event));

Expand All @@ -30,7 +29,9 @@ public static function send(EventWrapper $event, array $sections): bool
'template_id' => $sections['template_id'],
'title' => $sections['title'],
'content' => $sections['content'],
'vars' => $vars
'vars' => $vars,
'assignable_type' => $varsService::assignableClass(),
'assignable_id' => $varsService::assignableClass() ? $event->extractIdForPropertyOfClass($varsService::assignableClass()) : null,
]);

return true;
Expand Down
11 changes: 9 additions & 2 deletions src/Http/Controllers/FabricPdfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@
use EscolaLms\TemplatesPdf\Http\Resources\PdfListResource;
use EscolaLms\TemplatesPdf\Http\Resources\PdfResource;
use EscolaLms\TemplatesPdf\Models\FabricPDF;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use EscolaLms\TemplatesPdf\Services\Contracts\ReportBroServiceContract;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class FabricPdfController extends EscolaLmsBaseController implements FabricPdfControllerSwagger
class FabricPdfController extends EscolaLmsBaseController implements FabricPdfControllerSwagger
{
public function index(PdfListingRequest $request): JsonResponse
{
$pdfs = FabricPDF::where('user_id', auth()->user()->id)
$pdfs = FabricPDF::query()
->where('user_id', auth()->user()->id)
->when($request->has('assignable_type') && $request->has('assignable_id'),
fn(Builder $query) => $query
->where('assignable_type', $request->get('assignable_type'))
->where('assignable_id', $request->get('assignable_id'))
)
->paginate($request->get('per_page') ?? 15);

return $this->sendResponseForResource(PdfListResource::collection($pdfs), "pdfs list retrieved successfully");
Expand Down
18 changes: 18 additions & 0 deletions src/Http/Controllers/Swagger/FabricPdfControllerSwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ interface FabricPdfControllerSwagger
* default=15,
* ),
* ),
* @OA\Parameter(
* name="assignable_type",
* description="Assignable type (EscolaLms\Courses\Models\Course)",
* required=false,
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Parameter(
* name="assignable_id",
* description="Assignable ID",
* required=false,
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response=200,
* description="successful operation",
Expand Down
2 changes: 2 additions & 0 deletions src/Http/Resources/PdfListResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function toArray($request)
'template' => $this->template,
'user_id' => $this->user_id,
'vars' => VarsParser::parseVars($this->vars),
'assignable_type' => $this->assignable_type,
'assignable_id' => $this->assignable_id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
Expand Down
2 changes: 2 additions & 0 deletions src/Http/Resources/PdfResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function toArray($request)
'title' => $this->title,
'content' => $this->content ? json_decode($this->content) : null,
'vars' => VarsParser::parseVars($this->vars),
'assignable_type' => $this->assignable_type,
'assignable_id' => $this->assignable_id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
Expand Down
8 changes: 8 additions & 0 deletions src/Models/FabricPDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
* description="path to rendered PDF binary file",
* type="string",
* ),
* @OA\Property(
* property="assignable_type",
* type="string",
* ),
* @OA\Property(
* property="assignable_id",
* type="integer",
* ),
* )
*/
class FabricPDF extends Model
Expand Down
33 changes: 33 additions & 0 deletions tests/Api/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,39 @@ public function testListExisting(): void
$this->assertEquals($pdf->id, $seek->id);
}


public function testListExistingAssignable(): void
{
$pdf1 = FabricPDF::factory()->createOne(
[
'user_id' => $this->user->id,
'assignable_type' => 'EscolaLms\Test\Models\Test',
'assignable_id' => '1'
]
);
$pdf2 = FabricPDF::factory()->createOne(
[
'user_id' => $this->user->id,
]
);

$response = $this->actingAs($this->user)
->getJson('/api/pdfs?assignable_type=EscolaLms\Test\Models\Test&assignable_id=1');

$response->assertOk();
$response->assertJsonCount(1, 'data');

$seek = false;

foreach ($response->getData()->data as $item) {
if ($item->id == $pdf1->id) {
$seek = $pdf1;
}
}

$this->assertEquals($pdf1->id, $seek->id);
}

public function testListPdfsPagination(): void
{
FabricPDF::factory()
Expand Down

0 comments on commit 1e1a8cd

Please sign in to comment.