Skip to content

Commit

Permalink
Use Key (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethsandaru authored Aug 19, 2023
1 parent a13c3a2 commit 50c3a96
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
7 changes: 7 additions & 0 deletions app/Models/DocumentTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ public function getTemplatingMode(): TemplatingMode

return TemplatingMode::from($rawMode);
}

public static function getByUuidOrKey(string $value): DocumentTemplate
{
return DocumentTemplate::where(function ($q) use ($value) {
$q->orWhere('uuid', $value)->orWhere('key', $value);
})->firstOrFail();
}
}
5 changes: 3 additions & 2 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ public function boot(): void
protected function configureRateLimiting(): void
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
return Limit::perMinute(600)->by($request->user()?->id ?: $request->ip());
});
}

private function registerModelBindings(): void
{
Route::model('documentFile', DocumentFile::class);
Route::model('documentTemplate', DocumentTemplate::class);
Route::model('font', Font::class);

Route::bind('documentTemplateUuidKey', DocumentTemplate::getByUuidOrKey(...));
}
}
16 changes: 11 additions & 5 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@
Route::get('access', [AuthController::class, 'access']);

Route::resource('document-templates', DocumentTemplateController::class)
->parameter('document-template', 'documentTemplate')
->parameter('document-templates', 'documentTemplateUuidKey')
->except([
'create',
'edit',
]);
Route::post(
'document-templates/{documentTemplate}/preview-html',
'document-templates/{documentTemplateUuidKey}/preview-html',
[DocumentTemplateController::class, 'previewHtml']
);
Route::post(
'document-templates/{documentTemplate}/duplicate',
'document-templates/{documentTemplateUuidKey}/duplicate',
[DocumentTemplateController::class, 'duplicate']
);

Route::post('document-templates/{documentTemplate}/pdfs', [PdfRenderController::class, 'render']);
Route::post('document-templates/{documentTemplate}/pdfs-async', [PdfRenderController::class, 'renderAsync']);
Route::post(
'document-templates/{documentTemplateUuidKey}/pdfs',
[PdfRenderController::class, 'render']
);
Route::post(
'document-templates/{documentTemplateUuidKey}/pdfs-async',
[PdfRenderController::class, 'renderAsync']
);

Route::get('document-files', [DocumentFileController::class, 'index']);
Route::get('document-files/{documentFile}', [DocumentFileController::class, 'show']);
Expand Down
56 changes: 56 additions & 0 deletions tests/Feature/DocumentTemplateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ public function testShowReturnsASingleTemplate()
]);
}

public function testShowReturnsASingleTemplateUseKey()
{
$template = DocumentTemplate::factory()->create([
'key' => 'seth-tran-key',
]);

$this->json('GET', 'api/v1/document-templates/seth-tran-key')
->assertOk()
->assertJsonFragment([
'uuid' => $template->uuid,
'key' => $template->key,
'category' => $template->category,
'title' => $template->title,
'template' => $template->template,
]);
}

public function testStoreCreateNewTemplate()
{
Event::fake([
Expand Down Expand Up @@ -136,6 +153,45 @@ public function testUpdateUpdatesASingleTemplate()
);
}

public function testUpdateUpdatesASingleTemplateUseKey()
{
Event::fake([
DocumentTemplateUpdated::class,
]);

$template = DocumentTemplate::factory()->create();

$this->json('PUT', 'api/v1/document-templates/' . $template->key, [
'category' => 'Hihi',
'title' => 'Funny template',
'template' => '<h1>Hello {{ $name }}</h1>',
'default_variables' => [
'name' => 'DocKing',
],
'metadata' => [
'driver' => 'gotenberg',
],
])
->assertOk()
->assertJsonFragment([
'uuid' => $template->uuid,
]);

$this->assertDatabaseHas('document_templates', [
'uuid' => $template->uuid,
'category' => 'Hihi',
'title' => 'Funny template',
'template' => '<h1>Hello {{ $name }}</h1>',
'default_variables->name' => 'DocKing',
'metadata->driver' => 'gotenberg',
]);

Event::assertDispatched(
DocumentTemplateUpdated::class,
fn (DocumentTemplateUpdated $event) => $event->template->is($template)
);
}

public function testDestroySoftDeletesTheTemplate()
{
Event::fake([
Expand Down

0 comments on commit 50c3a96

Please sign in to comment.